diff options
-rw-r--r-- | src/bootstrap.cpp | 29 | ||||
-rw-r--r-- | src/engine/engine.hpp | 1 | ||||
-rw-r--r-- | src/engine/graphics/statusline.cpp | 4 | ||||
-rw-r--r-- | src/engine/user/cursor.cpp | 37 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 9 | ||||
-rw-r--r-- | src/engine/user/input.hpp | 1 | ||||
-rw-r--r-- | src/game/game.cpp | 37 | ||||
-rw-r--r-- | src/game/game.hpp | 7 | ||||
-rw-r--r-- | src/game/status_manager.hpp | 1 | ||||
-rw-r--r-- | src/game/statusline_subscriber_adapter.hpp | 29 | ||||
-rw-r--r-- | src/game/statusline_subscriber_adapter.tpp | 31 | ||||
-rw-r--r-- | src/interfaces/command.hpp | 19 | ||||
-rw-r--r-- | src/interfaces/cursor.hpp | 16 | ||||
-rw-r--r-- | src/interfaces/game.hpp | 1 | ||||
-rw-r--r-- | src/interfaces/input.hpp | 3 | ||||
-rw-r--r-- | src/interfaces/publisher.hpp | 21 | ||||
-rw-r--r-- | src/interfaces/statusline_subscriber_adapter.hpp | 16 | ||||
-rw-r--r-- | src/interfaces/subscriber.hpp | 11 |
18 files changed, 30 insertions, 243 deletions
diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 6d42263..ef54940 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -12,7 +12,6 @@ #include "interfaces/scene.hpp" #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" -#include "interfaces/statusline_subscriber_adapter.hpp" // Implementations #include "argument_parser.hpp" @@ -27,7 +26,6 @@ #include "game/game.hpp" #include "game/generation_tracker.hpp" #include "game/status_manager.hpp" -#include "game/statusline_subscriber_adapter.hpp" #include "randomization/generator.hpp" #include "randomization/seed_generator.hpp" @@ -64,16 +62,12 @@ auto bootstrap() noexcept -> yacppdic::Container std::shared_ptr<IGenerationTracker> generation_tracker = container.get<IGenerationTrackerFactory>()(true); - auto vector2_statusline_subscriber_adapter_factory = - container.get<IStatusLineSubscriberAdapterFactory<Vector2>>(); - return std::make_unique<Game>( scene, cursor_controller, generation_tracker, status_manager, - user_input_observer, - vector2_statusline_subscriber_adapter_factory); + user_input_observer); }); container.bind<IRandomNumberGeneratorFactory>().to_factory( @@ -109,27 +103,6 @@ auto bootstrap() noexcept -> yacppdic::Container return std::make_unique<StatusManager>(statusline); }); - container.bind<IStatusLineSubscriberAdapterFactory<Vector2>>().to_factory( - [](const std::shared_ptr<IStatusManager> &status_manager, - std::vector<StatusLineSection> sections) - { - return std::make_unique<StatusLineSubscriberAdapter<Vector2>>( - status_manager, - [](std::size_t section_index, const Vector2 &position) - { - switch (section_index) - { - case 0: - return fmt::format("{}", position.get_x()); - case 1: - return fmt::format("{}", position.get_y()); - default: - return std::string(""); - } - }, - sections); - }); - container.bind<IGenerationTrackerFactory>().to_factory( [](bool is_paused) { diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index b34ea2b..4fb4707 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -1,6 +1,5 @@ #pragma once -#include "interfaces/command.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" #include "interfaces/game.hpp" diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp index 52edd8f..a8d0f3b 100644 --- a/src/engine/graphics/statusline.cpp +++ b/src/engine/graphics/statusline.cpp @@ -64,14 +64,14 @@ auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 auto scene_height = static_cast<Vector2::Value>(scene_size.get_height()); - _cursor_controller->move_to(Vector2({.x = x, .y = scene_height}), true); + _cursor_controller->move_to(Vector2({.x = x, .y = scene_height})); return previous_position; } void StatusLine::_move_back(Vector2 previous_position) noexcept { - _cursor_controller->move_to(previous_position, true); + _cursor_controller->move_to(previous_position); _cursor_controller->show(); } diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 945433b..97e9104 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -18,11 +18,9 @@ void CursorController::move(const Vector2 &direction, const uint32_t &amount) no std::cout.flush(); _position = _position.to_direction(direction, static_cast<Vector2::Value>(amount)); - - notify_subscribers(CursorEvent::POSITION_CHANGE, _position); } -void CursorController::move_to(const Vector2 &position, bool silent) noexcept +void CursorController::move_to(const Vector2 &position) noexcept { fmt::print( MOVE_CURSOR_TO, @@ -32,11 +30,6 @@ void CursorController::move_to(const Vector2 &position, bool silent) noexcept std::cout.flush(); _position = position; - - if (!silent) - { - notify_subscribers(CursorEvent::POSITION_CHANGE, position); - } } auto CursorController::where() const noexcept -> Vector2 @@ -55,8 +48,6 @@ void CursorController::ensure_position() noexcept scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); _position = Vector2(vector2_options); - - notify_subscribers(CursorEvent::POSITION_CHANGE, _position); } void CursorController::hide() noexcept @@ -70,29 +61,3 @@ void CursorController::show() noexcept fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } - -void CursorController::subscribe( - const Event &event, - const Subscriber &subscriber) noexcept -{ - if (_subscribers.count(event) == 0) - { - _subscribers.insert({event, std::vector<Subscriber>()}); - } - - _subscribers.at(event).push_back(subscriber); -} - -void CursorController::notify_subscribers(const Event &event, const Context &context) - const noexcept -{ - if (_subscribers.count(event) == 0) - { - return; - } - - for (const auto &subscriber : _subscribers.at(event)) - { - subscriber->update(context); - } -} diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index ae6fe3c..de8c3ae 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -38,7 +38,7 @@ public: void move(const Vector2 &direction, const uint32_t &amount) noexcept override; - void move_to(const Vector2 &position, bool silent) noexcept override; + void move_to(const Vector2 &position) noexcept override; [[nodiscard]] auto where() const noexcept -> Vector2 override; @@ -48,13 +48,6 @@ public: void show() noexcept override; - void subscribe(const Event &event, const Subscriber &subscriber) noexcept override; - - void notify_subscribers(const Event &event, const Context &context) - const noexcept override; - private: Vector2 _position; - - std::unordered_map<CursorEvent, std::vector<Subscriber>> _subscribers; }; diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp index 2fb97ac..3cec1b8 100644 --- a/src/engine/user/input.hpp +++ b/src/engine/user/input.hpp @@ -1,7 +1,6 @@ #pragma once #include "interfaces/input.hpp" -#include "interfaces/subscriber.hpp" #include <yacppdic/auto_wirable.hpp> 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 <fmt/core.h> -#include <chrono> #include <cstdlib> #include <iostream> #include <utility> @@ -12,16 +11,12 @@ Game::Game( std::shared_ptr<ICursorController> cursor_controller, std::shared_ptr<IGenerationTracker> generation_tracker, std::shared_ptr<IStatusManager> status_manager, - std::shared_ptr<IUserInputObserver> user_input_observer, - IStatusLineSubscriberAdapterFactory<Vector2> - vector2_statusline_subscriber_adapter_factory) noexcept + std::shared_ptr<IUserInputObserver> 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<ISubscriber<Vector2>> 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 <chrono> #include <memory> @@ -19,9 +18,7 @@ public: std::shared_ptr<ICursorController> cursor_controller, std::shared_ptr<IGenerationTracker> generation_tracker, std::shared_ptr<IStatusManager> status_manager, - std::shared_ptr<IUserInputObserver> user_input_observer, - IStatusLineSubscriberAdapterFactory<Vector2> - vector2_statusline_subscriber_adapter_factory) noexcept; + std::shared_ptr<IUserInputObserver> user_input_observer) noexcept; void on_start() noexcept override; @@ -35,8 +32,6 @@ private: std::shared_ptr<IGenerationTracker> _generation_tracker; std::shared_ptr<IStatusManager> _status_manager; std::shared_ptr<IUserInputObserver> _user_input_observer; - IStatusLineSubscriberAdapterFactory<Vector2> - _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 <memory> -#include <string> -#include <vector> - -template <typename Context> -class StatusLineSubscriberAdapter : public ISubscriber<Context> -{ -public: - StatusLineSubscriberAdapter( - std::shared_ptr<IStatusManager> status_manager, - std::string (*format_func)(std::size_t section_index, const Context &context), - const std::vector<StatusLineSection> §ions) noexcept; - - void update(const Context &context) noexcept override; - -private: - std::shared_ptr<IStatusManager> _status_manager; - - std::string (*_format_func)(std::size_t section_index, const Context &context); - std::vector<StatusLineSection> _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 <utility> - -template <typename Context> -StatusLineSubscriberAdapter<Context>::StatusLineSubscriberAdapter( - std::shared_ptr<IStatusManager> status_manager, - std::string (*format_func)(std::size_t section_index, const Context &context), - const std::vector<StatusLineSection> §ions - - ) noexcept - : _status_manager(std::move(status_manager)), - _format_func(format_func), - _sections(sections) -{ -} - -template <typename Context> -void StatusLineSubscriberAdapter<Context>::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++; - } -} diff --git a/src/interfaces/command.hpp b/src/interfaces/command.hpp deleted file mode 100644 index f433eab..0000000 --- a/src/interfaces/command.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "interfaces/subscriber.hpp" - -#include <cstddef> - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class ICommand : public ISubscriber<std::nullptr_t> -{ -public: - ~ICommand() override = default; - - virtual void execute() noexcept = 0; - - void update(const std::nullptr_t & /*context*/) noexcept override - { - execute(); - }; -}; diff --git a/src/interfaces/cursor.hpp b/src/interfaces/cursor.hpp index 053ff40..63aae41 100644 --- a/src/interfaces/cursor.hpp +++ b/src/interfaces/cursor.hpp @@ -1,8 +1,5 @@ #pragma once -#include "interfaces/publisher.hpp" -#include "interfaces/subscriber.hpp" - #include "engine/data/vector2.hpp" #include <memory> @@ -13,19 +10,18 @@ enum CursorEvent }; // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class ICursorController : public IPublisher<CursorEvent, Vector2> +class ICursorController { public: using Event = CursorEvent; using Context = Vector2; - using Subscriber = std::shared_ptr<ISubscriber<Context>>; - ~ICursorController() noexcept override = default; + virtual ~ICursorController() noexcept = default; virtual void move(const Vector2 &direction, const uint32_t &amount) noexcept = 0; // NOLINTNEXTLINE(google-default-arguments) - virtual void move_to(const Vector2 &position, bool silent = false) noexcept = 0; + virtual void move_to(const Vector2 &position) noexcept = 0; [[nodiscard]] virtual auto where() const noexcept -> Vector2 = 0; @@ -34,10 +30,4 @@ public: virtual void hide() noexcept = 0; virtual void show() noexcept = 0; - - void - subscribe(const Event &event, const Subscriber &subscriber) noexcept override = 0; - - void notify_subscribers(const Event &event, const Context &context) - const noexcept override = 0; }; diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp index 3aac6a9..e205fae 100644 --- a/src/interfaces/game.hpp +++ b/src/interfaces/game.hpp @@ -1,6 +1,5 @@ #pragma once -#include "interfaces/command.hpp" #include "interfaces/cursor.hpp" #include "interfaces/input.hpp" #include "interfaces/scene.hpp" diff --git a/src/interfaces/input.hpp b/src/interfaces/input.hpp index 8239e48..0797c0d 100644 --- a/src/interfaces/input.hpp +++ b/src/interfaces/input.hpp @@ -1,8 +1,5 @@ #pragma once -#include "interfaces/publisher.hpp" -#include "interfaces/subscriber.hpp" - #include <cstddef> #include <memory> diff --git a/src/interfaces/publisher.hpp b/src/interfaces/publisher.hpp deleted file mode 100644 index 2c47c34..0000000 --- a/src/interfaces/publisher.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "interfaces/subscriber.hpp" - -#include <functional> -#include <memory> - -template <typename Event, typename Context> -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class IPublisher -{ -public: - virtual ~IPublisher() noexcept = default; - - virtual void subscribe( - const Event &event, - const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept = 0; - - virtual void - notify_subscribers(const Event &event, const Context &context) const noexcept = 0; -}; diff --git a/src/interfaces/statusline_subscriber_adapter.hpp b/src/interfaces/statusline_subscriber_adapter.hpp deleted file mode 100644 index 925341f..0000000 --- a/src/interfaces/statusline_subscriber_adapter.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "interfaces/status_manager.hpp" -#include "interfaces/statusline.hpp" -#include "interfaces/subscriber.hpp" - -#include <yacppdic/factory.hpp> - -#include <memory> -#include <vector> - -template <typename Context> -using IStatusLineSubscriberAdapterFactory = - yacppdic::Factory<std::unique_ptr<ISubscriber<Context>>( - const std::shared_ptr<IStatusManager> &status_manager, - std::vector<StatusLineSection> sections)>; diff --git a/src/interfaces/subscriber.hpp b/src/interfaces/subscriber.hpp deleted file mode 100644 index 942c1d5..0000000 --- a/src/interfaces/subscriber.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -template <typename Context> -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class ISubscriber -{ -public: - virtual ~ISubscriber() = default; - - virtual void update(const Context &context) noexcept = 0; -}; |