diff options
author | Aki <please@ignore.pl> | 2022-11-11 16:32:05 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-11-11 16:32:05 +0100 |
commit | 07049889c4b1afc2306ee609dc3b0ff69a92e3f4 (patch) | |
tree | d3bc41408285354c3b84264188ef7e5040aa7fd0 | |
parent | 4619a1d86dc21c42439b2c2efd6654b565d46441 (diff) | |
download | kurator-07049889c4b1afc2306ee609dc3b0ff69a92e3f4.zip kurator-07049889c4b1afc2306ee609dc3b0ff69a92e3f4.tar.gz kurator-07049889c4b1afc2306ee609dc3b0ff69a92e3f4.tar.bz2 |
Hiding away implementation details of battle simulation
-rw-r--r-- | battles/include/kurator/battles/Battle.h | 14 | ||||
-rw-r--r-- | battles/src/Battle.cpp | 42 | ||||
-rw-r--r-- | kurator/src/Battle.cpp | 7 | ||||
-rw-r--r-- | kurator/src/Battle.h | 2 |
4 files changed, 49 insertions, 16 deletions
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 <memory> + #include <entt/entity/registry.hpp> #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<Battle>; + + } // 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 <kurator/battles/Battle.h> +#include <memory> #include <random> +#include <entt/entity/registry.hpp> + #include <kurator/battles/components.h> #include <kurator/battles/Scenario.h> #include <kurator/universe/ShipType.h> @@ -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<universe::ShipType>(entity, ship.type); - registry.emplace<Team>(entity, ship.team); - registry.emplace<Transform>(entity, Point{pos(dev), pos(dev)}, Point{0.0, 0.0}); + const auto entity = _registry.create(); + _registry.emplace<universe::ShipType>(entity, ship.type); + _registry.emplace<Team>(entity, ship.team); + _registry.emplace<Transform>(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<Transform, Team>(); + auto view = _registry.view<Transform, Team>(); for (auto&& [entity, transform, team] : view.each()) transform.position.x += 0.1 * dt * (team.id * 2 - 1); } +std::unique_ptr<Battle> +prepare(const Scenario& scenario) +{ + return std::make_unique<BaseBattle>(scenario); +} + + } // namespace battles } // namespace kurator diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 5aa18fe..7320c19 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -8,7 +8,6 @@ #include <kurator/battles/components.h> #include <kurator/battles/Battle.h> -#include <kurator/battles/Scenario.h> #include <kurator/battles/scenarios.h> #include <kurator/universe/ShipType.h> @@ -22,7 +21,7 @@ namespace kurator Battle::Battle(std::shared_ptr<Session> _session) : session {std::move(_session)}, - battle {battles::scenarios::example()} + battle {battles::prepare(battles::scenarios::example())} { } @@ -30,7 +29,7 @@ Battle::Battle(std::shared_ptr<Session> _session) : void Battle::update(const float dt) { - battle.update(dt); + battle->update(dt); if (IsKeyPressed(KEY_SPACE)) session->set(std::make_shared<Title>(session)); } @@ -43,7 +42,7 @@ Battle::draw() const const int width = GetScreenWidth(); const int height = GetScreenHeight(); const double scale = std::min(width/10.0, height/10.0); - auto view = battle.registry.view<const universe::ShipType, const battles::Team, const battles::Transform>(); + auto view = battle->registry().view<const universe::ShipType, const battles::Team, const battles::Transform>(); for (auto [entity, ship_type, team, transform] : view.each()) { (void) entity; const auto color = team.id == 1 ? RED : GREEN; diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 476381b..64cdb47 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -20,7 +20,7 @@ public: void draw() const override; private: std::shared_ptr<Session> session; - battles::Battle battle; + std::unique_ptr<battles::Battle> battle; }; |