diff options
Diffstat (limited to 'battles')
-rw-r--r-- | battles/CMakeLists.txt | 1 | ||||
-rw-r--r-- | battles/src/BaseBattle.cpp | 17 | ||||
-rw-r--r-- | battles/src/BaseBattle.h | 2 | ||||
-rw-r--r-- | battles/src/TeamManager.cpp | 60 | ||||
-rw-r--r-- | battles/src/TeamManager.h | 31 |
5 files changed, 106 insertions, 5 deletions
diff --git a/battles/CMakeLists.txt b/battles/CMakeLists.txt index 261edd7..3aa0840 100644 --- a/battles/CMakeLists.txt +++ b/battles/CMakeLists.txt @@ -6,6 +6,7 @@ add_library( src/Point.cpp src/RandomSpawner.cpp src/scenarios.cpp + src/TeamManager.cpp ) target_include_directories( ${PROJECT_NAME} diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp index 8527a56..92ab351 100644 --- a/battles/src/BaseBattle.cpp +++ b/battles/src/BaseBattle.cpp @@ -30,6 +30,7 @@ BaseBattle::BaseBattle(const Scenario& scenario) : _registry.emplace<Transform>(entity, spawner.get(ship.team)); _registry.emplace<FloatingMovement>(entity, 0.4); _registry.emplace<AIState>(entity, Point{0.0, 0.0}); + manager.add(ship.team, entity); // registry supports on construction events } } @@ -44,16 +45,22 @@ BaseBattle::registry() void BaseBattle::update(const float dt) { - auto view = _registry.view<Transform, AIState, FloatingMovement>(); - for (auto&& [entity, transform, state, movement] : view.each()) { + auto view = _registry.view<Team, Transform, AIState, FloatingMovement>(); + for (auto&& [entity, team, transform, state, movement] : view.each()) { + if (!_registry.valid(state.target)) + state.target = manager.random((team.id + 1) % 2); + const auto target = _registry.get<Transform>(state.target); const double speed = movement.speed * dt; - const Point diff = (state.destination - transform.position); - if (diff.magnitude() > speed) { - const Point eff = diff.normalized().scale(speed); + const Point diff = target.position - transform.position; + const Point dest = target.position - diff.normalized(); + const Point move = dest - transform.position; + if (move.magnitude() > speed) { + const Point eff = move.normalized().scale(speed); transform.position.x += eff.x; transform.position.y += eff.y; } } + manager.clear(_registry); // registry supports on destructions events } diff --git a/battles/src/BaseBattle.h b/battles/src/BaseBattle.h index b47ebd7..124d1b7 100644 --- a/battles/src/BaseBattle.h +++ b/battles/src/BaseBattle.h @@ -6,6 +6,7 @@ #include <kurator/battles/Scenario.h> #include "RandomSpawner.h" +#include "TeamManager.h" namespace kurator @@ -23,6 +24,7 @@ public: private: entt::registry _registry; RandomSpawner spawner; + TeamManager manager; }; diff --git a/battles/src/TeamManager.cpp b/battles/src/TeamManager.cpp new file mode 100644 index 0000000..e4afeb3 --- /dev/null +++ b/battles/src/TeamManager.cpp @@ -0,0 +1,60 @@ +#include "TeamManager.h" + +#include <algorithm> +#include <random> +#include <utility> +#include <vector> + +#include <entt/entity/registry.hpp> + + +namespace kurator +{ +namespace battles +{ + + +TeamManager::TeamManager() : + teams {}, + generator {std::random_device{}()} +{ +} + + +void +TeamManager::add(int team, entt::entity entity) +{ + for (int i = teams.size(); i < team + 1; ++i) + teams.emplace_back(); + teams.at(team).push_back(std::move(entity)); +} + + +TeamManager::Team +TeamManager::get(int team) const +{ + return teams.at(team); +} + + +entt::entity +TeamManager::random(int team) +{ + auto& members = teams.at(team); + std::uniform_int_distribution<Team::size_type> uniform{0, members.size() - 1}; + return members.at(uniform(generator)); +} + + +void +TeamManager::clear(entt::registry& registry) +{ + for (auto& members : teams) { + auto is_valid = [®istry](entt::entity entity){ return !registry.valid(entity); }; + members.erase(std::remove_if(members.begin(), members.end(), is_valid), members.end()); + } +} + + +} // namespace battles +} // namespace kurator diff --git a/battles/src/TeamManager.h b/battles/src/TeamManager.h new file mode 100644 index 0000000..384d9ee --- /dev/null +++ b/battles/src/TeamManager.h @@ -0,0 +1,31 @@ +#pragma once + +#include <random> +#include <vector> + +#include <entt/entity/registry.hpp> + + +namespace kurator +{ +namespace battles +{ + + +class TeamManager +{ +public: + using Team = std::vector<entt::entity>; + TeamManager(); + void add(int team, entt::entity entity); + Team get(int team) const; + entt::entity random(int team); + void clear(entt::registry& registry); +private: + std::vector<Team> teams; + std::mt19937 generator; +}; + + +} // namespace battles +} // namespace kurator |