diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-08 16:06:03 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 3f9004b598fc8006576db9b8d2ae4e080101101b (patch) | |
tree | 97108db2a08253417139bd76741add2dd126f58d | |
parent | 2d9661790db30eb169d07d36b485943c598253b9 (diff) |
refactor: give game responsibility of statusline
-rw-r--r-- | src/bootstrap.cpp | 18 | ||||
-rw-r--r-- | src/engine/components/statusline.cpp | 1 | ||||
-rw-r--r-- | src/game/game.cpp | 16 | ||||
-rw-r--r-- | src/game/game.hpp | 4 | ||||
-rw-r--r-- | src/game/status_manager.cpp | 12 | ||||
-rw-r--r-- | src/game/status_manager.hpp | 13 | ||||
-rw-r--r-- | src/interfaces/status_manager.hpp | 9 |
7 files changed, 28 insertions, 45 deletions
diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 44ef211..e651512 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -29,12 +29,7 @@ #include "game/generation_tracker.hpp" #include "game/status_manager.hpp" -#include <fmt/core.h> - #include <memory> -#include <string> -#include <string_view> -#include <vector> auto bootstrap() noexcept -> yacppdic::Container { @@ -43,6 +38,7 @@ auto bootstrap() noexcept -> yacppdic::Container container.bind<IUserInputObserver>().to<UserInputObserver>(); container.bind<ICursorController>().to<CursorController>(); container.bind<ICLIGameEngine>().to<CLIGameEngine>(); + container.bind<IStatusManager>().to<StatusManager>(); container.bind<IGameFactory>().to_factory( [&container]( @@ -50,11 +46,10 @@ auto bootstrap() noexcept -> yacppdic::Container const std::shared_ptr<ICursorController> &cursor_controller, const std::shared_ptr<IUserInputObserver> &user_input_observer) { - std::shared_ptr<IStatusLine> statusline = container.get<IStatusLineFactory>()( - Bounds({.width = scene->size().get_width(), .height = 1})); + const auto statusline_factory = container.get<IStatusLineFactory>(); std::shared_ptr<IStatusManager> status_manager = - container.get<IStatusManagerFactory>()(statusline); + container.get<IStatusManager>(); std::shared_ptr<IGenerationTracker> generation_tracker = container.get<IGenerationTrackerFactory>()(true); @@ -62,6 +57,7 @@ auto bootstrap() noexcept -> yacppdic::Container const auto cell_helper_factory = container.get<ICellHelperFactory<char>>(); return std::make_unique<Game>( + statusline_factory, scene, cursor_controller, generation_tracker, @@ -93,12 +89,6 @@ auto bootstrap() noexcept -> yacppdic::Container return std::make_unique<StatusLine>(matrix_factory(size)); }); - container.bind<IStatusManagerFactory>().to_factory( - [](const std::shared_ptr<IStatusLine> &statusline) - { - return std::make_unique<StatusManager>(statusline); - }); - container.bind<IGenerationTrackerFactory>().to_factory( [](bool is_paused) { diff --git a/src/engine/components/statusline.cpp b/src/engine/components/statusline.cpp index df16e83..5b59a53 100644 --- a/src/engine/components/statusline.cpp +++ b/src/engine/components/statusline.cpp @@ -1,5 +1,6 @@ #include "statusline.hpp" +#include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "util/color.hpp" diff --git a/src/game/game.cpp b/src/game/game.cpp index 8d72324..3127d40 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1,5 +1,6 @@ #include "game.hpp" +#include "engine/data/bounds.hpp" #include "util/algorithm.hpp" #include <fmt/core.h> @@ -10,13 +11,15 @@ #include <utility> Game::Game( + IStatusLineFactory statusline_factory, std::shared_ptr<IScene> scene, 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, std::shared_ptr<ICellHelper> cell_helper) noexcept - : _scene(std::move(scene)), + : _statusline_factory(std::move(statusline_factory)), + _scene(std::move(scene)), _cursor_controller(std::move(cursor_controller)), _generation_tracker(std::move(generation_tracker)), _status_manager(std::move(status_manager)), @@ -27,9 +30,14 @@ Game::Game( void Game::on_start() noexcept { - _scene->register_component(_status_manager->get_statusline(), Vector2({0, 0})); + const auto scene_size = _scene->size(); + + std::shared_ptr<IStatusLine> statusline = + _statusline_factory(Bounds({.width = scene_size.get_width(), .height = 1})); + + _scene->register_component(statusline, Vector2({0, 0})); - _status_manager->initialize(); + _status_manager->bind(statusline); _status_manager->set_section_title(StatusLineSection::A, ""); _status_manager->set_section_title(StatusLineSection::B, "X: "); @@ -42,8 +50,6 @@ void Game::on_start() noexcept _status_manager->set_section_title(StatusLineSection::G, "Living cells: "); _status_manager->set_section_title(StatusLineSection::H, "Window size: "); - const auto scene_size = _scene->size(); - const auto center_position = Vector2( {.x = static_cast<Vector2::Value>(scene_size.get_width()) / 2, .y = static_cast<Vector2::Value>(scene_size.get_height()) / 2}); diff --git a/src/game/game.hpp b/src/game/game.hpp index d629400..8363c4d 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -8,6 +8,7 @@ #include "interfaces/matrix.hpp" #include "interfaces/scene.hpp" #include "interfaces/status_manager.hpp" +#include "interfaces/statusline.hpp" #include "engine/data/vector2.hpp" @@ -25,6 +26,7 @@ class Game : public IGame { public: Game( + IStatusLineFactory statusline_factory, std::shared_ptr<IScene> scene, std::shared_ptr<ICursorController> cursor_controller, std::shared_ptr<IGenerationTracker> generation_tracker, @@ -39,6 +41,8 @@ public: void on_exit() const noexcept override; private: + IStatusLineFactory _statusline_factory; + std::shared_ptr<IScene> _scene; std::shared_ptr<ICursorController> _cursor_controller; std::shared_ptr<IGenerationTracker> _generation_tracker; diff --git a/src/game/status_manager.cpp b/src/game/status_manager.cpp index c7d80e9..b4496ba 100644 --- a/src/game/status_manager.cpp +++ b/src/game/status_manager.cpp @@ -4,14 +4,10 @@ #include <utility> -StatusManager::StatusManager(std::shared_ptr<IStatusLine> statusline) noexcept - : _statusline(std::move(statusline)) - +void StatusManager::bind(const std::shared_ptr<IStatusLine> &statusline) noexcept { -} + _statusline = statusline; -void StatusManager::initialize() noexcept -{ _statusline->set_section_length(StatusLineSection::A, 5); _statusline->set_section_length(StatusLineSection::B, 15); _statusline->set_section_length(StatusLineSection::C, 15); @@ -46,7 +42,3 @@ void StatusManager::set_section_body( _statusline->set_status(section, body, section_title_length + 1); } -auto StatusManager::get_statusline() const noexcept -> std::shared_ptr<IStatusLine> -{ - return _statusline; -} diff --git a/src/game/status_manager.hpp b/src/game/status_manager.hpp index 245e888..85faf3c 100644 --- a/src/game/status_manager.hpp +++ b/src/game/status_manager.hpp @@ -1,20 +1,19 @@ #pragma once -#include "interfaces/generation_tracker.hpp" #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" -#include "engine/data/vector2.hpp" +#include <yacppdic/auto_wirable.hpp> #include <memory> +#include <string_view> #include <unordered_map> -class StatusManager : public IStatusManager +class StatusManager : public IStatusManager, + public yacppdic::AutoWirable<IStatusManager, StatusManager> { public: - explicit StatusManager(std::shared_ptr<IStatusLine> statusline) noexcept; - - void initialize() noexcept override; + void bind(const std::shared_ptr<IStatusLine> &statusline) noexcept override; void set_section_title( const StatusLineSection §ion, @@ -24,8 +23,6 @@ public: const StatusLineSection §ion, const std::string_view &body) noexcept override; - auto get_statusline() const noexcept -> std::shared_ptr<IStatusLine> override; - private: std::shared_ptr<IStatusLine> _statusline; diff --git a/src/interfaces/status_manager.hpp b/src/interfaces/status_manager.hpp index 9c7234e..7c56df1 100644 --- a/src/interfaces/status_manager.hpp +++ b/src/interfaces/status_manager.hpp @@ -2,8 +2,6 @@ #include "interfaces/statusline.hpp" -#include <yacppdic/factory.hpp> - #include <memory> #include <string_view> @@ -13,7 +11,7 @@ class IStatusManager public: virtual ~IStatusManager() = default; - virtual void initialize() noexcept = 0; + virtual void bind(const std::shared_ptr<IStatusLine> &statusline) noexcept = 0; virtual void set_section_title( const StatusLineSection §ion, @@ -22,10 +20,5 @@ public: virtual void set_section_body( const StatusLineSection §ion, const std::string_view &body) noexcept = 0; - - [[nodiscard]] virtual auto get_statusline() const noexcept - -> std::shared_ptr<IStatusLine> = 0; }; -using IStatusManagerFactory = yacppdic::Factory<std::unique_ptr<IStatusManager>( - const std::shared_ptr<IStatusLine> &statusline)>; |