diff options
author | Aki <please@ignore.pl> | 2023-02-10 23:47:39 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2023-02-10 23:47:39 +0100 |
commit | d6687be6a89ebd55fa9f3438d742bbf9cf4c9afa (patch) | |
tree | 943ec88bc6ef910af862328a82ff8251f6474b88 | |
parent | b6f4678d6466e2b16f32d4fd350b10af5720dd11 (diff) | |
download | kurator-d6687be6a89ebd55fa9f3438d742bbf9cf4c9afa.zip kurator-d6687be6a89ebd55fa9f3438d742bbf9cf4c9afa.tar.gz kurator-d6687be6a89ebd55fa9f3438d742bbf9cf4c9afa.tar.bz2 |
Created scaled Clock for consistent access to update times
-rw-r--r-- | engine/CMakeLists.txt | 1 | ||||
-rw-r--r-- | engine/include/kurator/engine/Clock.h | 20 | ||||
-rw-r--r-- | engine/src/Clock.cpp | 33 | ||||
-rw-r--r-- | kurator/src/Battle.cpp | 42 | ||||
-rw-r--r-- | kurator/src/Battle.h | 3 |
5 files changed, 77 insertions, 22 deletions
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 <kurator/engine/Clock.h> + +#include <raylib.h> + + +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> _session) : Battle::Battle(std::shared_ptr<Session> _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<sim::End>().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<Pause>(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<Timed>(); 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<PopMove, UIOffset>(); 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<Cross>(); 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<Line>(); 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 <kurator/campaign/Scenario.h> #include <kurator/engine/Camera.h> +#include <kurator/engine/Clock.h> #include <kurator/sim/Battle.h> #include <kurator/sim/events.h> #include <kurator/stats/EventLog.h> @@ -36,9 +37,9 @@ private: std::shared_ptr<Session> session; std::unique_ptr<sim::Battle> battle; engine::Camera camera; + engine::Clock clock; ForceBalance balance; stats::EventLog log; - float time_factor; Callback report; }; |