From 07049889c4b1afc2306ee609dc3b0ff69a92e3f4 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 11 Nov 2022 16:32:05 +0100 Subject: Hiding away implementation details of battle simulation --- battles/include/kurator/battles/Battle.h | 14 ++++++++--- battles/src/Battle.cpp | 42 ++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) (limited to 'battles') diff --git a/battles/include/kurator/battles/Battle.h b/battles/include/kurator/battles/Battle.h index 468a972..82a5da5 100644 --- a/battles/include/kurator/battles/Battle.h +++ b/battles/include/kurator/battles/Battle.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "Scenario.h" @@ -11,13 +13,17 @@ namespace battles { -struct Battle +class Battle { - explicit Battle(Scenario scenario); - void update(float dt); - entt::registry registry; +public: + virtual ~Battle() = default; + virtual entt::registry& registry() = 0; + virtual void update(float dt) = 0; }; +auto prepare(const Scenario& scenario) -> std::unique_ptr; + + } // namespace battles } // namespace kurator diff --git a/battles/src/Battle.cpp b/battles/src/Battle.cpp index 454cbfd..99ed076 100644 --- a/battles/src/Battle.cpp +++ b/battles/src/Battle.cpp @@ -1,7 +1,10 @@ #include +#include #include +#include + #include #include #include @@ -13,27 +16,52 @@ namespace battles { -Battle::Battle(Scenario scenario) +class BaseBattle : public Battle +{ +public: + BaseBattle(const Scenario& scenario); + entt::registry& registry() override; + void update(float dt) override; +private: + entt::registry _registry; +}; + + +BaseBattle::BaseBattle(const Scenario& scenario) { std::random_device dev; std::uniform_real_distribution<> pos{-2.5, 2.5}; for (const auto& ship : scenario.ships) { - const auto entity = registry.create(); - registry.emplace(entity, ship.type); - registry.emplace(entity, ship.team); - registry.emplace(entity, Point{pos(dev), pos(dev)}, Point{0.0, 0.0}); + const auto entity = _registry.create(); + _registry.emplace(entity, ship.type); + _registry.emplace(entity, ship.team); + _registry.emplace(entity, Point{pos(dev), pos(dev)}, Point{0.0, 0.0}); } } +entt::registry& +BaseBattle::registry() +{ + return _registry; +} + + void -Battle::update(const float dt) +BaseBattle::update(const float dt) { - auto view = registry.view(); + auto view = _registry.view(); for (auto&& [entity, transform, team] : view.each()) transform.position.x += 0.1 * dt * (team.id * 2 - 1); } +std::unique_ptr +prepare(const Scenario& scenario) +{ + return std::make_unique(scenario); +} + + } // namespace battles } // namespace kurator -- cgit v1.1