From d6687be6a89ebd55fa9f3438d742bbf9cf4c9afa Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 10 Feb 2023 23:47:39 +0100 Subject: Created scaled Clock for consistent access to update times --- engine/CMakeLists.txt | 1 + engine/include/kurator/engine/Clock.h | 20 +++++++++++++++++ engine/src/Clock.cpp | 33 +++++++++++++++++++++++++++ kurator/src/Battle.cpp | 42 +++++++++++++++++------------------ kurator/src/Battle.h | 3 ++- 5 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 engine/include/kurator/engine/Clock.h create mode 100644 engine/src/Clock.cpp diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 1ffbd98..7cb7f20 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -2,6 +2,7 @@ project(engine) add_library( ${PROJECT_NAME} STATIC src/Camera.cpp + src/Clock.cpp src/Point.cpp ) target_include_directories( diff --git a/engine/include/kurator/engine/Clock.h b/engine/include/kurator/engine/Clock.h new file mode 100644 index 0000000..4e5e7b1 --- /dev/null +++ b/engine/include/kurator/engine/Clock.h @@ -0,0 +1,20 @@ +#pragma once + + +namespace kurator +{ +namespace engine +{ + + +struct Clock +{ + float time_factor = 1.0f; + float dt() const; + float ui() const; + operator float() const; +}; + + +} // namespace engine +} // namespace kurator diff --git a/engine/src/Clock.cpp b/engine/src/Clock.cpp new file mode 100644 index 0000000..5261603 --- /dev/null +++ b/engine/src/Clock.cpp @@ -0,0 +1,33 @@ +#include + +#include + + +namespace kurator +{ +namespace engine +{ + + +float +Clock::dt() const +{ + return GetFrameTime() * time_factor; +} + + +float +Clock::ui() const +{ + return GetFrameTime(); +} + + +Clock::operator float() const +{ + return dt(); +} + + +} // namespace engine +} // namespace kurator diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 7a34841..c7bbdc7 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -42,7 +42,7 @@ Battle::Battle(std::shared_ptr _session) : Battle::Battle(std::shared_ptr _session, campaign::Scenario scenario, Battle::Callback _report) : session {std::move(_session)}, battle {sim::prepare(scenario)}, - time_factor {1.0}, + clock {}, report {std::move(_report)} { battle->dispatcher().sink().connect<&Battle::on_end>(*this); @@ -83,14 +83,14 @@ time_controls(const char* id, float& time_factor) } -static void progress_timers(entt::registry& registry, float dt, float time_factor); -static void move_ui_pops(entt::registry& registry, float dt); -static void blink_crosses(entt::registry& registry, float dt); -static void animate_lines(entt::registry& registry, float dt, float time_factor); +static void progress_timers(entt::registry& registry, engine::Clock& clock); +static void move_ui_pops(entt::registry& registry, engine::Clock& clock); +static void blink_crosses(entt::registry& registry, engine::Clock& clock); +static void animate_lines(entt::registry& registry, engine::Clock& clock); void -Battle::update(const float dt) +Battle::update(const float) { if (IsKeyPressed(KEY_ESCAPE)) return session->set(std::make_shared(session, session->current())); @@ -101,27 +101,27 @@ Battle::update(const float dt) } if (IsWindowResized()) camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); // won't work in frame - battle->update(dt * time_factor); auto& registry = battle->registry(); - progress_timers(registry, dt, time_factor); // clock? - move_ui_pops(registry, dt); - blink_crosses(registry, dt); - animate_lines(registry, dt, time_factor); + battle->update(clock.dt()); + progress_timers(registry, clock); + move_ui_pops(registry, clock); + blink_crosses(registry, clock); + animate_lines(registry, clock); balance.update(registry); ImGui::SetNextWindowPos({GetScreenWidth()/2.0f, GetScreenHeight()-100.0f}, ImGuiCond_Once, {0.5f, 0.5f}); ImGui::SetNextWindowSize({240.0f, 0.0f}, ImGuiCond_Once); if (ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoFocusOnAppearing)) - time_controls("TimeControls", time_factor); + time_controls("TimeControls", clock.time_factor); ImGui::End(); } void -progress_timers(entt::registry& registry, const float dt, const float time_factor) +progress_timers(entt::registry& registry, engine::Clock& clock) { auto timers = registry.view(); for (auto&& [entity, timer] : timers.each()) { - timer.left -= timer.scaled ? dt * time_factor : dt; + timer.left -= timer.scaled ? clock.dt() : clock.ui(); if (timer.left < 0.0) registry.destroy(entity); } @@ -129,14 +129,14 @@ progress_timers(entt::registry& registry, const float dt, const float time_facto void -move_ui_pops(entt::registry& registry, const float dt) +move_ui_pops(entt::registry& registry, engine::Clock& clock) { auto pops = registry.view(); for (auto&& [entity, pop, offset] : pops.each()) { - const auto speed = pop.speed.scale(dt); + const auto speed = pop.speed.scale(clock.ui()); offset.x += speed.x; offset.y += speed.y; - const auto damp = pop.speed.scale(pop.damp).scale(dt); + const auto damp = pop.speed.scale(pop.damp).scale(clock.ui()); pop.speed.x -= damp.x; pop.speed.y -= damp.y; } @@ -144,11 +144,11 @@ move_ui_pops(entt::registry& registry, const float dt) void -blink_crosses(entt::registry& registry, const float dt) +blink_crosses(entt::registry& registry, engine::Clock& clock) { auto crosses = registry.view(); for (auto&& [entity, cross] : crosses.each()) { - cross.timer += dt; + cross.timer += clock.ui(); const auto dphase = cross.phase * 2; if (cross.timer > dphase) cross.timer -= dphase; @@ -157,11 +157,11 @@ blink_crosses(entt::registry& registry, const float dt) void -animate_lines(entt::registry& registry, const float dt, const float time_factor) +animate_lines(entt::registry& registry, engine::Clock& clock) { auto lines = registry.view(); for (auto&& [entity, line] : lines.each()) - line.position += (1.0 + line.hlength) / line.duration * dt * time_factor; + line.position += (1.0 + line.hlength) / line.duration * clock.dt(); } diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index c25f6b8..8812164 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -36,9 +37,9 @@ private: std::shared_ptr session; std::unique_ptr battle; engine::Camera camera; + engine::Clock clock; ForceBalance balance; stats::EventLog log; - float time_factor; Callback report; }; -- cgit v1.1