diff options
-rw-r--r-- | battles/src/BaseBattle.cpp | 86 | ||||
-rw-r--r-- | battles/src/BaseBattle.h | 5 |
2 files changed, 67 insertions, 24 deletions
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp index fc4aa69..f12a944 100644 --- a/battles/src/BaseBattle.cpp +++ b/battles/src/BaseBattle.cpp @@ -1,8 +1,5 @@ #include "BaseBattle.h" -#include <memory> -#include <type_traits> - #include <entt/entity/registry.hpp> #include <kurator/battles/components.h> @@ -46,30 +43,61 @@ BaseBattle::registry() void BaseBattle::update(const float dt) { - 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); - if (!_registry.valid(state.target)) + pick_random_targets(); + keep_at_range(); + floating_movement(dt); + turrets(dt); + kill_off_dead(); + manager.clear(_registry); // registry supports on destructions events +} + + +void +BaseBattle::pick_random_targets() +{ + auto view = _registry.view<Team, AIState>(); + for (auto&& [entity, team, ai] : view.each()) { + if (!_registry.valid(ai.target)) + ai.target = manager.random((team.id + 1) % 2); // FIXME + } +} + + +void +BaseBattle::keep_at_range() +{ + auto view = _registry.view<Transform, AIState>(); + for (auto&& [entity, self, ai] : view.each()) { + if (!_registry.valid(ai.target)) continue; - const auto target = _registry.get<Transform>(state.target); + const auto target = _registry.get<Transform>(ai.target); + const Point offset = target.position - self.position; + ai.destination = target.position - offset.normalized(); + } +} + + +void +BaseBattle::floating_movement(const float dt) +{ + auto view = _registry.view<Transform, FloatingMovement, AIState>(); + for (auto&& [entity, self, movement, ai] : view.each()) { const double speed = movement.speed * dt; - 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; + const Point offset = ai.destination - self.position; + if (offset.magnitude() > speed) { + const Point move = offset.normalized().scale(speed); + self.position.x += move.x; + self.position.y += move.y; } } - auto view2 = _registry.view<HitPoints>(); - for (auto&& [entity, points] : view2.each()) { - if (points.health <= 0.0) - _registry.destroy(entity); - } - auto view3 = _registry.view<TurretControl, universe::TurretType>(); - for (auto&& [entity, control, def] : view3.each()) { // split into systems! +} + + +void +BaseBattle::turrets(const float dt) +{ + auto view = _registry.view<TurretControl, universe::TurretType>(); + for (auto&& [entity, control, def] : view.each()) { if (!_registry.valid(control.owner)) { _registry.destroy(entity); continue; @@ -88,7 +116,17 @@ BaseBattle::update(const float dt) control.reload = def.rate_of_fire; } } - manager.clear(_registry); // registry supports on destructions events +} + + +void +BaseBattle::kill_off_dead() +{ + auto view = _registry.view<HitPoints>(); + for (auto&& [entity, points] : view.each()) { + if (points.health <= 0.0) + _registry.destroy(entity); + } } diff --git a/battles/src/BaseBattle.h b/battles/src/BaseBattle.h index 124d1b7..02cc024 100644 --- a/battles/src/BaseBattle.h +++ b/battles/src/BaseBattle.h @@ -25,6 +25,11 @@ private: entt::registry _registry; RandomSpawner spawner; TeamManager manager; + void pick_random_targets(); + void keep_at_range(); + void floating_movement(float dt); + void turrets(float dt); + void kill_off_dead(); }; |