summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-02-14 00:13:02 +0100
committerAki <please@ignore.pl>2023-02-14 00:28:48 +0100
commit632c367ae6885657e6c99b96b8bf7a96b61a2bb9 (patch)
tree3b6210086d2c082533288d06f2843b9150e747dd
parent2a9f378c66b28cef1c5ee063cf4d7e4e2889076e (diff)
downloadkurator-632c367ae6885657e6c99b96b8bf7a96b61a2bb9.zip
kurator-632c367ae6885657e6c99b96b8bf7a96b61a2bb9.tar.gz
kurator-632c367ae6885657e6c99b96b8bf7a96b61a2bb9.tar.bz2
Extracted basic simulation behaviour and that part of state to own class
-rw-r--r--sim/CMakeLists.txt1
-rw-r--r--sim/include/kurator/sim.h4
-rw-r--r--sim/include/kurator/sim/System.h18
-rw-r--r--sim/src/BaseBattle.cpp34
-rw-r--r--sim/src/BaseBattle.h6
-rw-r--r--sim/src/BaseSimulation.cpp37
-rw-r--r--sim/src/BaseSimulation.h26
-rw-r--r--sim/src/sim.cpp15
-rw-r--r--sim/src/systems.cpp13
-rw-r--r--sim/src/systems.h (renamed from sim/include/kurator/sim/systems.h)3
10 files changed, 120 insertions, 37 deletions
diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt
index 55dcc60..a221d2f 100644
--- a/sim/CMakeLists.txt
+++ b/sim/CMakeLists.txt
@@ -2,6 +2,7 @@ project(sim)
add_library(
${PROJECT_NAME} STATIC
src/BaseBattle.cpp
+ src/BaseSimulation.cpp
src/Battle.cpp
src/Builder.cpp
src/FloatingMovement.cpp
diff --git a/sim/include/kurator/sim.h b/sim/include/kurator/sim.h
index 3c4364c..3330c48 100644
--- a/sim/include/kurator/sim.h
+++ b/sim/include/kurator/sim.h
@@ -3,6 +3,7 @@
#include <kurator/campaign/Scenario.h>
#include "sim/State.h"
+#include "sim/System.h"
namespace kurator
@@ -11,7 +12,8 @@ namespace sim
{
-void load_scenario(State& ctx, const campaign::Scenario& scenario);
+auto load_scenario(const campaign::Scenario& scenario) -> State;
+auto base_simulation_systems(State& ctx) -> System;
} // namespace sim
diff --git a/sim/include/kurator/sim/System.h b/sim/include/kurator/sim/System.h
new file mode 100644
index 0000000..101812a
--- /dev/null
+++ b/sim/include/kurator/sim/System.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <functional>
+
+#include <kurator/engine/Context.h>
+
+
+namespace kurator
+{
+namespace sim
+{
+
+
+using System = std::function<void(engine::Context&)>;
+
+
+} // namespace sim
+} // namespace kurator
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/include/kurator/sim/systems.h b/sim/src/systems.h
index 76353c2..4c8838b 100644
--- a/sim/include/kurator/sim/systems.h
+++ b/sim/src/systems.h
@@ -2,6 +2,8 @@
#include <kurator/engine/Context.h>
+#include "TeamManager.h"
+
namespace kurator
{
@@ -9,6 +11,7 @@ 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);