revisat tabe.cpp, item.cpp i game.cpp

This commit is contained in:
2025-09-24 09:37:23 +02:00
parent 6a223b68ba
commit 2977869ab5
7 changed files with 96 additions and 93 deletions

View File

@@ -79,7 +79,7 @@ Game::Game(Player::Id player_id, int current_stage, bool demo)
scoreboard_ = Scoreboard::get();
fade_in_->setColor(param.fade.color);
fade_in_->setPreDuration(demo_.enabled ? DEMO_FADE_PRE_DURATION_MS : 0);
fade_in_->setPreDuration(demo_.enabled ? static_cast<int>(DEMO_FADE_PRE_DURATION_S * 1000) : 0);
fade_in_->setPostDuration(0);
fade_in_->setType(Fade::Type::RANDOM_SQUARE2);
fade_in_->setMode(Fade::Mode::IN);
@@ -327,13 +327,13 @@ void Game::updateGameStateGameOver(float deltaTime) {
checkBulletCollision();
cleanVectors();
if (game_over_timer_ < GAME_OVER_DURATION_MS) {
if (game_over_timer_ < GAME_OVER_DURATION_S) {
handleGameOverEvents(); // Maneja eventos al inicio
game_over_timer_ += deltaTime; // Incremento time-based
constexpr float FADE_TRIGGER_MS = GAME_OVER_DURATION_MS - (150.0f * (1000.0f / 60.0f)); // 2500ms antes del final
if (game_over_timer_ >= FADE_TRIGGER_MS && !fade_out_->isEnabled()) {
constexpr float FADE_TRIGGER_S = GAME_OVER_DURATION_S - 2.5f; // 2.5 segundos antes del final
if (game_over_timer_ >= FADE_TRIGGER_S && !fade_out_->isEnabled()) {
fade_out_->activate();
}
}
@@ -783,10 +783,10 @@ void Game::throwCoffee(int x, int y) {
smart_sprites_.back()->setPosY(y - 8);
smart_sprites_.back()->setWidth(param.game.item_size);
smart_sprites_.back()->setHeight(param.game.item_size);
smart_sprites_.back()->setVelX(-1.0F + ((rand() % 5) * 0.5F));
smart_sprites_.back()->setVelY(-4.0F);
smart_sprites_.back()->setVelX((-1.0F + ((rand() % 5) * 0.5F)) * 60.0f); // Convertir a pixels/segundo
smart_sprites_.back()->setVelY(-4.0F * 60.0f); // Convertir a pixels/segundo
smart_sprites_.back()->setAccelX(0.0F);
smart_sprites_.back()->setAccelY(0.2F);
smart_sprites_.back()->setAccelY(0.2F * 60.0f); // Convertir a pixels/segundo²
smart_sprites_.back()->setDestX(x + (smart_sprites_.back()->getVelX() * 50));
smart_sprites_.back()->setDestY(param.game.height + 1);
smart_sprites_.back()->setEnabled(true);
@@ -859,23 +859,23 @@ void Game::handlePlayerCollision(std::shared_ptr<Player> &player, std::shared_pt
// Actualiza el estado del tiempo detenido
void Game::updateTimeStopped(float deltaTime) {
static constexpr float WARNING_THRESHOLD_MS = 2000.0f; // 120 frames a 60fps
static constexpr float CLOCK_SOUND_INTERVAL_MS = 500.0f; // 30 frames a 60fps
static constexpr float COLOR_FLASH_INTERVAL_MS = 250.0f; // 15 frames a 60fps
static constexpr float WARNING_THRESHOLD_S = 2.0f; // 120 frames a 60fps → segundos
static constexpr float CLOCK_SOUND_INTERVAL_S = 0.5f; // 30 frames a 60fps → segundos
static constexpr float COLOR_FLASH_INTERVAL_S = 0.25f; // 15 frames a 60fps → segundos
if (time_stopped_timer_ > 0) {
time_stopped_timer_ -= deltaTime;
// Fase de advertencia (últimos 2 segundos)
if (time_stopped_timer_ <= WARNING_THRESHOLD_MS) {
if (time_stopped_timer_ <= WARNING_THRESHOLD_S) {
static float last_sound_time = 0.0f;
last_sound_time += deltaTime;
if (last_sound_time >= CLOCK_SOUND_INTERVAL_MS) {
if (last_sound_time >= CLOCK_SOUND_INTERVAL_S) {
balloon_manager_->normalColorsToAllBalloons();
playSound("clock.wav");
last_sound_time = 0.0f;
} else if (last_sound_time >= COLOR_FLASH_INTERVAL_MS) {
} else if (last_sound_time >= COLOR_FLASH_INTERVAL_S) {
balloon_manager_->reverseColorsToAllBalloons();
playSound("clock.wav");
}
@@ -883,7 +883,7 @@ void Game::updateTimeStopped(float deltaTime) {
// Fase normal - solo sonido ocasional
static float sound_timer = 0.0f;
sound_timer += deltaTime;
if (sound_timer >= CLOCK_SOUND_INTERVAL_MS) {
if (sound_timer >= CLOCK_SOUND_INTERVAL_S) {
playSound("clock.wav");
sound_timer = 0.0f;
}
@@ -978,7 +978,7 @@ void Game::fillCanvas() {
void Game::enableTimeStopItem() {
balloon_manager_->stopAllBalloons();
balloon_manager_->reverseColorsToAllBalloons();
time_stopped_timer_ = TIME_STOPPED_DURATION_MS;
time_stopped_timer_ = TIME_STOPPED_DURATION_S;
}
// Deshabilita el efecto del item de detener el tiempo
@@ -988,12 +988,12 @@ void Game::disableTimeStopItem() {
balloon_manager_->normalColorsToAllBalloons();
}
// Calcula el deltatime
// Calcula el deltatime en segundos
auto Game::calculateDeltaTime() -> float {
const Uint64 current_time = SDL_GetTicks();
const float delta_time = static_cast<float>(current_time - last_time_);
const float delta_time_ms = static_cast<float>(current_time - last_time_);
last_time_ = current_time;
return delta_time;
return delta_time_ms / 1000.0f; // Convertir de milisegundos a segundos
}
// Bucle para el juego
@@ -1731,10 +1731,17 @@ void Game::updateGameStateShowingGetReadyMessage(float deltaTime) {
if (path_sprites_.empty()) {
setState(State::PLAYING);
}
if (counter_ == 100) {
playMusic();
// Reproducir música después de ~1.67 segundos (100 frames a 60fps)
static bool music_started = false;
static float music_timer = 0.0f;
if (!music_started) {
music_timer += deltaTime;
if (music_timer >= 1.67f) {
playMusic();
music_started = true;
}
}
++counter_;
}
// Actualiza las variables durante el transcurso normal del juego
@@ -1895,12 +1902,12 @@ void Game::onPauseStateChanged(bool is_paused) {
// Maneja eventos del juego completado usando flags para triggers únicos
void Game::handleGameCompletedEvents() {
constexpr float START_CELEBRATIONS_MS = 6667.0f; // 400 frames a 60fps
constexpr float END_CELEBRATIONS_MS = 11667.0f; // 700 frames a 60fps
constexpr float START_CELEBRATIONS_S = 6.667f; // 400 frames a 60fps → segundos
constexpr float END_CELEBRATIONS_S = 11.667f; // 700 frames a 60fps → segundos
// Inicio de celebraciones
static bool start_celebrations_triggered = false;
if (!start_celebrations_triggered && game_completed_timer_ >= START_CELEBRATIONS_MS) {
if (!start_celebrations_triggered && game_completed_timer_ >= START_CELEBRATIONS_S) {
createMessage({paths_.at(4), paths_.at(5)}, Resource::get()->getTexture("game_text_congratulations"));
createMessage({paths_.at(6), paths_.at(7)}, Resource::get()->getTexture("game_text_1000000_points"));
@@ -1919,7 +1926,7 @@ void Game::handleGameCompletedEvents() {
// Fin de celebraciones
static bool end_celebrations_triggered = false;
if (!end_celebrations_triggered && game_completed_timer_ >= END_CELEBRATIONS_MS) {
if (!end_celebrations_triggered && game_completed_timer_ >= END_CELEBRATIONS_S) {
for (auto &player : players_) {
if (player->isCelebrating()) {
player->setPlayingState(player->qualifiesForHighScore() ? Player::State::ENTERING_NAME_GAME_COMPLETED : Player::State::LEAVING_SCREEN);