From da8efebc3b17041e962d9d2d577a053734843121 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 17 Nov 2022 00:04:07 +0100 Subject: Exploded battle update method into couple of smaller ones --- battles/src/BaseBattle.cpp | 86 +++++++++++++++++++++++++++++++++------------- battles/src/BaseBattle.h | 5 +++ 2 files changed, 67 insertions(+), 24 deletions(-) (limited to 'battles') 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 -#include - #include #include @@ -46,30 +43,61 @@ BaseBattle::registry() void BaseBattle::update(const float dt) { - 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); - 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(); + 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(); + for (auto&& [entity, self, ai] : view.each()) { + if (!_registry.valid(ai.target)) continue; - const auto target = _registry.get(state.target); + const auto target = _registry.get(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(); + 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(); - for (auto&& [entity, points] : view2.each()) { - if (points.health <= 0.0) - _registry.destroy(entity); - } - auto view3 = _registry.view(); - for (auto&& [entity, control, def] : view3.each()) { // split into systems! +} + + +void +BaseBattle::turrets(const float dt) +{ + auto view = _registry.view(); + 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(); + 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(); }; -- cgit v1.1