From 9e6555c0f3be53d65496b442c4a60c2592fe4f97 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 12 Feb 2023 00:26:12 +0100 Subject: Extracted system update functions out of sim BaseBattle --- sim/src/BaseBattle.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'sim/src/BaseBattle.cpp') diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index e34af76..6182fec 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -25,7 +25,6 @@ namespace sim BaseBattle::BaseBattle(const campaign::Scenario& scenario) : - time {0.0}, _registry {}, spawner {scenario.last_team(), scenario.radius, 0.1} { @@ -57,17 +56,19 @@ BaseBattle::dispatcher() } +static void keep_at_range(engine::Context& ctx); +static void kill_off_dead(engine::Context& ctx); + + void BaseBattle::update(engine::Context& ctx) { - time = ctx.clock.game; pick_random_targets(); - keep_at_range(); + keep_at_range(ctx); FloatingMovement::update(ctx); TurretControl::update(ctx); - kill_off_dead(); - manager.clear(_registry); // registry supports on destructions events - manager.update(_dispatcher); + kill_off_dead(ctx); + manager.update(ctx); } @@ -83,13 +84,13 @@ BaseBattle::pick_random_targets() void -BaseBattle::keep_at_range() +keep_at_range(engine::Context& ctx) { - auto view = _registry.view(); + auto view = ctx.registry.view(); for (auto&& [entity, self, ai] : view.each()) { - if (!_registry.valid(ai.target)) + if (!ctx.registry.valid(ai.target)) continue; - const auto target = _registry.get(ai.target); + const auto target = ctx.registry.get(ai.target); const auto offset = target.position - self.position; ai.destination = target.position - offset.normalized().scale(ai.keep_at_range); } @@ -97,18 +98,18 @@ BaseBattle::keep_at_range() void -BaseBattle::kill_off_dead() +kill_off_dead(engine::Context& ctx) { - auto view = _registry.view(); + auto view = ctx.registry.view(); for (auto&& [entity, points] : view.each()) { if (points.is_alive()) continue; - if (_registry.all_of(entity)) { - const auto& [identifier, team] = _registry.get(entity); - _dispatcher.trigger(stats::ShipLeft{time, identifier, team.id, true}); - _dispatcher.trigger(Destroyed{entity}); + if (ctx.registry.all_of(entity)) { + const auto& [identifier, team] = ctx.registry.get(entity); + ctx.dispatcher.trigger(stats::ShipLeft{ctx.clock.game, identifier, team.id, true}); + ctx.dispatcher.trigger(Destroyed{entity}); } - _registry.destroy(entity); + ctx.registry.destroy(entity); } } -- cgit v1.1