From 057d4061a1dca1dc76096826fd8066316ce5447d Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 16 Nov 2022 23:37:17 +0100 Subject: Extracted entity construction to Builder class --- battles/CMakeLists.txt | 1 + battles/src/BaseBattle.cpp | 21 ++++++------------- battles/src/Builder.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ battles/src/Builder.h | 35 +++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 battles/src/Builder.cpp create mode 100644 battles/src/Builder.h (limited to 'battles') 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 #include +#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::type>(entity, type); - _registry.emplace(entity, ship.team); - _registry.emplace(entity, spawner.get(ship.team)); - _registry.emplace(entity, 0.4); - _registry.emplace(entity, Point{0.0, 0.0}); - _registry.emplace(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::type>(turret, def); - _registry.emplace(turret, 0.0, entity); - _registry.emplace(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 + +#include +#include +#include +#include + +#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(entity, ship_type); + registry.emplace(entity, team); + registry.emplace(entity, spawner.get(team)); + registry.emplace(entity, 0.4); + registry.emplace(entity, Point{0.0, 0.0}); + registry.emplace(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(entity, turret_type); + registry.emplace(entity, 0.0, owner); + registry.emplace(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 + +#include +#include + +#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 -- cgit v1.1