#include "game.hpp" #include "commands/insert_cell.hpp" #include "commands/move_cursor.hpp" #include "commands/quit.hpp" #include "commands/toggle_pause.hpp" #include #include #include #include Game::Game( std::shared_ptr scene, std::shared_ptr cursor_controller, std::shared_ptr generation_tracker, std::shared_ptr status_manager, IStatusLineSubscriberAdapterFactory vector2_statusline_subscriber_adapter_factory) noexcept : _scene(std::move(scene)), _cursor_controller(std::move(cursor_controller)), _generation_tracker(std::move(generation_tracker)), _status_manager(std::move(status_manager)), _vector2_statusline_subscriber_adapter_factory( vector2_statusline_subscriber_adapter_factory) { } void Game::on_start() noexcept { _status_manager->initialize(); _status_manager->set_section_title(StatusLineSection::A, ""); _status_manager->set_section_title(StatusLineSection::B, "X: "); _status_manager->set_section_title(StatusLineSection::C, "Y: "); _status_manager->set_section_title(StatusLineSection::D, "Paused: "); _status_manager->set_section_title(StatusLineSection::E, "Generation: "); _status_manager->set_section_title(StatusLineSection::F, "Time since last frame: "); std::shared_ptr> vector2_statusline_subscriber_adapter = _vector2_statusline_subscriber_adapter_factory( _status_manager, {StatusLineSection::B, StatusLineSection::C}); _cursor_controller->subscribe( CursorEvent::POSITION_CHANGE, vector2_statusline_subscriber_adapter); const auto scene_size = _scene->size(); const auto center_position = Vector2( {.x = static_cast(scene_size.get_width()) / 2, .y = static_cast(scene_size.get_height()) / 2}); _cursor_controller->move_to(center_position); _status_manager->set_section_body( StatusLineSection::B, fmt::format("{}", center_position.get_x())); _status_manager->set_section_body( StatusLineSection::C, fmt::format("{}", center_position.get_y())); _status_manager->set_section_body( StatusLineSection::D, _generation_tracker->get_is_paused() ? "yes" : "no"); _status_manager->set_section_body(StatusLineSection::E, "0"); _status_manager->set_section_body(StatusLineSection::F, "0"); _last_update_time = std::chrono::system_clock::now(); } void Game::on_update() noexcept { const auto time_since_last_update = std::chrono::system_clock::now() - _last_update_time; _status_manager->set_section_body( StatusLineSection::F, fmt::format("{} nanoseconds", time_since_last_update.count())); _last_update_time = std::chrono::system_clock::now(); } void Game::on_exit() const noexcept { for (auto row : *_scene->get_matrix()) { for (auto &col : row) { fmt::print("{}", col); } fmt::print("\n"); } std::cout.flush(); } auto Game::get_input_config() const noexcept -> std::unordered_map> { return { {'q', std::make_shared()}, {'i', std::make_shared(_cursor_controller, _scene)}, {'p', std::make_shared(_generation_tracker, _status_manager)}, {'k', std::make_shared(Vector2::up(), _cursor_controller, _scene)}, {'j', std::make_shared( Vector2::down(), _cursor_controller, _scene)}, {'h', std::make_shared( Vector2::left(), _cursor_controller, _scene)}, {'l', std::make_shared( Vector2::right(), _cursor_controller, _scene)}}; }