diff options
author | Aki <please@ignore.pl> | 2022-12-31 18:56:32 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-12-31 18:56:32 +0100 |
commit | 2c6e2d631760f962e58fef7048ec0c7d3ecaa387 (patch) | |
tree | 72fa93d4a67fbf18700b1d1a5a8d5ff91568323f /sim | |
parent | 3615d2a54325eca6e85dc0975e3391d5f943ec3a (diff) | |
download | kurator-2c6e2d631760f962e58fef7048ec0c7d3ecaa387.zip kurator-2c6e2d631760f962e58fef7048ec0c7d3ecaa387.tar.gz kurator-2c6e2d631760f962e58fef7048ec0c7d3ecaa387.tar.bz2 |
Turrets may now shoot multiple times per frame
Diffstat (limited to 'sim')
-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 |