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 ++++++++++++++++++++++++---- src/game/game.hpp | 7 ++++++- src/game/generation_tracker.cpp | 5 +++++ src/game/generation_tracker.hpp | 2 ++ src/interfaces/generation_tracker.hpp | 2 ++ 5 files changed, 39 insertions(+), 5 deletions(-) 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 diff --git a/src/game/game.hpp b/src/game/game.hpp index 955269c..75fb165 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -33,7 +33,12 @@ private: std::shared_ptr _status_manager; std::shared_ptr _user_input_observer; - std::chrono::system_clock::time_point _last_update_time; + using TimePoint = std::chrono::system_clock::time_point; + + TimePoint _last_update_time; + TimePoint _last_gen_update_time; + + int _gen_update_speed_millis; void _move_cursor(const Vector2 &direction) noexcept; }; diff --git a/src/game/generation_tracker.cpp b/src/game/generation_tracker.cpp index 0a7a923..15b4ce4 100644 --- a/src/game/generation_tracker.cpp +++ b/src/game/generation_tracker.cpp @@ -7,6 +7,11 @@ auto GenerationTracker::get_current_generation() const noexcept -> uint32_t return _current_generation; } +void GenerationTracker::set_current_generation(uint32_t current_generation) noexcept +{ + _current_generation = current_generation; +} + auto GenerationTracker::get_is_paused() const noexcept -> bool { return _is_paused; diff --git a/src/game/generation_tracker.hpp b/src/game/generation_tracker.hpp index bd15a3e..1a83d71 100644 --- a/src/game/generation_tracker.hpp +++ b/src/game/generation_tracker.hpp @@ -11,6 +11,8 @@ public: [[nodiscard]] auto get_current_generation() const noexcept -> uint32_t override; + void set_current_generation(uint32_t current_generation) noexcept override; + [[nodiscard]] auto get_is_paused() const noexcept -> bool override; void set_is_paused(bool is_paused) noexcept override; diff --git a/src/interfaces/generation_tracker.hpp b/src/interfaces/generation_tracker.hpp index ca2e36b..c3f6a7e 100644 --- a/src/interfaces/generation_tracker.hpp +++ b/src/interfaces/generation_tracker.hpp @@ -13,6 +13,8 @@ public: [[nodiscard]] virtual auto get_current_generation() const noexcept -> uint32_t = 0; + virtual void set_current_generation(uint32_t current_generation) noexcept = 0; + [[nodiscard]] virtual auto get_is_paused() const noexcept -> bool = 0; virtual void set_is_paused(bool is_paused) noexcept = 0; -- cgit v1.2.3-18-g5258