From 1b39b8aaf6be5be56530979044fb893a5b51b489 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 13 Nov 2022 00:20:42 +0100 Subject: Implemented dumb keep-at-range behaviour --- battles/src/BaseBattle.cpp | 17 +++++++++---- battles/src/BaseBattle.h | 2 ++ battles/src/TeamManager.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++ battles/src/TeamManager.h | 31 +++++++++++++++++++++++ 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 battles/src/TeamManager.cpp create mode 100644 battles/src/TeamManager.h (limited to 'battles/src') 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(entity, spawner.get(ship.team)); _registry.emplace(entity, 0.4); _registry.emplace(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(); - for (auto&& [entity, transform, state, movement] : view.each()) { + auto view = _registry.view(); + 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(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 #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 +#include +#include +#include + +#include + + +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 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 +#include + +#include + + +namespace kurator +{ +namespace battles +{ + + +class TeamManager +{ +public: + using Team = std::vector; + 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 teams; + std::mt19937 generator; +}; + + +} // namespace battles +} // namespace kurator -- cgit v1.1