diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/game.cpp | 98 | ||||
-rw-r--r-- | src/game/game.hpp | 8 | ||||
-rw-r--r-- | src/game/status_manager.cpp | 1 |
3 files changed, 76 insertions, 31 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp index b9f5e3a..5606e71 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1,13 +1,9 @@ #include "game.hpp" -#include "commands/insert_cell.hpp" -#include "commands/move_cursor.hpp" -#include "commands/quit.hpp" -#include "commands/toggle_pause.hpp" - #include <fmt/core.h> #include <chrono> +#include <cstdlib> #include <iostream> #include <utility> @@ -16,12 +12,14 @@ 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 : _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) { @@ -71,11 +69,63 @@ void Game::on_start() noexcept _status_manager->set_section_body(StatusLineSection::F, "0"); + _status_manager->set_section_body(StatusLineSection::G, "lol"); + _last_update_time = std::chrono::system_clock::now(); } void Game::on_update() noexcept { + if (_user_input_observer->is_key_pressed('q')) + { + std::exit(EXIT_SUCCESS); + } + + if (_user_input_observer->is_key_pressed('i')) + { + const auto position = _cursor_controller->where(); + + std::cout.put('x'); + std::cout.flush(); + + _cursor_controller->move_to(position); + + auto matrix = _scene->get_matrix(); + + const auto pos_offset = Vector2({.x = 0U, .y = 1U}); + + matrix->set(position - pos_offset, "#"); + } + + if (_user_input_observer->is_key_pressed('h')) + { + _move_cursor(Vector2::left()); + } + + if (_user_input_observer->is_key_pressed('j')) + { + _move_cursor(Vector2::down()); + } + + if (_user_input_observer->is_key_pressed('k')) + { + _move_cursor(Vector2::up()); + } + + if (_user_input_observer->is_key_pressed('l')) + { + _move_cursor(Vector2::right()); + } + + if (_user_input_observer->is_key_pressed('p')) + { + auto onoff = !_generation_tracker->get_is_paused(); + + _generation_tracker->set_is_paused(onoff); + + _status_manager->set_section_body(StatusLineSection::D, onoff ? "yes" : "no"); + } + const auto time_since_last_update = std::chrono::system_clock::now() - _last_update_time; @@ -84,6 +134,10 @@ void Game::on_update() noexcept fmt::format("{} nanoseconds", time_since_last_update.count())); _last_update_time = std::chrono::system_clock::now(); + + _status_manager->set_section_body( + StatusLineSection::G, + fmt::format("{}", _user_input_observer->is_key_pressed('v'))); } void Game::on_exit() const noexcept @@ -101,28 +155,16 @@ void Game::on_exit() const noexcept std::cout.flush(); } -auto Game::get_input_config() const noexcept - -> std::unordered_map<char, std::shared_ptr<ICommand>> +void Game::_move_cursor(const Vector2 &direction) noexcept { - return { - {'q', std::make_shared<QuitCommand>()}, - {'i', std::make_shared<InsertCellCommand>(_cursor_controller, _scene)}, - {'p', std::make_shared<TogglePauseCommand>(_generation_tracker, _status_manager)}, - {'k', - std::make_shared<MoveCursorCommand>(Vector2::up(), _cursor_controller, _scene)}, - {'j', - std::make_shared<MoveCursorCommand>( - Vector2::down(), - _cursor_controller, - _scene)}, - {'h', - std::make_shared<MoveCursorCommand>( - Vector2::left(), - _cursor_controller, - _scene)}, - {'l', - std::make_shared<MoveCursorCommand>( - Vector2::right(), - _cursor_controller, - _scene)}}; + const auto new_position = _cursor_controller->where().to_direction(direction, 1); + + const auto scene_size = _scene->size(); + + if (scene_size.validate_coords(new_position) != CoordsValidation::VALID) + { + return; + } + + _cursor_controller->move_to(new_position); } diff --git a/src/game/game.hpp b/src/game/game.hpp index fb2ad63..fb6a5ed 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -3,6 +3,7 @@ #include "interfaces/cursor.hpp" #include "interfaces/game.hpp" #include "interfaces/generation_tracker.hpp" +#include "interfaces/input.hpp" #include "interfaces/scene.hpp" #include "interfaces/status_manager.hpp" #include "interfaces/statusline_subscriber_adapter.hpp" @@ -18,6 +19,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; @@ -27,16 +29,16 @@ public: void on_exit() const noexcept override; - [[nodiscard]] auto get_input_config() const noexcept - -> std::unordered_map<char, std::shared_ptr<ICommand>> override; - private: 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; IStatusLineSubscriberAdapterFactory<Vector2> _vector2_statusline_subscriber_adapter_factory; std::chrono::system_clock::time_point _last_update_time; + + void _move_cursor(const Vector2 &direction) noexcept; }; diff --git a/src/game/status_manager.cpp b/src/game/status_manager.cpp index 33174d1..9bd4f37 100644 --- a/src/game/status_manager.cpp +++ b/src/game/status_manager.cpp @@ -18,6 +18,7 @@ void StatusManager::initialize() noexcept _statusline->set_section_length(StatusLineSection::D, 20U); _statusline->set_section_length(StatusLineSection::E, 25U); _statusline->set_section_length(StatusLineSection::F, 50U); + _statusline->set_section_length(StatusLineSection::G, 25U); _statusline->initialize_background(); } |