From 6d89d5db5140c04cc94bf4791a32a06a89e95b62 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 May 2022 19:47:25 +0200 Subject: refactor: improve input handling in game loop --- src/engine/user/input.cpp | 5 +++ src/engine/user/input.hpp | 2 ++ src/game/game.cpp | 83 +++++++++++++++++++++++++---------------------- src/game/game.hpp | 2 ++ src/interfaces/input.hpp | 2 ++ 5 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp index ac6d660..cbba143 100644 --- a/src/engine/user/input.cpp +++ b/src/engine/user/input.cpp @@ -38,6 +38,11 @@ bool UserInputObserver::is_key_pressed(Key key) noexcept return is_key_pressed; } +auto UserInputObserver::get_currently_pressed_key() const noexcept -> Key +{ + return _currently_pressed; +} + void UserInputObserver::clear_currently_pressed() noexcept { _currently_pressed_mutex.lock(); diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp index 3cec1b8..29534e8 100644 --- a/src/engine/user/input.hpp +++ b/src/engine/user/input.hpp @@ -18,6 +18,8 @@ public: bool is_key_pressed(Key key) noexcept override; + Key get_currently_pressed_key() const noexcept override; + void clear_currently_pressed() noexcept override; private: diff --git a/src/game/game.cpp b/src/game/game.cpp index de2b4df..41bafdf 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -61,51 +61,51 @@ void Game::on_start() noexcept 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, "#"); - } + const auto pressed_key = _user_input_observer->get_currently_pressed_key(); auto cursor_has_moved = false; - if (_user_input_observer->is_key_pressed('h')) + switch (pressed_key) { + case 'h': _move_cursor(Vector2::left()); cursor_has_moved = true; - } + break; - if (_user_input_observer->is_key_pressed('j')) - { + case 'j': _move_cursor(Vector2::down()); cursor_has_moved = true; - } + break; - if (_user_input_observer->is_key_pressed('k')) - { + case 'k': _move_cursor(Vector2::up()); cursor_has_moved = true; - } + break; - if (_user_input_observer->is_key_pressed('l')) - { + case 'l': _move_cursor(Vector2::right()); cursor_has_moved = true; + break; + + case 'q': + std::exit(EXIT_SUCCESS); + + case 'i': + _insert_cell(_cursor_controller->where(), 'x'); + break; + + case 'p': + { + auto onoff = !_generation_tracker->get_is_paused(); + + _generation_tracker->set_is_paused(onoff); + _status_manager->set_section_body(StatusLineSection::D, onoff ? "yes" : "no"); + + break; + } + + default: + break; } if (cursor_has_moved) @@ -121,15 +121,6 @@ void Game::on_update() noexcept fmt::format("{}", current_pos.get_y())); } - 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_now = std::chrono::system_clock::now(); const auto time_since_last_update = time_now - _last_update_time; @@ -187,3 +178,17 @@ void Game::_move_cursor(const Vector2 &direction) noexcept _cursor_controller->move_to(new_position); } + +void Game::_insert_cell(const Vector2 &position, char cell) noexcept +{ + std::cout.put(cell); + 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, cell); +} diff --git a/src/game/game.hpp b/src/game/game.hpp index 75fb165..0ffa1d6 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -41,4 +41,6 @@ private: int _gen_update_speed_millis; void _move_cursor(const Vector2 &direction) noexcept; + + void _insert_cell(const Vector2 &position, char cell) noexcept; }; diff --git a/src/interfaces/input.hpp b/src/interfaces/input.hpp index 0797c0d..c2ecefb 100644 --- a/src/interfaces/input.hpp +++ b/src/interfaces/input.hpp @@ -15,5 +15,7 @@ public: virtual bool is_key_pressed(Key key) noexcept = 0; + virtual Key get_currently_pressed_key() const noexcept = 0; + virtual void clear_currently_pressed() noexcept = 0; }; -- cgit v1.2.3-18-g5258