aboutsummaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-03 12:02:11 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:00 +0200
commitdf49c32fc0792214182d510b4a58c524bf8b59c5 (patch)
treefb626b5bdb359bca08a85d8d8fefb1679b4e61ab /src/game
parent4f27cc4f86fb7265bc6ad78cdfbe5d858a92a56e (diff)
feat: add ability to change generation speed
Diffstat (limited to 'src/game')
-rw-r--r--src/game/game.cpp27
-rw-r--r--src/game/game.hpp7
-rw-r--r--src/game/status_manager.cpp2
3 files changed, 26 insertions, 10 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 8ea6571..9d8c246 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -34,7 +34,9 @@ void Game::on_start() noexcept
_status_manager->set_section_title(StatusLineSection::C, "Y: ");
_status_manager->set_section_title(StatusLineSection::D, "Paused: ");
_status_manager->set_section_title(StatusLineSection::E, "Generation: ");
- _status_manager->set_section_title(StatusLineSection::F, "Time since last frame: ");
+ _status_manager->set_section_title(
+ StatusLineSection::F,
+ "Minimum time since last generation: ");
_status_manager->set_section_title(StatusLineSection::G, "Living cells: ");
_status_manager->set_section_title(StatusLineSection::H, "Window size: ");
@@ -150,6 +152,17 @@ void Game::on_update() noexcept
is_generation_stepping = true;
break;
+ case '+':
+ if (_min_time_since_last_gen_millis > 0)
+ {
+ _min_time_since_last_gen_millis -= MIN_TIME_SINCE_LAST_GEN_INCREMENT;
+ }
+ break;
+
+ case '-':
+ _min_time_since_last_gen_millis += MIN_TIME_SINCE_LAST_GEN_DECREMENT;
+ break;
+
default:
break;
}
@@ -167,29 +180,27 @@ void Game::on_update() noexcept
fmt::format("{}", current_pos.get_y()));
}
- const auto time_now = std::chrono::system_clock::now();
-
- const auto time_since_last_update = time_now - _last_update_time;
-
_status_manager->set_section_body(
StatusLineSection::F,
- fmt::format("{} nanoseconds", time_since_last_update.count()));
+ fmt::format("{} milliseconds", _min_time_since_last_gen_millis));
_status_manager->set_section_body(
StatusLineSection::G,
fmt::format("{}", _living_cell_positions.size()));
+ const auto time_now = std::chrono::system_clock::now();
+
if (_generation_tracker->get_is_paused() && !is_generation_stepping)
{
_last_update_time = time_now;
return;
}
- const auto time_since_last_gen_update =
+ const auto time_since_last_gen =
std::chrono::duration_cast<std::chrono::milliseconds>(
time_now - _last_gen_update_time);
- if (time_since_last_gen_update.count() <= GENERATION_UPDATE_SPEED_MILLIS)
+ if (time_since_last_gen.count() <= _min_time_since_last_gen_millis)
{
_last_update_time = time_now;
return;
diff --git a/src/game/game.hpp b/src/game/game.hpp
index 7e43c7c..b5b69cf 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -16,7 +16,10 @@
#include <list>
#include <memory>
-constexpr auto GENERATION_UPDATE_SPEED_MILLIS = 200;
+constexpr auto DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS = 200;
+
+constexpr auto MIN_TIME_SINCE_LAST_GEN_INCREMENT = 50;
+constexpr auto MIN_TIME_SINCE_LAST_GEN_DECREMENT = 50;
class Game : public IGame
{
@@ -48,6 +51,8 @@ private:
TimePoint _last_update_time;
TimePoint _last_gen_update_time;
+ int32_t _min_time_since_last_gen_millis = DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS;
+
std::list<Vector2> _living_cell_positions;
void _move_cursor(const Vector2 &direction) noexcept;
diff --git a/src/game/status_manager.cpp b/src/game/status_manager.cpp
index 4c189b9..3f2df01 100644
--- a/src/game/status_manager.cpp
+++ b/src/game/status_manager.cpp
@@ -17,7 +17,7 @@ void StatusManager::initialize() noexcept
_statusline->set_section_length(StatusLineSection::C, 15U);
_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::F, 60U);
_statusline->set_section_length(StatusLineSection::G, 30U);
_statusline->set_section_length(StatusLineSection::H, 30U);