Files
coffee_crisis_arcade_edition/source/cooldown.hpp
2025-10-24 17:12:57 +02:00

49 lines
1.3 KiB
C++

#pragma once
#include <algorithm> // Para std::max
class Cooldown {
public:
Cooldown(float first_delay_s = 0.0F, float repeat_delay_s = 0.0F)
: first_delay_s_(first_delay_s), repeat_delay_s_(repeat_delay_s) {}
// Llamar cada frame con delta en segundos (float)
void update(float delta_s) {
if (remaining_s_ <= 0.0F) {
remaining_s_ = 0.0F;
return;
}
remaining_s_ -= delta_s;
remaining_s_ = std::max(remaining_s_, 0.0F);
}
// Llamar cuando el input está activo. Devuelve true si debe ejecutarse la acción ahora.
auto tryConsumeOnHeld() -> bool {
if (remaining_s_ > 0.0F) {
return false;
}
float delay = held_before_ ? repeat_delay_s_ : first_delay_s_;
remaining_s_ = delay;
held_before_ = true;
return true;
}
// Llamar cuando el input se suelta
void onReleased() {
held_before_ = false;
remaining_s_ = 0.0F;
}
[[nodiscard]] auto empty() const -> bool { return remaining_s_ == 0.0F; }
// Fuerza un valor en segundos (útil para tests o resets)
void forceSet(float seconds) { remaining_s_ = seconds > 0.0F ? seconds : 0.0F; }
private:
float first_delay_s_;
float repeat_delay_s_;
float remaining_s_{0.0F};
bool held_before_{false};
};