From b74611d2b20fc071b8a699f2ce25e61f60118d6e Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 22 May 2022 23:13:29 +0200 Subject: refactor: improve input handling & remove commands --- src/game/game.cpp | 98 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 28 deletions(-) (limited to 'src/game/game.cpp') 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 #include +#include #include #include @@ -16,12 +12,14 @@ 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 : _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> +void Game::_move_cursor(const Vector2 &direction) noexcept { - return { - {'q', std::make_shared()}, - {'i', std::make_shared(_cursor_controller, _scene)}, - {'p', std::make_shared(_generation_tracker, _status_manager)}, - {'k', - std::make_shared(Vector2::up(), _cursor_controller, _scene)}, - {'j', - std::make_shared( - Vector2::down(), - _cursor_controller, - _scene)}, - {'h', - std::make_shared( - Vector2::left(), - _cursor_controller, - _scene)}, - {'l', - std::make_shared( - 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); } -- cgit v1.2.3-18-g5258