From 632c367ae6885657e6c99b96b8bf7a96b61a2bb9 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 14 Feb 2023 00:13:02 +0100 Subject: Extracted basic simulation behaviour and that part of state to own class --- sim/src/BaseBattle.cpp | 34 ++++------------------------------ sim/src/BaseBattle.h | 6 +++--- sim/src/BaseSimulation.cpp | 37 +++++++++++++++++++++++++++++++++++++ sim/src/BaseSimulation.h | 26 ++++++++++++++++++++++++++ sim/src/sim.cpp | 15 +++++++++++++-- sim/src/systems.cpp | 13 ++++++++++++- sim/src/systems.h | 20 ++++++++++++++++++++ 7 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 sim/src/BaseSimulation.cpp create mode 100644 sim/src/BaseSimulation.h create mode 100644 sim/src/systems.h (limited to 'sim/src') diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 916a9f8..ee09740 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -1,21 +1,11 @@ #include "BaseBattle.h" -#include - #include #include #include #include #include -#include -#include -#include -#include -#include - -#include "Builder.h" -#include "RandomSpawner.h" namespace kurator @@ -24,10 +14,10 @@ namespace sim { -BaseBattle::BaseBattle(const campaign::Scenario& scenario) +BaseBattle::BaseBattle(const campaign::Scenario& scenario) : + state {load_scenario(scenario)}, + base {base_simulation_systems(state)} { - load_scenario(state, scenario); - manager.extend(state.registry); } @@ -48,23 +38,7 @@ BaseBattle::const_context() const void BaseBattle::update(engine::Context& ctx) { - pick_random_targets(); - keep_at_range(ctx); - FloatingMovement::update(ctx); - TurretControl::update(ctx); - kill_off_dead(ctx); - manager.update(ctx); -} - - -void -BaseBattle::pick_random_targets() -{ - auto view = state.registry.view(); - for (auto&& [entity, team, ai] : view.each()) { - if (!state.registry.valid(ai.target)) - ai.target = manager.random(team.id); - } + base(ctx); } diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index 6f59457..3e98658 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "TeamManager.h" @@ -17,14 +18,13 @@ namespace sim class BaseBattle : public Battle { public: - BaseBattle(const campaign::Scenario& scenario); + explicit BaseBattle(const campaign::Scenario& scenario); engine::Context context() override; engine::ConstContext const_context() const override; void update(engine::Context& ctx) override; private: State state; - TeamManager manager; - void pick_random_targets(); + System base; }; diff --git a/sim/src/BaseSimulation.cpp b/sim/src/BaseSimulation.cpp new file mode 100644 index 0000000..05b224d --- /dev/null +++ b/sim/src/BaseSimulation.cpp @@ -0,0 +1,37 @@ +#include "BaseSimulation.h" + +#include +#include +#include +#include + +#include "systems.h" +#include "TeamManager.h" + + +namespace kurator +{ +namespace sim +{ + + +BaseSimulation::BaseSimulation(State& ctx) +{ + manager.extend(ctx.registry); +} + + +void +BaseSimulation::operator()(engine::Context& ctx) +{ + pick_random_targets(ctx, manager); + keep_at_range(ctx); + FloatingMovement::update(ctx); + TurretControl::update(ctx); + kill_off_dead(ctx); + manager.update(ctx); +} + + +} // namespace sim +} // namespace kurator diff --git a/sim/src/BaseSimulation.h b/sim/src/BaseSimulation.h new file mode 100644 index 0000000..4d73382 --- /dev/null +++ b/sim/src/BaseSimulation.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "TeamManager.h" + + +namespace kurator +{ +namespace sim +{ + + +class BaseSimulation +{ +public: + explicit BaseSimulation(State& ctx); + void operator()(engine::Context& ctx); +private: + TeamManager manager; +}; + + +} // namespace sim +} // namespace kurator diff --git a/sim/src/sim.cpp b/sim/src/sim.cpp index f915d20..4cb7467 100644 --- a/sim/src/sim.cpp +++ b/sim/src/sim.cpp @@ -3,8 +3,10 @@ #include #include #include +#include #include +#include "BaseSimulation.h" #include "Builder.h" #include "RandomSpawner.h" @@ -15,9 +17,10 @@ namespace sim { -void -load_scenario(State& ctx, const campaign::Scenario& scenario) +State +load_scenario(const campaign::Scenario& scenario) { + State ctx {}; RandomSpawner spawner {scenario.last_team(), scenario.radius, 0.1}; Builder build {ctx.registry, spawner}; for (const auto& ship : scenario.ships) { @@ -29,6 +32,14 @@ load_scenario(State& ctx, const campaign::Scenario& scenario) state.keep_at_range = std::min(state.keep_at_range, turret_type.optimal_range); } } + return ctx; +} + + +System +base_simulation_systems(State& ctx) +{ + return BaseSimulation{ctx}; } diff --git a/sim/src/systems.cpp b/sim/src/systems.cpp index 114ed2f..4dc1e71 100644 --- a/sim/src/systems.cpp +++ b/sim/src/systems.cpp @@ -1,4 +1,4 @@ -#include +#include "systems.h" #include #include @@ -15,6 +15,17 @@ namespace sim void +pick_random_targets(engine::Context& ctx, TeamManager& manager) +{ + auto view = ctx.registry.view(); + for (auto&& [entity, team, ai] : view.each()) { + if (!ctx.registry.valid(ai.target)) + ai.target = manager.random(team.id); + } +} + + +void keep_at_range(engine::Context& ctx) { auto view = ctx.registry.view(); diff --git a/sim/src/systems.h b/sim/src/systems.h new file mode 100644 index 0000000..4c8838b --- /dev/null +++ b/sim/src/systems.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "TeamManager.h" + + +namespace kurator +{ +namespace sim +{ + + +void pick_random_targets(engine::Context& ctx, TeamManager& manager); +void keep_at_range(engine::Context& ctx); +void kill_off_dead(engine::Context& ctx); + + +} // namespace sim +} // namespace kurator -- cgit v1.1