From 03bb1614c25a56ef6225db09c4c59b7a5f8fa808 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 13 Feb 2023 01:06:31 +0100 Subject: Externalized part of simulation systems --- sim/CMakeLists.txt | 1 + sim/include/kurator/sim/systems.h | 17 ++++++++++++++ sim/src/BaseBattle.cpp | 39 +------------------------------ sim/src/systems.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 sim/include/kurator/sim/systems.h create mode 100644 sim/src/systems.cpp diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index 9d24182..27f312b 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -7,6 +7,7 @@ add_library( src/FloatingMovement.cpp src/HitPoints.cpp src/RandomSpawner.cpp + src/systems.cpp src/TeamManager.cpp src/TurretControl.cpp ) diff --git a/sim/include/kurator/sim/systems.h b/sim/include/kurator/sim/systems.h new file mode 100644 index 0000000..76353c2 --- /dev/null +++ b/sim/include/kurator/sim/systems.h @@ -0,0 +1,17 @@ +#pragma once + +#include + + +namespace kurator +{ +namespace sim +{ + + +void keep_at_range(engine::Context& ctx); +void kill_off_dead(engine::Context& ctx); + + +} // namespace sim +} // namespace kurator diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 0c23065..888379e 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -8,11 +8,9 @@ #include #include #include -#include #include -#include +#include #include -#include #include #include "Builder.h" @@ -67,10 +65,6 @@ BaseBattle::dispatcher() } -static void keep_at_range(engine::Context& ctx); -static void kill_off_dead(engine::Context& ctx); - - void BaseBattle::update(engine::Context& ctx) { @@ -94,36 +88,5 @@ BaseBattle::pick_random_targets() } -void -keep_at_range(engine::Context& ctx) -{ - auto view = ctx.registry.view(); - for (auto&& [entity, self, ai] : view.each()) { - if (!ctx.registry.valid(ai.target)) - continue; - 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); - } -} - - -void -kill_off_dead(engine::Context& ctx) -{ - auto view = ctx.registry.view(); - for (auto&& [entity, points] : view.each()) { - if (points.is_alive()) - continue; - 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}); - } - ctx.registry.destroy(entity); - } -} - - } // namespace sim } // namespace kurator diff --git a/sim/src/systems.cpp b/sim/src/systems.cpp new file mode 100644 index 0000000..114ed2f --- /dev/null +++ b/sim/src/systems.cpp @@ -0,0 +1,49 @@ +#include + +#include +#include +#include +#include +#include +#include + + +namespace kurator +{ +namespace sim +{ + + +void +keep_at_range(engine::Context& ctx) +{ + auto view = ctx.registry.view(); + for (auto&& [entity, self, ai] : view.each()) { + if (!ctx.registry.valid(ai.target)) + continue; + 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); + } +} + + +void +kill_off_dead(engine::Context& ctx) +{ + auto view = ctx.registry.view(); + for (auto&& [entity, points] : view.each()) { + if (points.is_alive()) + continue; + 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}); + } + ctx.registry.destroy(entity); + } +} + + +} // namespace sim +} // namespace kurator -- cgit v1.1