diff options
Diffstat (limited to 'sim/src')
-rw-r--r-- | sim/src/BaseBattle.cpp | 34 | ||||
-rw-r--r-- | sim/src/BaseBattle.h | 6 | ||||
-rw-r--r-- | sim/src/BaseSimulation.cpp | 37 | ||||
-rw-r--r-- | sim/src/BaseSimulation.h | 26 | ||||
-rw-r--r-- | sim/src/sim.cpp | 15 | ||||
-rw-r--r-- | sim/src/systems.cpp | 13 | ||||
-rw-r--r-- | sim/src/systems.h | 20 |
7 files changed, 115 insertions, 36 deletions
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 <algorithm> - #include <entt/entity/registry.hpp> #include <entt/signal/dispatcher.hpp> #include <kurator/engine/Context.h> #include <kurator/campaign/Scenario.h> #include <kurator/sim.h> -#include <kurator/sim/components.h> -#include <kurator/sim/FloatingMovement.h> -#include <kurator/sim/systems.h> -#include <kurator/sim/TurretControl.h> -#include <kurator/universe/UniqueIdentifier.h> - -#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<Team, AIState>(); - 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 <kurator/campaign/Scenario.h> #include <kurator/sim/Battle.h> #include <kurator/sim/State.h> +#include <kurator/sim/System.h> #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 <kurator/engine/Context.h> +#include <kurator/sim/State.h> +#include <kurator/sim/FloatingMovement.h> +#include <kurator/sim/TurretControl.h> + +#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 <kurator/engine/Context.h> +#include <kurator/sim/State.h> + +#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 <kurator/campaign/Scenario.h> #include <kurator/sim/components.h> #include <kurator/sim/State.h> +#include <kurator/sim/System.h> #include <kurator/universe/UniqueIdentifier.h> +#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 <kurator/sim/systems.h> +#include "systems.h" #include <kurator/engine/Context.h> #include <kurator/sim/components.h> @@ -15,6 +15,17 @@ namespace sim void +pick_random_targets(engine::Context& ctx, TeamManager& manager) +{ + auto view = ctx.registry.view<Team, AIState>(); + 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<Transform, AIState>(); 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 <kurator/engine/Context.h> + +#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 |