summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--battles/CMakeLists.txt1
-rw-r--r--battles/src/BaseBattle.cpp21
-rw-r--r--battles/src/Builder.cpp52
-rw-r--r--battles/src/Builder.h35
4 files changed, 94 insertions, 15 deletions
diff --git a/battles/CMakeLists.txt b/battles/CMakeLists.txt
index 3aa0840..4484670 100644
--- a/battles/CMakeLists.txt
+++ b/battles/CMakeLists.txt
@@ -3,6 +3,7 @@ add_library(
${PROJECT_NAME}
src/BaseBattle.cpp
src/Battle.cpp
+ src/Builder.cpp
src/Point.cpp
src/RandomSpawner.cpp
src/scenarios.cpp
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp
index 87f450a..fc4aa69 100644
--- a/battles/src/BaseBattle.cpp
+++ b/battles/src/BaseBattle.cpp
@@ -9,6 +9,8 @@
#include <kurator/battles/Scenario.h>
#include <kurator/universe.h>
+#include "Builder.h"
+
namespace kurator
{
@@ -24,22 +26,11 @@ BaseBattle::BaseBattle(const Scenario& scenario) :
spawner {total_teams_in(scenario), 2.5, 0.1}
{
const auto repo = universe::load_sample();
+ Builder build {_registry, spawner};
for (const auto& ship : scenario.ships) {
- const auto entity = _registry.create();
- const auto type = repo->ship_type(ship.type);
- _registry.emplace<std::decay<decltype(type)>::type>(entity, type);
- _registry.emplace<Team>(entity, ship.team);
- _registry.emplace<Transform>(entity, spawner.get(ship.team));
- _registry.emplace<FloatingMovement>(entity, 0.4);
- _registry.emplace<AIState>(entity, Point{0.0, 0.0});
- _registry.emplace<HitPoints>(entity, type.base_health_points);
- for (const auto& turret_name : ship.turrets) {
- const auto turret = _registry.create();
- const auto def = repo->turret_type(turret_name);
- _registry.emplace<std::decay<decltype(def)>::type>(turret, def);
- _registry.emplace<TurretControl>(turret, 0.0, entity);
- _registry.emplace<Transform>(turret, Point{0.0, 0.0}, 0.0, entity);
- }
+ const auto entity = build(repo->ship_type(ship.type), ship.team);
+ for (const auto& turret_type : ship.turrets)
+ build(repo->turret_type(turret_type), entity);
manager.add(ship.team, entity); // registry supports on construction events
}
}
diff --git a/battles/src/Builder.cpp b/battles/src/Builder.cpp
new file mode 100644
index 0000000..228e893
--- /dev/null
+++ b/battles/src/Builder.cpp
@@ -0,0 +1,52 @@
+#include "Builder.h"
+
+#include <entt/entity/registry.hpp>
+
+#include <kurator/battles/components.h>
+#include <kurator/battles/Point.h>
+#include <kurator/universe/ShipType.h>
+#include <kurator/universe/TurretType.h>
+
+#include "Spawner.h"
+
+
+namespace kurator
+{
+namespace battles
+{
+
+
+Builder::Builder(entt::registry& _registry, Spawner& _spawner) :
+ registry {_registry},
+ spawner {_spawner}
+{
+}
+
+
+entt::entity
+Builder::operator()(const universe::ShipType& ship_type, const int team) const
+{
+ const auto entity = registry.create();
+ registry.emplace<universe::ShipType>(entity, ship_type);
+ registry.emplace<Team>(entity, team);
+ registry.emplace<Transform>(entity, spawner.get(team));
+ registry.emplace<FloatingMovement>(entity, 0.4);
+ registry.emplace<AIState>(entity, Point{0.0, 0.0});
+ registry.emplace<HitPoints>(entity, ship_type.base_health_points);
+ return entity;
+}
+
+
+entt::entity
+Builder::operator()(const universe::TurretType& turret_type, const entt::entity& owner) const
+{
+ const auto entity = registry.create();
+ registry.emplace<universe::TurretType>(entity, turret_type);
+ registry.emplace<TurretControl>(entity, 0.0, owner);
+ registry.emplace<Transform>(entity, Point{0.0, 0.0}, 0.0, owner);
+ return entity;
+}
+
+
+} // namespace battles
+} // namespace kurator
diff --git a/battles/src/Builder.h b/battles/src/Builder.h
new file mode 100644
index 0000000..df50c4e
--- /dev/null
+++ b/battles/src/Builder.h
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <entt/entity/registry.hpp>
+
+#include <kurator/universe/ShipType.h>
+#include <kurator/universe/TurretType.h>
+
+#include "Spawner.h"
+
+
+namespace kurator
+{
+namespace battles
+{
+
+
+class Builder
+{
+public:
+ Builder(entt::registry& _registry, Spawner& _spawner);
+ ~Builder() = default;
+ Builder(Builder&&) = delete;
+ Builder(const Builder&) = delete;
+ Builder& operator=(Builder&&) = delete;
+ Builder& operator=(const Builder&) = delete;
+ entt::entity operator()(const universe::ShipType& ship_type, int team) const;
+ entt::entity operator()(const universe::TurretType& turret_type, const entt::entity& owner) const;
+private:
+ entt::registry& registry;
+ Spawner& spawner;
+};
+
+
+} // namespace battles
+} // namespace kurator