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 ++++++++++++++++++----------------- sim/src/BaseBattle.h | 3 --- sim/src/TeamManager.cpp | 14 ++++---------- sim/src/TeamManager.h | 6 +++--- 4 files changed, 25 insertions(+), 33 deletions(-) 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); } } diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index d67f33e..7532dd8 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -25,14 +25,11 @@ public: entt::dispatcher& dispatcher() override; void update(engine::Context& ctx) override; private: - double time; entt::registry _registry; entt::dispatcher _dispatcher; RandomSpawner spawner; TeamManager manager; void pick_random_targets(); - void keep_at_range(); - void kill_off_dead(); }; diff --git a/sim/src/TeamManager.cpp b/sim/src/TeamManager.cpp index d26ed67..0325c0c 100644 --- a/sim/src/TeamManager.cpp +++ b/sim/src/TeamManager.cpp @@ -6,8 +6,8 @@ #include #include -#include +#include #include @@ -58,21 +58,15 @@ TeamManager::random(const int exclude) void -TeamManager::clear(entt::registry& registry) +TeamManager::update(engine::Context& ctx) { for (auto& members : teams) { - auto is_valid = [®istry](entt::entity entity){ return !registry.valid(entity); }; + auto is_valid = [&](entt::entity entity){ return !ctx.registry.valid(entity); }; members.erase(std::remove_if(members.begin(), members.end(), is_valid), members.end()); } -} - - -void -TeamManager::update(entt::dispatcher& dispatcher) -{ const auto left = std::count_if(teams.begin(), teams.end(), [](const Team& team){ return team.size() > 0; }); if (left < 2) - dispatcher.trigger(End{}); + ctx.dispatcher.trigger(End{}); } diff --git a/sim/src/TeamManager.h b/sim/src/TeamManager.h index 502bed2..f7cf1f6 100644 --- a/sim/src/TeamManager.h +++ b/sim/src/TeamManager.h @@ -4,7 +4,8 @@ #include #include -#include + +#include namespace kurator @@ -21,8 +22,7 @@ public: void add(int team, entt::entity entity); Team get(int team) const; entt::entity random(int exclude); - void clear(entt::registry& registry); - void update(entt::dispatcher& dispatcher); + void update(engine::Context& ctx); private: std::vector teams; std::mt19937 generator; -- cgit v1.1