diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-22 23:13:29 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:59 +0200 |
commit | b74611d2b20fc071b8a699f2ce25e61f60118d6e (patch) | |
tree | 55d4dbf727724f7f527f2acebea83abd34317329 /src/game/game.cpp | |
parent | b1183c712d94d38f75068bd62df006f73bd3550f (diff) |
refactor: improve input handling & remove commands
Diffstat (limited to 'src/game/game.cpp')
-rw-r--r-- | src/game/game.cpp | 98 |
1 files changed, 70 insertions, 28 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); } |