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 +++++++++++++++++------------- src/game/game.hpp | 7 +----- src/game/status_manager.hpp | 1 - src/game/statusline_subscriber_adapter.hpp | 29 ----------------------- src/game/statusline_subscriber_adapter.tpp | 31 ------------------------- 5 files changed, 22 insertions(+), 83 deletions(-) delete mode 100644 src/game/statusline_subscriber_adapter.hpp delete mode 100644 src/game/statusline_subscriber_adapter.tpp (limited to 'src/game') 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')) diff --git a/src/game/game.hpp b/src/game/game.hpp index fb6a5ed..955269c 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -6,7 +6,6 @@ #include "interfaces/input.hpp" #include "interfaces/scene.hpp" #include "interfaces/status_manager.hpp" -#include "interfaces/statusline_subscriber_adapter.hpp" #include #include @@ -19,9 +18,7 @@ public: 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; void on_start() noexcept override; @@ -35,8 +32,6 @@ private: std::shared_ptr _generation_tracker; std::shared_ptr _status_manager; std::shared_ptr _user_input_observer; - IStatusLineSubscriberAdapterFactory - _vector2_statusline_subscriber_adapter_factory; std::chrono::system_clock::time_point _last_update_time; diff --git a/src/game/status_manager.hpp b/src/game/status_manager.hpp index ea61ff8..7bb4f2f 100644 --- a/src/game/status_manager.hpp +++ b/src/game/status_manager.hpp @@ -3,7 +3,6 @@ #include "interfaces/generation_tracker.hpp" #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" -#include "interfaces/subscriber.hpp" #include "engine/data/vector2.hpp" diff --git a/src/game/statusline_subscriber_adapter.hpp b/src/game/statusline_subscriber_adapter.hpp deleted file mode 100644 index c0c5805..0000000 --- a/src/game/statusline_subscriber_adapter.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "interfaces/statusline.hpp" -#include "interfaces/statusline_subscriber_adapter.hpp" -#include "interfaces/subscriber.hpp" - -#include -#include -#include - -template -class StatusLineSubscriberAdapter : public ISubscriber -{ -public: - StatusLineSubscriberAdapter( - std::shared_ptr status_manager, - std::string (*format_func)(std::size_t section_index, const Context &context), - const std::vector §ions) noexcept; - - void update(const Context &context) noexcept override; - -private: - std::shared_ptr _status_manager; - - std::string (*_format_func)(std::size_t section_index, const Context &context); - std::vector _sections; -}; - -#include "statusline_subscriber_adapter.tpp" diff --git a/src/game/statusline_subscriber_adapter.tpp b/src/game/statusline_subscriber_adapter.tpp deleted file mode 100644 index 9054772..0000000 --- a/src/game/statusline_subscriber_adapter.tpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "statusline_subscriber_adapter.hpp" - -#include - -template -StatusLineSubscriberAdapter::StatusLineSubscriberAdapter( - std::shared_ptr status_manager, - std::string (*format_func)(std::size_t section_index, const Context &context), - const std::vector §ions - - ) noexcept - : _status_manager(std::move(status_manager)), - _format_func(format_func), - _sections(sections) -{ -} - -template -void StatusLineSubscriberAdapter::update(const Context &context) noexcept -{ - for (std::size_t index = 0; const auto §ion : _sections) - { - auto status = _format_func(index, context); - - _status_manager->set_section_body(_sections[index], status); - - index++; - } -} -- cgit v1.2.3-18-g5258