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/CMakeLists.txt | 1 + sim/include/kurator/sim/TurretControl.h | 24 ++++++++++++++ sim/include/kurator/sim/components.h | 9 ------ sim/src/BaseBattle.cpp | 40 ++---------------------- sim/src/Builder.cpp | 1 + sim/src/TurretControl.cpp | 55 +++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 47 deletions(-) create mode 100644 sim/include/kurator/sim/TurretControl.h create mode 100644 sim/src/TurretControl.cpp (limited to 'sim') diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index ff2621c..e10056a 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -8,6 +8,7 @@ add_library( src/Point.cpp src/RandomSpawner.cpp src/TeamManager.cpp + src/TurretControl.cpp ) target_include_directories( ${PROJECT_NAME} diff --git a/sim/include/kurator/sim/TurretControl.h b/sim/include/kurator/sim/TurretControl.h new file mode 100644 index 0000000..f6dc13a --- /dev/null +++ b/sim/include/kurator/sim/TurretControl.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + + +namespace kurator +{ +namespace sim +{ + + +struct TurretControl +{ + double delay; + double reload; + int rounds; + entt::entity owner; + static void update(entt::registry& registry, entt::dispatcher& dispatcher, float dt); +}; + + +} // namespace sim +} // namespace kurator diff --git a/sim/include/kurator/sim/components.h b/sim/include/kurator/sim/components.h index 7190369..34b0f93 100644 --- a/sim/include/kurator/sim/components.h +++ b/sim/include/kurator/sim/components.h @@ -39,14 +39,5 @@ struct HitPoints }; -struct TurretControl -{ - double delay; - double reload; - int rounds; - entt::entity owner; -}; - - } // namespace sim } // namespace kurator 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