diff options
-rw-r--r-- | sim/src/TurretControl.cpp | 52 |
1 files changed, 33 insertions, 19 deletions
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<AIState, Transform>(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<HitPoints>(state.target); - const auto& target = registry.get<Transform>(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<Transform>(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<HitPoints>(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 |