migrat Game, Player i Item a time based

This commit is contained in:
2025-10-28 10:52:13 +01:00
parent 9e8c5e13df
commit 31c84f9676
8 changed files with 190 additions and 153 deletions

View File

@@ -115,42 +115,39 @@ void Game::run() {
// Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update() {
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - ticks_ > GAME_SPEED) {
// Actualiza el contador de ticks
ticks_ = SDL_GetTicks();
// Calcula el delta time
const float DELTA_TIME = delta_timer_.tick();
// Comprueba el teclado
checkInput();
// Comprueba el teclado
checkInput();
#ifdef _DEBUG
Debug::get()->clear();
Debug::get()->clear();
#endif
// Actualiza los objetos
room_->update();
if (mode_ == GameMode::GAME) {
player_->update();
checkPlayerIsOnBorder();
checkPlayerAndItems();
checkPlayerAndEnemies();
checkIfPlayerIsAlive();
checkGameOver();
checkEndGame();
checkRestoringJail();
checkSomeCheevos();
}
demoCheckRoomChange();
scoreboard_->update();
keepMusicPlaying();
updateBlackScreen();
Screen::get()->update();
#ifdef _DEBUG
updateDebugInfo();
#endif
// Actualiza los objetos
room_->update(DELTA_TIME);
if (mode_ == GameMode::GAME) {
player_->update(DELTA_TIME);
checkPlayerIsOnBorder();
checkPlayerAndItems();
checkPlayerAndEnemies();
checkIfPlayerIsAlive();
checkGameOver();
checkEndGame();
checkRestoringJail(DELTA_TIME);
checkSomeCheevos();
}
demoCheckRoomChange(DELTA_TIME);
scoreboard_->update();
keepMusicPlaying();
updateBlackScreen(DELTA_TIME);
Screen::get()->update(DELTA_TIME);
#ifdef _DEBUG
updateDebugInfo();
#endif
}
// Pinta los objetos en pantalla
@@ -336,7 +333,7 @@ void Game::checkIfPlayerIsAlive() {
// Comprueba si ha terminado la partida
void Game::checkGameOver() {
if (board_->lives < 0 && black_screen_counter_ > 17) {
if (board_->lives < 0 && black_screen_time_ > GAME_OVER_THRESHOLD) {
SceneManager::current = SceneManager::Scene::GAME_OVER;
}
}
@@ -379,12 +376,12 @@ void Game::setBlackScreen() {
}
// Actualiza las variables relativas a la pantalla en negro
void Game::updateBlackScreen() {
void Game::updateBlackScreen(float delta_time) {
if (black_screen_) {
black_screen_counter_++;
if (black_screen_counter_ > 20) {
black_screen_time_ += delta_time;
if (black_screen_time_ > BLACK_SCREEN_DURATION) {
black_screen_ = false;
black_screen_counter_ = 0;
black_screen_time_ = 0.0F;
player_->setPaused(false);
room_->setPaused(false);
@@ -458,20 +455,19 @@ void Game::togglePause() {
}
// Da vidas al jugador cuando está en la Jail
void Game::checkRestoringJail() {
void Game::checkRestoringJail(float delta_time) {
if (room_->getName() != "THE JAIL" || board_->lives == 9) {
jail_restore_time_ = 0.0F; // Reset timer cuando no está en la Jail
return;
}
static int counter_ = 0;
if (!paused_) {
counter_++;
jail_restore_time_ += delta_time;
}
// Incrementa el numero de vidas
if (counter_ == 100) {
counter_ = 0;
if (jail_restore_time_ >= JAIL_RESTORE_INTERVAL) {
jail_restore_time_ -= JAIL_RESTORE_INTERVAL; // Mantiene el excedente para precisión
board_->lives++;
JA_PlaySound(Resource::get()->getSound("death.wav"));
@@ -599,17 +595,17 @@ void Game::keepMusicPlaying() {
// DEMO MODE: Inicializa las variables para el modo demo
void Game::demoInit() {
if (mode_ == GameMode::DEMO) {
demo_ = DemoData(0, 400, 0, {"04.room", "54.room", "20.room", "09.room", "05.room", "11.room", "31.room", "44.room"});
demo_ = DemoData(0.0F, 0, {"04.room", "54.room", "20.room", "09.room", "05.room", "11.room", "31.room", "44.room"});
current_room_ = demo_.rooms.front();
}
}
// DEMO MODE: Comprueba si se ha de cambiar de habitación
void Game::demoCheckRoomChange() {
void Game::demoCheckRoomChange(float delta_time) {
if (mode_ == GameMode::DEMO) {
demo_.counter++;
if (demo_.counter == demo_.room_time) {
demo_.counter = 0;
demo_.time_accumulator += delta_time;
if (demo_.time_accumulator >= DEMO_ROOM_DURATION) {
demo_.time_accumulator = 0.0F;
demo_.room_index++;
if (demo_.room_index == (int)demo_.rooms.size()) {
SceneManager::current = SceneManager::Scene::LOGO;

View File

@@ -8,6 +8,7 @@
#include <vector> // Para vector
#include "game/entities/player.hpp" // Para PlayerSpawn
#include "utils/delta_timer.hpp" // Para DeltaTimer
class Room; // lines 12-12
class RoomTracker; // lines 13-13
class Scoreboard; // lines 14-14
@@ -22,24 +23,27 @@ enum class GameMode {
class Game {
private:
// Constantes de tiempo
static constexpr float BLACK_SCREEN_DURATION = 0.30F; // Duración de la pantalla negra en segundos (20 frames a 66.67fps)
static constexpr float GAME_OVER_THRESHOLD = 0.255F; // Tiempo antes del game over en segundos (17 frames a 66.67fps)
static constexpr float DEMO_ROOM_DURATION = 6.0F; // Duración de cada habitación en modo demo en segundos (400 frames)
static constexpr float JAIL_RESTORE_INTERVAL = 1.5F; // Intervalo de restauración de vidas en la Jail en segundos (100 frames)
// Estructuras
struct DemoData {
int counter; // Contador para el modo demo
int room_time; // Tiempo que se muestra cada habitación
float time_accumulator; // Acumulador de tiempo para el modo demo
int room_index; // Índice para el vector de habitaciones
std::vector<std::string> rooms; // Listado con los mapas de la demo
// Constructor por defecto
DemoData()
: counter(0),
room_time(0),
: time_accumulator(0.0F),
room_index(0),
rooms({}) {}
// Constructor parametrizado
DemoData(int counter, int room_time, int room_index, const std::vector<std::string>& rooms)
: counter(counter),
room_time(room_time),
DemoData(float time_accumulator, int room_index, const std::vector<std::string>& rooms)
: time_accumulator(time_accumulator),
room_index(room_index),
rooms(rooms) {}
};
@@ -54,16 +58,17 @@ class Game {
std::shared_ptr<Surface> room_name_surface_; // Textura para escribir el nombre de la habitación
// Variables
GameMode mode_; // Modo del juego
DemoData demo_; // Variables para el modo demo
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
std::string current_room_; // Fichero de la habitación actual
PlayerSpawn spawn_point_; // Lugar de la habitación donde aparece el jugador
bool paused_ = false; // Indica si el juego se encuentra en pausa
bool black_screen_ = false; // Indica si la pantalla está en negro. Se utiliza para la muerte del jugador
int black_screen_counter_ = 0; // Contador para temporizar la pantalla en negro
int total_items_; // Cantidad total de items que hay en el mapeado del juego
SDL_FRect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación
GameMode mode_; // Modo del juego
DemoData demo_; // Variables para el modo demo
DeltaTimer delta_timer_; // Timer para calcular delta time
std::string current_room_; // Fichero de la habitación actual
PlayerSpawn spawn_point_; // Lugar de la habitación donde aparece el jugador
bool paused_ = false; // Indica si el juego se encuentra en pausa
bool black_screen_ = false; // Indica si la pantalla está en negro. Se utiliza para la muerte del jugador
float black_screen_time_ = 0.0F; // Tiempo acumulado en pantalla negra en segundos
int total_items_; // Cantidad total de items que hay en el mapeado del juego
SDL_FRect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación
float jail_restore_time_ = 0.0F; // Tiempo acumulado para restauración de vidas en la Jail
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();
@@ -116,7 +121,7 @@ class Game {
void setBlackScreen();
// Actualiza las variables relativas a la pantalla en negro
void updateBlackScreen();
void updateBlackScreen(float delta_time);
// Dibuja la pantalla negra
void renderBlackScreen() const;
@@ -134,7 +139,7 @@ class Game {
void togglePause();
// Da vidas al jugador cuando está en la Jail
void checkRestoringJail();
void checkRestoringJail(float delta_time);
// Inicializa el diccionario de las estadísticas
void initStats();
@@ -161,7 +166,7 @@ class Game {
void demoInit();
// DEMO MODE: Comprueba si se ha de cambiar de habitación
void demoCheckRoomChange();
void demoCheckRoomChange(float delta_time);
public:
// Constructor