magic numbers: game.cpp
This commit is contained in:
@@ -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 ? 500 : 0);
|
||||
fade_in_->setPreDuration(demo_.enabled ? DEMO_FADE_PRE_DURATION_MS : 0);
|
||||
fade_in_->setPostDuration(0);
|
||||
fade_in_->setType(Fade::Type::RANDOM_SQUARE2);
|
||||
fade_in_->setMode(Fade::Mode::IN);
|
||||
@@ -210,43 +210,8 @@ void Game::updateHiScore() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables del jugador (frame-based)
|
||||
void Game::updatePlayers() {
|
||||
for (auto &player : players_) {
|
||||
player->update();
|
||||
|
||||
if (player->isPlaying()) {
|
||||
// Comprueba la colisión entre el jugador y los globos
|
||||
auto balloon = checkPlayerBalloonCollision(player);
|
||||
|
||||
// Si hay colisión
|
||||
if (balloon) {
|
||||
// Si el globo está parado y el temporizador activo, lo explota
|
||||
if (balloon->isStopped() && time_stopped_counter_ > 0) {
|
||||
balloon_manager_->popBalloon(balloon);
|
||||
}
|
||||
// En caso contrario, el jugador ha sido golpeado por un globo activo
|
||||
else {
|
||||
handlePlayerCollision(player, balloon);
|
||||
|
||||
if (demo_.enabled && allPlayersAreNotPlaying()) {
|
||||
fade_out_->setType(Fade::Type::RANDOM_SQUARE2);
|
||||
fade_out_->setPostDuration(500);
|
||||
fade_out_->activate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las colisiones entre el jugador y los items
|
||||
checkPlayerItemCollision(player);
|
||||
}
|
||||
}
|
||||
|
||||
// Organiza la lista de jugadores
|
||||
sortPlayersByZOrder();
|
||||
}
|
||||
|
||||
// Actualiza las variables del jugador (time-based)
|
||||
// Actualiza las variables del jugador
|
||||
void Game::updatePlayers(float deltaTime) {
|
||||
for (auto &player : players_) {
|
||||
player->update(deltaTime);
|
||||
@@ -348,9 +313,9 @@ void Game::updateStage() {
|
||||
}
|
||||
|
||||
// Actualiza el estado de fin de la partida
|
||||
void Game::updateGameStateGameOver() {
|
||||
void Game::updateGameStateGameOver(float deltaTime) {
|
||||
fade_out_->update();
|
||||
updatePlayers();
|
||||
updatePlayers(deltaTime);
|
||||
updateScoreboard();
|
||||
updateBackground();
|
||||
balloon_manager_->update();
|
||||
@@ -359,12 +324,12 @@ void Game::updateGameStateGameOver() {
|
||||
updateItems();
|
||||
updateSmartSprites();
|
||||
updatePathSprites();
|
||||
updateTimeStopped();
|
||||
updateTimeStopped(deltaTime);
|
||||
checkBulletCollision();
|
||||
cleanVectors();
|
||||
|
||||
if (game_over_counter_ > 0) {
|
||||
if (game_over_counter_ == GAME_OVER_COUNTER) {
|
||||
if (game_over_counter_ >= GAME_OVER_DURATION_MS) {
|
||||
createMessage({paths_.at(2), paths_.at(3)}, Resource::get()->getTexture("game_text_game_over"));
|
||||
Audio::get()->fadeOutMusic(1000);
|
||||
balloon_manager_->setBouncingSounds(true);
|
||||
@@ -399,11 +364,11 @@ void Game::updateGameStateGameOver() {
|
||||
}
|
||||
|
||||
// Gestiona eventos para el estado del final del juego
|
||||
void Game::updateGameStateCompleted() {
|
||||
void Game::updateGameStateCompleted(float deltaTime) {
|
||||
constexpr int START_CELEBRATIONS = 400;
|
||||
constexpr int END_CELEBRATIONS = START_CELEBRATIONS + 300;
|
||||
|
||||
updatePlayers();
|
||||
updatePlayers(deltaTime);
|
||||
updateScoreboard();
|
||||
updateBackground();
|
||||
balloon_manager_->update();
|
||||
@@ -923,21 +888,35 @@ void Game::handlePlayerCollision(std::shared_ptr<Player> &player, std::shared_pt
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza y comprueba el valor de la variable
|
||||
void Game::updateTimeStopped() {
|
||||
// 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
|
||||
|
||||
if (time_stopped_counter_ > 0) {
|
||||
time_stopped_counter_--;
|
||||
if (time_stopped_counter_ > 120) {
|
||||
if (static_cast<int>(time_stopped_counter_) % 30 == 0) {
|
||||
time_stopped_counter_ -= deltaTime;
|
||||
|
||||
// Fase de advertencia (últimos 2 segundos)
|
||||
if (time_stopped_counter_ <= WARNING_THRESHOLD_MS) {
|
||||
static float last_sound_time = 0.0f;
|
||||
last_sound_time += deltaTime;
|
||||
|
||||
if (last_sound_time >= CLOCK_SOUND_INTERVAL_MS) {
|
||||
balloon_manager_->normalColorsToAllBalloons();
|
||||
playSound("clock.wav");
|
||||
last_sound_time = 0.0f;
|
||||
} else if (last_sound_time >= COLOR_FLASH_INTERVAL_MS) {
|
||||
balloon_manager_->reverseColorsToAllBalloons();
|
||||
playSound("clock.wav");
|
||||
}
|
||||
} else {
|
||||
if (static_cast<int>(time_stopped_counter_) % 30 == 0) {
|
||||
balloon_manager_->normalColorsToAllBalloons();
|
||||
playSound("clock.wav");
|
||||
} else if (static_cast<int>(time_stopped_counter_) % 30 == 15) {
|
||||
balloon_manager_->reverseColorsToAllBalloons();
|
||||
// Fase normal - solo sonido ocasional
|
||||
static float sound_timer = 0.0f;
|
||||
sound_timer += deltaTime;
|
||||
if (sound_timer >= CLOCK_SOUND_INTERVAL_MS) {
|
||||
playSound("clock.wav");
|
||||
sound_timer = 0.0f;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -945,24 +924,8 @@ void Game::updateTimeStopped() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza toda la lógica del juego (frame-based)
|
||||
void Game::update() {
|
||||
if (SDL_GetTicks() - last_time_ > param.game.speed) {
|
||||
last_time_ = SDL_GetTicks();
|
||||
screen_->update();
|
||||
|
||||
updateDemo();
|
||||
#ifdef RECORDING
|
||||
updateRecording();
|
||||
#endif
|
||||
updateGameStates();
|
||||
fillCanvas();
|
||||
}
|
||||
|
||||
Audio::update();
|
||||
}
|
||||
|
||||
// Actualiza toda la lógica del juego (time-based)
|
||||
// Actualiza toda la lógica del juego
|
||||
void Game::update(float deltaTime) {
|
||||
screen_->update();
|
||||
|
||||
@@ -970,7 +933,7 @@ void Game::update(float deltaTime) {
|
||||
#ifdef RECORDING
|
||||
updateRecording();
|
||||
#endif
|
||||
updateGameStates();
|
||||
updateGameStates(deltaTime);
|
||||
fillCanvas();
|
||||
|
||||
Audio::update();
|
||||
@@ -989,26 +952,26 @@ void Game::render() {
|
||||
}
|
||||
|
||||
// Actualiza los estados del juego
|
||||
void Game::updateGameStates() {
|
||||
void Game::updateGameStates(float deltaTime) {
|
||||
if (!pause_manager_->isPaused()) {
|
||||
switch (state_) {
|
||||
case State::FADE_IN:
|
||||
updateGameStateFadeIn();
|
||||
break;
|
||||
case State::ENTERING_PLAYER:
|
||||
updateGameStateEnteringPlayer();
|
||||
updateGameStateEnteringPlayer(deltaTime);
|
||||
break;
|
||||
case State::SHOWING_GET_READY_MESSAGE:
|
||||
updateGameStateShowingGetReadyMessage();
|
||||
updateGameStateShowingGetReadyMessage(deltaTime);
|
||||
break;
|
||||
case State::PLAYING:
|
||||
updateGameStatePlaying();
|
||||
updateGameStatePlaying(deltaTime);
|
||||
break;
|
||||
case State::COMPLETED:
|
||||
updateGameStateCompleted();
|
||||
updateGameStateCompleted(deltaTime);
|
||||
break;
|
||||
case State::GAME_OVER:
|
||||
updateGameStateGameOver();
|
||||
updateGameStateGameOver(deltaTime);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -1047,7 +1010,7 @@ void Game::fillCanvas() {
|
||||
void Game::enableTimeStopItem() {
|
||||
balloon_manager_->stopAllBalloons();
|
||||
balloon_manager_->reverseColorsToAllBalloons();
|
||||
time_stopped_counter_ = TIME_STOPPED_COUNTER;
|
||||
time_stopped_counter_ = TIME_STOPPED_DURATION_MS;
|
||||
}
|
||||
|
||||
// Deshabilita el efecto del item de detener el tiempo
|
||||
@@ -1780,9 +1743,9 @@ void Game::updateGameStateFadeIn() {
|
||||
}
|
||||
|
||||
// Actualiza las variables durante dicho estado
|
||||
void Game::updateGameStateEnteringPlayer() {
|
||||
void Game::updateGameStateEnteringPlayer(float deltaTime) {
|
||||
balloon_manager_->update();
|
||||
updatePlayers();
|
||||
updatePlayers(deltaTime);
|
||||
updateScoreboard();
|
||||
updateBackground();
|
||||
for (const auto &player : players_) {
|
||||
@@ -1795,8 +1758,8 @@ void Game::updateGameStateEnteringPlayer() {
|
||||
}
|
||||
|
||||
// Actualiza las variables durante dicho estado
|
||||
void Game::updateGameStateShowingGetReadyMessage() {
|
||||
updateGameStatePlaying();
|
||||
void Game::updateGameStateShowingGetReadyMessage(float deltaTime) {
|
||||
updateGameStatePlaying(deltaTime);
|
||||
if (path_sprites_.empty()) {
|
||||
setState(State::PLAYING);
|
||||
}
|
||||
@@ -1807,13 +1770,13 @@ void Game::updateGameStateShowingGetReadyMessage() {
|
||||
}
|
||||
|
||||
// Actualiza las variables durante el transcurso normal del juego
|
||||
void Game::updateGameStatePlaying() {
|
||||
void Game::updateGameStatePlaying(float deltaTime) {
|
||||
#ifdef _DEBUG
|
||||
if (auto_pop_balloons_) {
|
||||
stage_manager_->addPower(5);
|
||||
}
|
||||
#endif
|
||||
updatePlayers();
|
||||
updatePlayers(deltaTime);
|
||||
checkPlayersStatusPlaying();
|
||||
updateScoreboard();
|
||||
updateBackground();
|
||||
@@ -1824,7 +1787,7 @@ void Game::updateGameStatePlaying() {
|
||||
updateStage();
|
||||
updateSmartSprites();
|
||||
updatePathSprites();
|
||||
updateTimeStopped();
|
||||
updateTimeStopped(deltaTime);
|
||||
updateHelper();
|
||||
checkBulletCollision();
|
||||
updateMenace();
|
||||
@@ -2038,4 +2001,53 @@ void Game::handleDebugEvents(const SDL_Event &event) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// Inicio de celebraciones
|
||||
static bool start_celebrations_triggered = false;
|
||||
if (!start_celebrations_triggered && game_completed_counter_ >= START_CELEBRATIONS_MS) {
|
||||
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"));
|
||||
|
||||
for (auto &player : players_) {
|
||||
if (player->isPlaying()) {
|
||||
player->addScore(1000000, Options::settings.hi_score_table.back().score);
|
||||
player->setPlayingState(Player::State::CELEBRATING);
|
||||
} else {
|
||||
player->setPlayingState(Player::State::GAME_OVER);
|
||||
}
|
||||
}
|
||||
|
||||
updateHiScore();
|
||||
start_celebrations_triggered = true;
|
||||
}
|
||||
|
||||
// Fin de celebraciones
|
||||
static bool end_celebrations_triggered = false;
|
||||
if (!end_celebrations_triggered && game_completed_counter_ >= END_CELEBRATIONS_MS) {
|
||||
for (auto &player : players_) {
|
||||
if (player->isCelebrating()) {
|
||||
player->setPlayingState(player->qualifiesForHighScore() ? Player::State::ENTERING_NAME_GAME_COMPLETED : Player::State::LEAVING_SCREEN);
|
||||
}
|
||||
}
|
||||
|
||||
fade_out_->activate();
|
||||
end_celebrations_triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Maneja eventos de game over usando flag para trigger único
|
||||
void Game::handleGameOverEvents() {
|
||||
static bool game_over_triggered = false;
|
||||
if (!game_over_triggered && game_over_counter_ >= GAME_OVER_DURATION_MS) {
|
||||
createMessage({paths_.at(2), paths_.at(3)}, Resource::get()->getTexture("game_text_game_over"));
|
||||
Audio::get()->fadeOutMusic(1000);
|
||||
balloon_manager_->setBouncingSounds(true);
|
||||
game_over_triggered = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user