diff options
author | Aki <please@ignore.pl> | 2022-11-11 17:02:58 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-11-11 17:02:58 +0100 |
commit | bd8b0a9ba1bdd68b915fed75f1e901851b340efd (patch) | |
tree | 03e073ed0e4ffa5063768f79821745ffd7fd45e4 /battles/src | |
parent | 07049889c4b1afc2306ee609dc3b0ff69a92e3f4 (diff) | |
download | kurator-bd8b0a9ba1bdd68b915fed75f1e901851b340efd.zip kurator-bd8b0a9ba1bdd68b915fed75f1e901851b340efd.tar.gz kurator-bd8b0a9ba1bdd68b915fed75f1e901851b340efd.tar.bz2 |
Extracted random spawning mechanism to a dedicated class
Diffstat (limited to 'battles/src')
-rw-r--r-- | battles/src/Battle.cpp | 26 | ||||
-rw-r--r-- | battles/src/RandomSpawner.cpp | 38 | ||||
-rw-r--r-- | battles/src/RandomSpawner.h | 28 |
3 files changed, 88 insertions, 4 deletions
diff --git a/battles/src/Battle.cpp b/battles/src/Battle.cpp index 99ed076..a28cdc6 100644 --- a/battles/src/Battle.cpp +++ b/battles/src/Battle.cpp @@ -9,6 +9,8 @@ #include <kurator/battles/Scenario.h> #include <kurator/universe/ShipType.h> +#include "RandomSpawner.h" + namespace kurator { @@ -16,6 +18,9 @@ namespace battles { +int total_teams_in(const Scenario& scenario); + + class BaseBattle : public Battle { public: @@ -24,18 +29,19 @@ public: void update(float dt) override; private: entt::registry _registry; + RandomSpawner spawner; }; -BaseBattle::BaseBattle(const Scenario& scenario) +BaseBattle::BaseBattle(const Scenario& scenario) : + _registry {}, + spawner {total_teams_in(scenario), 2.5, 0.1} { - 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}); + _registry.emplace<Transform>(entity, spawner.get(ship.team)); } } @@ -63,5 +69,17 @@ 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 diff --git a/battles/src/RandomSpawner.cpp b/battles/src/RandomSpawner.cpp new file mode 100644 index 0000000..a81a18c --- /dev/null +++ b/battles/src/RandomSpawner.cpp @@ -0,0 +1,38 @@ +#include "RandomSpawner.h" + +#include <cmath> + +#include <kurator/battles/components.h> +#include <kurator/battles/Point.h> + + +namespace kurator +{ +namespace battles +{ + + +RandomSpawner::RandomSpawner(const int total_teams, const double distance, const double variation) : + angle_step {2.0 * M_PI / total_teams}, + device {}, + distribution_d {distance - distance * variation, distance + distance * variation}, + distribution_a {-variation * M_PI, variation * M_PI} +{ +} + + +Transform +RandomSpawner::get(const int team) +{ + const double distance = distribution_d(device); + const double angle = angle_step * team + distribution_a(device); + const Point position { + distance * std::cos(angle), + distance * std::sin(angle), + }; + return {position, {}}; +} + + +} // namespace battles +} // namespace kurator diff --git a/battles/src/RandomSpawner.h b/battles/src/RandomSpawner.h new file mode 100644 index 0000000..7505ae8 --- /dev/null +++ b/battles/src/RandomSpawner.h @@ -0,0 +1,28 @@ +#pragma once + +#include <random> + +#include <kurator/battles/components.h> + + +namespace kurator +{ +namespace battles +{ + + +class RandomSpawner +{ +public: + RandomSpawner(int total_teams, double distance, double variation); + Transform get(int team); +private: + const double angle_step; + std::random_device device; + std::uniform_real_distribution<double> distribution_d; + std::uniform_real_distribution<double> distribution_a; +}; + + +} // namespace battles +} // namespace kurator |