From d0629836f9aa002a872d59c7f9e465dca9f2cc02 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 11 Feb 2023 00:25:15 +0100 Subject: Added Context to streamline system update call interface --- engine/CMakeLists.txt | 1 + engine/include/kurator/engine/Context.h | 25 ++++++++++++++++++ engine/src/Context.cpp | 24 +++++++++++++++++ kurator/src/Battle.cpp | 46 +++++++++++++++++---------------- kurator/src/Battle.h | 1 + 5 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 engine/include/kurator/engine/Context.h create mode 100644 engine/src/Context.cpp diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 7cb7f20..43c09a5 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -3,6 +3,7 @@ add_library( ${PROJECT_NAME} STATIC src/Camera.cpp src/Clock.cpp + src/Context.cpp src/Point.cpp ) target_include_directories( diff --git a/engine/include/kurator/engine/Context.h b/engine/include/kurator/engine/Context.h new file mode 100644 index 0000000..c2c7cca --- /dev/null +++ b/engine/include/kurator/engine/Context.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "Clock.h" + + +namespace kurator +{ +namespace engine +{ + + +struct Context +{ + Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_); + entt::registry& registry; + entt::dispatcher& dispatcher; + Clock& clock; +}; + + +} // namespace engine +} // namespace kurator diff --git a/engine/src/Context.cpp b/engine/src/Context.cpp new file mode 100644 index 0000000..eeec938 --- /dev/null +++ b/engine/src/Context.cpp @@ -0,0 +1,24 @@ +#include + +#include +#include + +#include + + +namespace kurator +{ +namespace engine +{ + + +Context::Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_) : + registry {registry_}, + dispatcher {dispatcher_}, + clock {clock_} +{ +} + + +} // namespace engine +} // namespace kurator diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index c7bbdc7..8ea3e11 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -83,10 +84,10 @@ time_controls(const char* id, 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); +static void progress_timers(engine::Context& ctx); +static void move_ui_pops(engine::Context& ctx); +static void blink_crosses(engine::Context& ctx); +static void animate_lines(engine::Context& ctx); void @@ -102,11 +103,12 @@ Battle::update(const float) if (IsWindowResized()) camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); // won't work in frame auto& registry = battle->registry(); + engine::Context ctx {registry, battle->dispatcher(), clock}; battle->update(clock.dt()); - progress_timers(registry, clock); - move_ui_pops(registry, clock); - blink_crosses(registry, clock); - animate_lines(registry, clock); + progress_timers(ctx); + move_ui_pops(ctx); + blink_crosses(ctx); + animate_lines(ctx); 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); @@ -117,26 +119,26 @@ Battle::update(const float) void -progress_timers(entt::registry& registry, engine::Clock& clock) +progress_timers(engine::Context& ctx) { - auto timers = registry.view(); + auto timers = ctx.registry.view(); for (auto&& [entity, timer] : timers.each()) { - timer.left -= timer.scaled ? clock.dt() : clock.ui(); + timer.left -= timer.scaled ? ctx.clock.dt() : ctx.clock.ui(); if (timer.left < 0.0) - registry.destroy(entity); + ctx.registry.destroy(entity); } } void -move_ui_pops(entt::registry& registry, engine::Clock& clock) +move_ui_pops(engine::Context& ctx) { - auto pops = registry.view(); + auto pops = ctx.registry.view(); for (auto&& [entity, pop, offset] : pops.each()) { - const auto speed = pop.speed.scale(clock.ui()); + const auto speed = pop.speed.scale(ctx.clock.ui()); offset.x += speed.x; offset.y += speed.y; - const auto damp = pop.speed.scale(pop.damp).scale(clock.ui()); + const auto damp = pop.speed.scale(pop.damp).scale(ctx.clock.ui()); pop.speed.x -= damp.x; pop.speed.y -= damp.y; } @@ -144,11 +146,11 @@ move_ui_pops(entt::registry& registry, engine::Clock& clock) void -blink_crosses(entt::registry& registry, engine::Clock& clock) +blink_crosses(engine::Context& ctx) { - auto crosses = registry.view(); + auto crosses = ctx.registry.view(); for (auto&& [entity, cross] : crosses.each()) { - cross.timer += clock.ui(); + cross.timer += ctx.clock.ui(); const auto dphase = cross.phase * 2; if (cross.timer > dphase) cross.timer -= dphase; @@ -157,11 +159,11 @@ blink_crosses(entt::registry& registry, engine::Clock& clock) void -animate_lines(entt::registry& registry, engine::Clock& clock) +animate_lines(engine::Context& ctx) { - auto lines = registry.view(); + auto lines = ctx.registry.view(); for (auto&& [entity, line] : lines.each()) - line.position += (1.0 + line.hlength) / line.duration * clock.dt(); + line.position += (1.0 + line.hlength) / line.duration * ctx.clock.dt(); } diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 8812164..b928855 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include -- cgit v1.1