From ce54b10abb2da54ded8f50f39f9ab4e102298161 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 22 Oct 2025 19:54:27 +0200 Subject: [PATCH] afegida classe Cooldown --- source/cooldown.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 source/cooldown.hpp diff --git a/source/cooldown.hpp b/source/cooldown.hpp new file mode 100644 index 0000000..2061605 --- /dev/null +++ b/source/cooldown.hpp @@ -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_; +};