From e66011756340e03fe941723f762119ed78ec2402 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 11 Feb 2023 16:50:09 +0100 Subject: Added ConstContext for drawing systems This whole ongoing refactoring process is meant to better understand relationships between Battle, Simulation and Systems. --- engine/include/kurator/engine/Context.h | 23 +++++++++++- engine/src/Context.cpp | 25 ++++++++++++- kurator/src/Battle.cpp | 65 +++++++++++++++++++++++++-------- 3 files changed, 95 insertions(+), 18 deletions(-) diff --git a/engine/include/kurator/engine/Context.h b/engine/include/kurator/engine/Context.h index c2c7cca..093285e 100644 --- a/engine/include/kurator/engine/Context.h +++ b/engine/include/kurator/engine/Context.h @@ -3,6 +3,7 @@ #include #include +#include "Camera.h" #include "Clock.h" @@ -12,12 +13,32 @@ namespace engine { +struct Context; +struct ConstContext; + + struct Context { - Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_); + Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_, Camera& camera_); entt::registry& registry; entt::dispatcher& dispatcher; Clock& clock; + Camera& camera; + operator ConstContext() const; +}; + + +struct ConstContext +{ + ConstContext( + const entt::registry& registry_, + const entt::dispatcher& dispatcher_, + const Clock& clock_, + const Camera& camera_); + const entt::registry& registry; + const entt::dispatcher& dispatcher; + const Clock& clock; + const Camera& camera; }; diff --git a/engine/src/Context.cpp b/engine/src/Context.cpp index eeec938..8c81c89 100644 --- a/engine/src/Context.cpp +++ b/engine/src/Context.cpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -12,10 +13,30 @@ namespace engine { -Context::Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_) : +Context::Context(entt::registry& registry_, entt::dispatcher& dispatcher_, Clock& clock_, Camera& camera_) : registry {registry_}, dispatcher {dispatcher_}, - clock {clock_} + clock {clock_}, + camera {camera_} +{ +} + + +Context::operator ConstContext() const +{ + return ConstContext{registry, dispatcher, clock, camera}; +} + + +ConstContext::ConstContext( + const entt::registry& registry_, + const entt::dispatcher& dispatcher_, + const Clock& clock_, + const Camera& camera_) : + registry {registry_}, + dispatcher {dispatcher_}, + clock {clock_}, + camera {camera_} { } diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 7363661..b47c539 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -103,7 +103,7 @@ Battle::update() camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); // won't work in frame auto& registry = battle->registry(); clock.update(); - engine::Context ctx {registry, battle->dispatcher(), clock}; + engine::Context ctx {registry, battle->dispatcher(), clock, camera}; battle->update(clock.dt); progress_timers(ctx); move_ui_pops(ctx); @@ -167,17 +167,34 @@ animate_lines(engine::Context& ctx) } +static void draw_crosses(engine::ConstContext& ctx); +static void draw_lines(engine::ConstContext& ctx); +static void draw_markers(engine::ConstContext& ctx); +static void draw_pops(engine::ConstContext& ctx); + + void Battle::draw() const { ClearBackground(BLACK); Grid().draw(camera); - auto& registry = battle->registry(); - auto crosses = registry.view(); + engine::ConstContext ctx {battle->registry(), battle->dispatcher(), clock, camera}; + draw_crosses(ctx); + draw_lines(ctx); + draw_markers(ctx); + draw_pops(ctx); + balance.draw(); +} + + +void +draw_crosses(engine::ConstContext& ctx) +{ + auto crosses = ctx.registry.view(); for (const auto& [entity, transform, cross] : crosses.each()) { if (cross.timer > cross.phase) continue; - const auto pos = camera.to_screen(transform.position); + const auto pos = ctx.camera.to_screen(transform.position); DrawLine( pos.x - cross.hlength, pos.y - cross.hlength, @@ -191,23 +208,35 @@ Battle::draw() const pos.y + cross.hlength, cross.color); } - auto lines = registry.view(); +} + + +void +draw_lines(engine::ConstContext& ctx) +{ + auto lines = ctx.registry.view(); for (const auto& [entity, line] : lines.each()) { const auto diff = line.end - line.start; const auto fstart = line.position - line.hlength; const auto fend = line.position + line.hlength; - const auto start = camera.to_screen(line.start + diff.scale(fstart > 0.0 ? fstart : 0.0)); - const auto end = camera.to_screen(line.start + diff.scale(fend > 1.0 ? 1.0 : fend)); + const auto start = ctx.camera.to_screen(line.start + diff.scale(fstart > 0.0 ? fstart : 0.0)); + const auto end = ctx.camera.to_screen(line.start + diff.scale(fend > 1.0 ? 1.0 : fend)); DrawLine(start.x, start.y, end.x, end.y, line.color); } - auto view = registry.view(); +} + + +void +draw_markers(engine::ConstContext& ctx) +{ + auto view = ctx.registry.view(); for (auto [entity, marker, transform] : view.each()) { - const auto pos = camera.to_screen(transform.position); - if (registry.all_of(entity)) { - const auto& movement = registry.get(entity); + const auto pos = ctx.camera.to_screen(transform.position); + if (ctx.registry.all_of(entity)) { + const auto& movement = ctx.registry.get(entity); const auto& velocity = movement.speed; const auto edge = pos + velocity.normalized().scale(marker.radius); - const auto tip = edge + velocity.scale(2.0 * camera.scale); + const auto tip = edge + velocity.scale(2.0 * ctx.camera.scale); DrawLine(edge.x, edge.y, tip.x, tip.y, DARKGRAY); } const engine::Point direction {std::cos(transform.angle), std::sin(transform.angle)}; @@ -216,12 +245,18 @@ Battle::draw() const DrawLine(pos.x, pos.y, edge.x, edge.y, WHITE); DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, GRAY); } - auto pops = registry.view(); +} + + +void +draw_pops(engine::ConstContext& ctx) +{ + auto pops = ctx.registry.view(); for (const auto& [entity, text, transform, offset] : pops.each()) { - const auto pos = camera.to_screen(transform.position).subtract(text.width/2, text.font_size/2) + offset; + const auto screen = ctx.camera.to_screen(transform.position); + const auto pos = screen.subtract(text.width/2, text.font_size/2) + offset; DrawText(text.text.c_str(), pos.x, pos.y, text.font_size, text.color); } - balance.draw(); } -- cgit v1.1