From c9297a82291882e4043ae5e0de2e631940e96b52 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 May 2022 19:08:02 +0200 Subject: feat: add incrementing generation --- src/game/game.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/game/game.cpp') diff --git a/src/game/game.cpp b/src/game/game.cpp index c41364a..de2b4df 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -16,7 +16,8 @@ Game::Game( _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)) + _user_input_observer(std::move(user_input_observer)), + _gen_update_speed_millis(1000) { } @@ -129,14 +130,33 @@ void Game::on_update() noexcept _status_manager->set_section_body(StatusLineSection::D, onoff ? "yes" : "no"); } - const auto time_since_last_update = - std::chrono::system_clock::now() - _last_update_time; + 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())); - _last_update_time = std::chrono::system_clock::now(); + const auto time_since_last_gen_update = + std::chrono::duration_cast( + time_now - _last_gen_update_time); + + if (!_generation_tracker->get_is_paused() && + time_since_last_gen_update.count() > _gen_update_speed_millis) + { + const auto new_current_gen = _generation_tracker->get_current_generation() + 1U; + + _generation_tracker->set_current_generation(new_current_gen); + + _status_manager->set_section_body( + StatusLineSection::E, + fmt::format("{}", new_current_gen)); + + _last_gen_update_time = time_now; + } + + _last_update_time = time_now; } void Game::on_exit() const noexcept -- cgit v1.2.3-18-g5258