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/game/game.cpp | 83 +++++++++++++++++++++++++++++-------------------------- src/game/game.hpp | 2 ++ 2 files changed, 46 insertions(+), 39 deletions(-) (limited to 'src/game') 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; }; -- cgit v1.2.3-18-g5258