diff --git a/source/sections/game.cpp b/source/sections/game.cpp index 149b62b..c0eaaf0 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -1091,6 +1091,7 @@ void Game::updateScoreboard() { void Game::pause(bool value) { paused_ = value; screen_->attenuate(paused_); + tabe_->pauseTimer(paused_); } // Añade una puntuación a la tabla de records diff --git a/source/tabe.cpp b/source/tabe.cpp index 2cac0ae..1abc542 100644 --- a/source/tabe.cpp +++ b/source/tabe.cpp @@ -20,11 +20,12 @@ Tabe::Tabe() // Actualiza la lógica void Tabe::update() { - if (enabled_) { + if (enabled_ && !timer_.is_paused) { sprite_->update(); move(); updateState(); } + timer_.update(); if (timer_.should_spawn()) { enable(); @@ -192,4 +193,9 @@ void Tabe::updateTimer() { void Tabe::disable() { enabled_ = false; timer_.reset(); +} + +// Detiene/activa el timer +void Tabe::pauseTimer(bool value) { + timer_.setPaused(value); } \ No newline at end of file diff --git a/source/tabe.h b/source/tabe.h index 43227a7..869868b 100644 --- a/source/tabe.h +++ b/source/tabe.h @@ -26,10 +26,12 @@ struct TabeTimer { Uint32 current_time; // Tiempo actual Uint32 delta_time; // Diferencia de tiempo desde la última actualización Uint32 last_time; // Tiempo de la última actualización + bool is_paused; // Indica si el temporizador está pausado // Constructor TabeTimer(float minTime, float maxTime) - : min_spawn_time(minTime * 60000), max_spawn_time(maxTime * 60000), current_time(SDL_GetTicks()) { + : min_spawn_time(minTime * 60000), max_spawn_time(maxTime * 60000), + current_time(SDL_GetTicks()), is_paused(false) { reset(); } @@ -43,19 +45,36 @@ struct TabeTimer { // Actualiza el temporizador, decrementando el tiempo hasta la próxima aparición void update() { current_time = SDL_GetTicks(); - delta_time = current_time - last_time; + + // Solo actualizar si no está pausado + if (!is_paused) { + delta_time = current_time - last_time; + + if (time_until_next_spawn > delta_time) { + time_until_next_spawn -= delta_time; + } else { + time_until_next_spawn = 0; + } + } + + // Siempre actualizar last_time para evitar saltos de tiempo al despausar last_time = current_time; + } - if (time_until_next_spawn > delta_time) { - time_until_next_spawn -= delta_time; - } else { - time_until_next_spawn = 0; + // Pausa o reanuda el temporizador + void setPaused(bool paused) { + if (is_paused != paused) { + is_paused = paused; + // Al despausar, actualizar last_time para evitar saltos + if (!paused) { + last_time = SDL_GetTicks(); + } } } // Indica si el temporizador ha finalizado bool should_spawn() const { - return time_until_next_spawn == 0; + return time_until_next_spawn == 0 && !is_paused; } }; @@ -72,6 +91,7 @@ class Tabe { void enable(); // Habilita el objeto void setState(TabeState state); // Establece el estado bool tryToGetBonus(); // Intenta obtener el bonus + void pauseTimer(bool value); // Detiene/activa el timer // --- Getters --- SDL_FRect &getCollider() { return sprite_->getRect(); } // Obtiene el área de colisión