Files
coffee_crisis_arcade_edition/source/tabe.h
2025-07-20 16:12:27 +02:00

136 lines
5.9 KiB
C++

#pragma once
#include <SDL3/SDL.h> // Para Uint32, SDL_GetTicks, SDL_FRect
#include <cstdlib> // Para rand
#include <memory> // Para unique_ptr
#include "animated_sprite.h" // Para AnimatedSprite
// --- Enumeraciones para dirección y estado ---
enum class TabeDirection : int {
TO_THE_LEFT = 0,
TO_THE_RIGHT = 1,
};
enum class TabeState : int {
FLY = 0,
HIT = 1,
};
// --- Estructura para el temporizador del Tabe ---
struct TabeTimer {
private:
static constexpr Uint32 MINUTES_TO_MILLISECONDS = 60000; // Factor de conversión de minutos a milisegundos
public:
Uint32 time_until_next_spawn; // Tiempo restante para la próxima aparición
Uint32 min_spawn_time; // Tiempo mínimo entre apariciones (en milisegundos)
Uint32 max_spawn_time; // Tiempo máximo entre apariciones (en milisegundos)
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{false}; // Indica si el temporizador está pausado
// Constructor - los parámetros min_time y max_time están en mintos
TabeTimer(float min_time, float max_time)
: min_spawn_time(static_cast<Uint32>(min_time * MINUTES_TO_MILLISECONDS)),
max_spawn_time(static_cast<Uint32>(max_time * MINUTES_TO_MILLISECONDS)),
current_time(SDL_GetTicks()) {
reset();
}
// Restablece el temporizador con un nuevo tiempo hasta la próxima aparición
void reset() {
Uint32 range = max_spawn_time - min_spawn_time;
time_until_next_spawn = min_spawn_time + rand() % (range + 1);
last_time = SDL_GetTicks();
}
// Actualiza el temporizador, decrementando el tiempo hasta la próxima aparición
void update() {
current_time = SDL_GetTicks();
// 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;
}
// 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
[[nodiscard]] auto shouldSpawn() const -> bool {
return time_until_next_spawn == 0 && !is_paused;
}
};
// --- Clase Tabe ---
class Tabe {
public:
// --- Constructores y destructor ---
Tabe();
~Tabe() = default;
// --- Métodos principales ---
void update(); // Actualiza la lógica
void render(); // Dibuja el objeto
void enable(); // Habilita el objeto
void setState(TabeState state); // Establece el estado
auto tryToGetBonus() -> bool; // Intenta obtener el bonus
void pauseTimer(bool value); // Detiene/activa el timer
// --- Getters ---
auto getCollider() -> SDL_FRect& { return sprite_->getRect(); } // Obtiene el área de colisión
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Indica si el objeto está activo
private:
// --- Constantes ---
static constexpr int WIDTH = 32;
static constexpr int HEIGHT = 32;
// --- Objetos y punteros ---
std::unique_ptr<AnimatedSprite> sprite_; // Sprite con los gráficos y animaciones
// --- Variables de estado ---
float x_ = 0; // Posición X
float y_ = 0; // Posición Y
float speed_ = 0.0F; // Velocidad de movimiento
float accel_ = 0.0F; // Aceleración
int fly_distance_ = 0; // Distancia de vuelo
int waiting_counter_ = 0; // Tiempo que pasa quieto
bool enabled_ = false; // Indica si el objeto está activo
TabeDirection direction_ = TabeDirection::TO_THE_LEFT; // Dirección actual
TabeDirection destiny_ = TabeDirection::TO_THE_LEFT; // Destino
TabeState state_ = TabeState::FLY; // Estado actual
int hit_counter_ = 0; // Contador para el estado HIT
int number_of_hits_ = 0; // Cantidad de disparos recibidos
bool has_bonus_ = true; // Indica si aún tiene el bonus para soltar
TabeTimer timer_; // Temporizador para gestionar la aparición
// --- Métodos internos ---
void move(); // Mueve el objeto
void shiftSprite() { sprite_->setPos(x_, y_); } // Actualiza la posición del sprite
void setRandomFlyPath(TabeDirection direction, int lenght); // Establece un vuelo aleatorio
void updateState(); // Actualiza el estado
void updateTimer(); // Actualiza el temporizador
void disable(); // Deshabilita el objeto
};