#include #include #include #include #include #include #include namespace kurator { namespace sim { static bool consume(float& dt, double& target); static bool reload_(float& dt, Turret& turret); void Turret::shoot_at(State& ctx, const entt::entity& target, const double distance) { while (reload_(action, *this)) { auto& target_points = ctx.registry.get(target); const auto& movement = ctx.registry.get(target); auto damage = type.effective_damage(distance, movement.speed.magnitude()); if (damage > 0.0) { damage = target_points.deal(damage); ctx.dispatcher.trigger(Hit{damage, owner, target}); } delay = type.rate_of_fire; if (--rounds < 1) reload = type.reload; } } void update_turrets(State& ctx) { auto turrets = ctx.registry.view(); for (auto&& [entity, turret] : turrets.each()) { if (!ctx.registry.valid(turret.owner)) { ctx.registry.destroy(entity); continue; } while (turret.action > 0.0) { if (reload_(turret.action, turret)) break; } turret.action = ctx.clock.dt; } } bool consume(float& dt, double& target) { if (target <= 0.0) return true; const auto _dt = dt; dt -= target; target -= _dt; return target <= 0.0; } bool reload_(float& dt, Turret& turret) // at this point move it to the class? { if (turret.rounds < 1 && consume(dt, turret.reload)) turret.rounds = turret.type.rounds; return turret.rounds > 0 && consume(dt, turret.delay); } } // namespace sim } // namespace kurator