From 2c6e2d631760f962e58fef7048ec0c7d3ecaa387 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 31 Dec 2022 18:56:32 +0100 Subject: Turrets may now shoot multiple times per frame --- sim/src/TurretControl.cpp | 52 ++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'sim/src') diff --git a/sim/src/TurretControl.cpp b/sim/src/TurretControl.cpp index b5527fc..19dbe21 100644 --- a/sim/src/TurretControl.cpp +++ b/sim/src/TurretControl.cpp @@ -14,6 +14,9 @@ namespace sim { +bool consume(float& dt, double& target); + + void TurretControl::update(entt::registry& registry, entt::dispatcher& dispatcher, const float dt) { @@ -26,30 +29,41 @@ TurretControl::update(entt::registry& registry, entt::dispatcher& dispatcher, co 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}); + const auto& target = registry.get(state.target); + const auto distance = transform.position.distance(target.position); + if (distance > def.optimal_range * 2.5) + continue; + auto remaining_dt = dt; + while (remaining_dt > 0.0) { + if (control.rounds < 1 && consume(remaining_dt, control.reload)) + control.rounds = def.rounds; + if (control.rounds > 0 && consume(remaining_dt, control.delay)) { + auto& target_points = registry.get(state.target); + 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; } - control.delay = def.rate_of_fire; - if (--control.rounds < 1) - control.reload = def.reload; } } } +bool +consume(float& dt, double& target) +{ + if (target <= 0.0) + return true; + const auto _dt = dt; + dt -= target; + target -= _dt; + return target <= 0.0; +} + + } // namespace sim } // namespace kurator -- cgit v1.1