From 18e1db1fb8692752f64d3912c3b5ff1818be9028 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 May 2022 18:12:39 +0200 Subject: refactor: replace last subscriber patterned code --- src/game/game.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/game/game.cpp') diff --git a/src/game/game.cpp b/src/game/game.cpp index 3bd8738..c41364a 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -12,16 +11,12 @@ Game::Game( std::shared_ptr cursor_controller, std::shared_ptr generation_tracker, std::shared_ptr status_manager, - std::shared_ptr user_input_observer, - IStatusLineSubscriberAdapterFactory - vector2_statusline_subscriber_adapter_factory) noexcept + std::shared_ptr user_input_observer) noexcept : _scene(std::move(scene)), _cursor_controller(std::move(cursor_controller)), _generation_tracker(std::move(generation_tracker)), _status_manager(std::move(status_manager)), - _user_input_observer(std::move(user_input_observer)), - _vector2_statusline_subscriber_adapter_factory( - vector2_statusline_subscriber_adapter_factory) + _user_input_observer(std::move(user_input_observer)) { } @@ -36,15 +31,6 @@ void Game::on_start() noexcept _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( @@ -95,24 +81,43 @@ void Game::on_update() noexcept matrix->set(position - pos_offset, "#"); } + auto cursor_has_moved = false; + if (_user_input_observer->is_key_pressed('h')) { _move_cursor(Vector2::left()); + cursor_has_moved = true; } if (_user_input_observer->is_key_pressed('j')) { _move_cursor(Vector2::down()); + cursor_has_moved = true; } if (_user_input_observer->is_key_pressed('k')) { _move_cursor(Vector2::up()); + cursor_has_moved = true; } if (_user_input_observer->is_key_pressed('l')) { _move_cursor(Vector2::right()); + cursor_has_moved = true; + } + + if (cursor_has_moved) + { + const auto current_pos = _cursor_controller->where(); + + _status_manager->set_section_body( + StatusLineSection::B, + fmt::format("{}", current_pos.get_x())); + + _status_manager->set_section_body( + StatusLineSection::C, + fmt::format("{}", current_pos.get_y())); } if (_user_input_observer->is_key_pressed('p')) -- cgit v1.2.3-18-g5258