afegida classe Cooldown
This commit is contained in:
45
source/cooldown.hpp
Normal file
45
source/cooldown.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
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),
|
||||
remaining_s_(0.0f), held_before_(false) {}
|
||||
|
||||
// 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;
|
||||
if (remaining_s_ < 0.0f) remaining_s_ = 0.0f;
|
||||
}
|
||||
|
||||
// Llamar cuando el input está activo. Devuelve true si debe ejecutarse la acción ahora.
|
||||
bool tryConsumeOnHeld() {
|
||||
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;
|
||||
}
|
||||
|
||||
bool empty() const { 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_;
|
||||
bool held_before_;
|
||||
};
|
||||
Reference in New Issue
Block a user