diff options
author | Aki <please@ignore.pl> | 2022-11-17 00:04:07 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-11-17 00:06:14 +0100 |
commit | da8efebc3b17041e962d9d2d577a053734843121 (patch) | |
tree | 0d40c71fce571894925d0f3b6c28b2295f9e6eb3 | |
parent | 057d4061a1dca1dc76096826fd8066316ce5447d (diff) | |
download | kurator-da8efebc3b17041e962d9d2d577a053734843121.zip kurator-da8efebc3b17041e962d9d2d577a053734843121.tar.gz kurator-da8efebc3b17041e962d9d2d577a053734843121.tar.bz2 |
Exploded battle update method into couple of smaller ones
-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(); }; |