From a5266d3e5da1d9ee9f873124674c68843a407ac0 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 11 Feb 2023 17:26:49 +0100 Subject: Context is now used in sim systems --- sim/src/BaseBattle.cpp | 9 +++++---- sim/src/BaseBattle.h | 8 ++------ sim/src/BaseBattle.inl.h | 19 ------------------- sim/src/FloatingMovement.cpp | 14 ++++++-------- sim/src/TurretControl.cpp | 26 ++++++++++++-------------- 5 files changed, 25 insertions(+), 51 deletions(-) delete mode 100644 sim/src/BaseBattle.inl.h (limited to 'sim/src') diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 3e3815c..e34af76 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -57,13 +58,13 @@ BaseBattle::dispatcher() void -BaseBattle::update(const float dt) +BaseBattle::update(engine::Context& ctx) { - time += dt; + time = ctx.clock.game; pick_random_targets(); keep_at_range(); - update(dt); - update(dt); + FloatingMovement::update(ctx); + TurretControl::update(ctx); kill_off_dead(); manager.clear(_registry); // registry supports on destructions events manager.update(_dispatcher); diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index b20b518..d67f33e 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -22,9 +23,8 @@ public: BaseBattle(const campaign::Scenario& scenario); entt::registry& registry() override; entt::dispatcher& dispatcher() override; - void update(float dt) override; + void update(engine::Context& ctx) override; private: - template void update(float dt); double time; entt::registry _registry; entt::dispatcher _dispatcher; @@ -32,13 +32,9 @@ private: TeamManager manager; void pick_random_targets(); void keep_at_range(); - void turrets(float dt); void kill_off_dead(); }; } // namespace sim } // namespace kurator - - -#include "BaseBattle.inl.h" diff --git a/sim/src/BaseBattle.inl.h b/sim/src/BaseBattle.inl.h deleted file mode 100644 index 8014cb3..0000000 --- a/sim/src/BaseBattle.inl.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - - -namespace kurator -{ -namespace sim -{ - - -template -void -BaseBattle::update(const float dt) -{ - System::update(_registry, _dispatcher, dt); -} - - -} // namespace sim -} // namespace kurator diff --git a/sim/src/FloatingMovement.cpp b/sim/src/FloatingMovement.cpp index e1794d5..da6cd20 100644 --- a/sim/src/FloatingMovement.cpp +++ b/sim/src/FloatingMovement.cpp @@ -1,8 +1,6 @@ #include -#include -#include - +#include #include @@ -13,21 +11,21 @@ namespace sim void -FloatingMovement::update(entt::registry& registry, entt::dispatcher&, const float dt) +FloatingMovement::update(engine::Context& ctx) { - auto view = registry.view(); + auto view = ctx.registry.view(); for (auto&& [entity, transform, movement, ai] : view.each()) { const auto offset = ai.destination - transform.position; const auto at_destination = offset.magnitude() > movement.destination_boundary; const auto acceleration = at_destination ? - offset.normalized().scale(movement.acceleration * dt) : - offset.normalized().scale(-1 * movement.deceleration * dt); + offset.normalized().scale(movement.acceleration * ctx.clock.dt) : + offset.normalized().scale(-1 * movement.deceleration * ctx.clock.dt); movement.speed.x += acceleration.x; movement.speed.y += acceleration.y; if (movement.speed.magnitude() > movement.max_speed) movement.speed = movement.speed.normalized().scale(movement.max_speed); - const auto speed = movement.speed.scale(dt); + const auto speed = movement.speed.scale(ctx.clock.dt); transform.position.x += speed.x; transform.position.y += speed.y; transform.angle = speed.angle(); diff --git a/sim/src/TurretControl.cpp b/sim/src/TurretControl.cpp index bfac27f..fa42bf5 100644 --- a/sim/src/TurretControl.cpp +++ b/sim/src/TurretControl.cpp @@ -1,8 +1,6 @@ #include -#include -#include - +#include #include #include #include @@ -19,33 +17,33 @@ bool consume(float& dt, double& target); void -TurretControl::update(entt::registry& registry, entt::dispatcher& dispatcher, const float dt) +TurretControl::update(engine::Context& ctx) { - auto view = registry.view(); + auto view = ctx.registry.view(); for (auto&& [entity, control, def] : view.each()) { - if (!registry.valid(control.owner)) { - registry.destroy(entity); + if (!ctx.registry.valid(control.owner)) { + ctx.registry.destroy(entity); continue; } - if (!registry.all_of(control.owner)) + if (!ctx.registry.all_of(control.owner)) continue; - const auto& [state, transform] = registry.get(control.owner); - if (!registry.valid(state.target)) + const auto& [state, transform] = ctx.registry.get(control.owner); + if (!ctx.registry.valid(state.target)) continue; - const auto& target = registry.get(state.target); + const auto& target = ctx.registry.get(state.target); const auto distance = transform.position.distance(target.position); if (distance > def.effective_range()) continue; - auto remaining_dt = dt; + auto remaining_dt = ctx.clock.dt; while (remaining_dt > 0.0) { if (control.rounds < 1 && consume(remaining_dt, control.reload)) control.rounds = def.rounds; if (control.rounds > 0 && consume(remaining_dt, control.delay)) { - auto& target_points = registry.get(state.target); + auto& target_points = ctx.registry.get(state.target); auto damage = def.effective_damage(distance); if (damage > 0.0) { damage = target_points.deal(damage); - dispatcher.trigger(Hit{damage, control.owner, state.target}); + ctx.dispatcher.trigger(Hit{damage, control.owner, state.target}); } control.delay = def.rate_of_fire; if (--control.rounds < 1) -- cgit v1.1