diff options
author | Aki <please@ignore.pl> | 2022-11-12 13:43:54 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-11-12 13:43:54 +0100 |
commit | 0e57ea98049658b4d82488c43d71b4ced534c2af (patch) | |
tree | 4f3a9c3cecc138db77ab948db28fed2ce7792c29 | |
parent | 53d45e11743909b8816ef95f50ac1c2b6467a3b7 (diff) | |
download | kurator-0e57ea98049658b4d82488c43d71b4ced534c2af.zip kurator-0e57ea98049658b4d82488c43d71b4ced534c2af.tar.gz kurator-0e57ea98049658b4d82488c43d71b4ced534c2af.tar.bz2 |
Extracted basic implementation of concrete Battle
-rw-r--r-- | battles/CMakeLists.txt | 1 | ||||
-rw-r--r-- | battles/src/BaseBattle.cpp | 66 | ||||
-rw-r--r-- | battles/src/BaseBattle.h | 30 | ||||
-rw-r--r-- | battles/src/Battle.cpp | 66 |
4 files changed, 98 insertions, 65 deletions
diff --git a/battles/CMakeLists.txt b/battles/CMakeLists.txt index 15e9042..37d1903 100644 --- a/battles/CMakeLists.txt +++ b/battles/CMakeLists.txt @@ -1,6 +1,7 @@ project(battles) add_library( ${PROJECT_NAME} + src/BaseBattle.cpp src/Battle.cpp src/RandomSpawner.cpp src/scenarios.cpp diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp new file mode 100644 index 0000000..a9ac45c --- /dev/null +++ b/battles/src/BaseBattle.cpp @@ -0,0 +1,66 @@ +#include "BaseBattle.h" + +#include <cmath> +#include <memory> + +#include <entt/entity/registry.hpp> + +#include <kurator/battles/components.h> +#include <kurator/battles/Scenario.h> +#include <kurator/universe/ShipType.h> + + +namespace kurator +{ +namespace battles +{ + + +int total_teams_in(const Scenario& scenario); + + +BaseBattle::BaseBattle(const Scenario& scenario) : + _registry {}, + spawner {total_teams_in(scenario), 2.5, 0.1} +{ + 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, spawner.get(ship.team)); + } +} + + +entt::registry& +BaseBattle::registry() +{ + return _registry; +} + + +void +BaseBattle::update(const float dt) +{ + auto view = _registry.view<Transform>(); + for (auto&& [entity, transform] : view.each()) { + transform.position.x += 0.1 * dt * std::cos(transform.angle); + transform.position.y += 0.1 * dt * std::sin(transform.angle); + } +} + + +int +total_teams_in(const Scenario& scenario) +{ + int last_team = 0; + for (const auto& ship : scenario.ships) { + if (ship.team > last_team) + last_team = ship.team; + } + return last_team + 1; +} + + +} // namespace battles +} // namespace kurator diff --git a/battles/src/BaseBattle.h b/battles/src/BaseBattle.h new file mode 100644 index 0000000..b47ebd7 --- /dev/null +++ b/battles/src/BaseBattle.h @@ -0,0 +1,30 @@ +#pragma once + +#include <entt/entity/registry.hpp> + +#include <kurator/battles/Battle.h> +#include <kurator/battles/Scenario.h> + +#include "RandomSpawner.h" + + +namespace kurator +{ +namespace battles +{ + + +class BaseBattle : public Battle +{ +public: + BaseBattle(const Scenario& scenario); + entt::registry& registry() override; + void update(float dt) override; +private: + entt::registry _registry; + RandomSpawner spawner; +}; + + +} // namespace battles +} // namespace kurator diff --git a/battles/src/Battle.cpp b/battles/src/Battle.cpp index 7040dd0..f44a88e 100644 --- a/battles/src/Battle.cpp +++ b/battles/src/Battle.cpp @@ -1,16 +1,10 @@ #include <kurator/battles/Battle.h> -#include <cmath> #include <memory> -#include <random> -#include <entt/entity/registry.hpp> - -#include <kurator/battles/components.h> #include <kurator/battles/Scenario.h> -#include <kurator/universe/ShipType.h> -#include "RandomSpawner.h" +#include "BaseBattle.h" namespace kurator @@ -19,52 +13,6 @@ namespace battles { -int total_teams_in(const Scenario& scenario); - - -class BaseBattle : public Battle -{ -public: - BaseBattle(const Scenario& scenario); - entt::registry& registry() override; - void update(float dt) override; -private: - entt::registry _registry; - RandomSpawner spawner; -}; - - -BaseBattle::BaseBattle(const Scenario& scenario) : - _registry {}, - spawner {total_teams_in(scenario), 2.5, 0.1} -{ - 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, spawner.get(ship.team)); - } -} - - -entt::registry& -BaseBattle::registry() -{ - return _registry; -} - - -void -BaseBattle::update(const float dt) -{ - auto view = _registry.view<Transform>(); - for (auto&& [entity, transform] : view.each()) { - transform.position.x += 0.1 * dt * std::cos(transform.angle); - transform.position.y += 0.1 * dt * std::sin(transform.angle); - } -} - - std::unique_ptr<Battle> prepare(const Scenario& scenario) { @@ -72,17 +20,5 @@ prepare(const Scenario& scenario) } -int -total_teams_in(const Scenario& scenario) -{ - int last_team = 0; - for (const auto& ship : scenario.ships) { - if (ship.team > last_team) - last_team = ship.team; - } - return last_team + 1; -} - - } // namespace battles } // namespace kurator |