From 087803532fd7dfa04bc9a39fd2886f572002d0b1 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 31 Dec 2022 18:12:24 +0100 Subject: Extracted TurretControl to own unit --- sim/src/BaseBattle.cpp | 40 ++-------------------------------- sim/src/Builder.cpp | 1 + sim/src/TurretControl.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 sim/src/TurretControl.cpp (limited to 'sim/src') diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 1b27c2e..8ec603a 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -64,7 +65,7 @@ BaseBattle::update(const float dt) pick_random_targets(); keep_at_range(); FloatingMovement::update(_registry, _dispatcher, dt); - turrets(dt); + TurretControl::update(_registry, _dispatcher, dt); kill_off_dead(); manager.clear(_registry); // registry supports on destructions events manager.update(_dispatcher); @@ -97,43 +98,6 @@ BaseBattle::keep_at_range() 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; - } - const auto& [state, transform] = _registry.get(control.owner); // no checks - if (!_registry.valid(state.target)) - continue; - if (control.reload > 0.0) - control.reload -= dt; - if (control.rounds < 1 && control.reload <= 0.0) - control.rounds = def.rounds; - if (control.delay > 0.0) - control.delay -= dt; - if (control.rounds > 0 && control.delay <= 0.0) { - auto& target_points = _registry.get(state.target); - const auto& target = _registry.get(state.target); - const auto distance = transform.position.distance(target.position); - if (distance > def.optimal_range * 2.5) - continue; - const auto damage = def.effective_damage(distance); - if (damage > 0.0) { - target_points.health -= damage; - _dispatcher.trigger(Hit{damage, control.owner, state.target}); - } - control.delay = def.rate_of_fire; - if (--control.rounds < 1) - control.reload = def.reload; - } - } -} - - -void BaseBattle::kill_off_dead() { auto view = _registry.view(); diff --git a/sim/src/Builder.cpp b/sim/src/Builder.cpp index 068d012..11e0f1e 100644 --- a/sim/src/Builder.cpp +++ b/sim/src/Builder.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/sim/src/TurretControl.cpp b/sim/src/TurretControl.cpp new file mode 100644 index 0000000..b5527fc --- /dev/null +++ b/sim/src/TurretControl.cpp @@ -0,0 +1,55 @@ +#include + +#include +#include + +#include +#include +#include + + +namespace kurator +{ +namespace sim +{ + + +void +TurretControl::update(entt::registry& registry, entt::dispatcher& dispatcher, const float dt) +{ + auto view = registry.view(); + for (auto&& [entity, control, def] : view.each()) { + if (!registry.valid(control.owner)) { + registry.destroy(entity); + continue; + } + const auto& [state, transform] = registry.get(control.owner); // no checks + if (!registry.valid(state.target)) + continue; + if (control.reload > 0.0) + control.reload -= dt; + if (control.rounds < 1 && control.reload <= 0.0) + control.rounds = def.rounds; + if (control.delay > 0.0) + control.delay -= dt; + if (control.rounds > 0 && control.delay <= 0.0) { + auto& target_points = registry.get(state.target); + const auto& target = registry.get(state.target); + const auto distance = transform.position.distance(target.position); + if (distance > def.optimal_range * 2.5) + continue; + const auto damage = def.effective_damage(distance); + if (damage > 0.0) { + target_points.health -= damage; + dispatcher.trigger(Hit{damage, control.owner, state.target}); + } + control.delay = def.rate_of_fire; + if (--control.rounds < 1) + control.reload = def.reload; + } + } +} + + +} // namespace sim +} // namespace kurator -- cgit v1.1