magic numbers: title.cpp

This commit is contained in:
2025-09-17 13:53:31 +02:00
parent 9acd9aa631
commit ae30c9b34f
8 changed files with 87 additions and 124 deletions

View File

@@ -42,7 +42,7 @@ Title::Title()
tiled_bg_(std::make_unique<TiledBG>(param.game.game_area.rect, TiledBGMode::RANDOM)),
game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)),
mini_logo_sprite_(std::make_unique<Sprite>(Resource::get()->getTexture("logo_jailgames_mini.png"))),
state_(TitleState::LOGO_ANIMATING),
state_(State::LOGO_ANIMATING),
num_controllers_(Input::get()->getNumGamepads()) {
// Configura objetos
tiled_bg_->setColor(param.title.bg_color);
@@ -60,16 +60,16 @@ Title::Title()
Section::attract_mode = IS_TITLE_TO_DEMO ? Section::AttractMode::TITLE_TO_LOGO : Section::AttractMode::TITLE_TO_DEMO;
// Define los anclajes de los elementos
anchor_.mini_logo = (param.game.height / 5 * 4) + BLOCK;
anchor_.mini_logo = (param.game.height / MINI_LOGO_Y_DIVISOR * MINI_LOGO_Y_FACTOR) + BLOCK;
mini_logo_sprite_->setY(anchor_.mini_logo);
anchor_.copyright_text = anchor_.mini_logo + mini_logo_sprite_->getHeight() + 3;
anchor_.copyright_text = anchor_.mini_logo + mini_logo_sprite_->getHeight() + COPYRIGHT_TEXT_SPACING;
}
// Destructor
Title::~Title() {
Audio::get()->stopAllSounds();
if (Section::name == Section::Name::LOGO) {
Audio::get()->fadeOutMusic(300);
Audio::get()->fadeOutMusic(MUSIC_FADE_OUT_SHORT_MS);
}
// Desregistra los jugadores de Options
@@ -77,32 +77,17 @@ Title::~Title() {
Options::gamepad_manager.clearPlayers();
}
// Actualiza las variables del objeto (frame-based)
void Title::update() {
if (SDL_GetTicks() - last_time_ > param.game.speed) {
last_time_ = SDL_GetTicks();
Screen::get()->update();
updateFade();
updateState();
updateStartPrompt();
updatePlayers();
}
Audio::update();
}
// Actualiza las variables del objeto (time-based)
// Actualiza las variables del objeto
void Title::update(float deltaTime) {
Screen::get()->update();
updateFade();
updateState(deltaTime);
updateStartPrompt();
for (auto& player : players_) {
player->update(deltaTime); // deltaTime ya está en segundos
player->update(deltaTime);
}
Audio::update();
}
@@ -292,7 +277,7 @@ void Title::handleStartButtonPress(const Options::Gamepad* controller) {
}
auto Title::canProcessStartButton() const -> bool {
return (state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP);
return (state_ != State::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP);
}
void Title::processPlayer1Start() {
@@ -311,7 +296,7 @@ void Title::processPlayer2Start() {
void Title::activatePlayerAndSetState(Player::Id player_id) {
getPlayer(player_id)->setPlayingState(Player::State::TITLE_ANIMATION);
setState(TitleState::START_HAS_BEEN_PRESSED);
setState(State::START_HAS_BEEN_PRESSED);
counter_time_ = 0.0f;
}
@@ -392,59 +377,22 @@ void Title::updateFade() {
}
// Actualiza el estado
void Title::updateState() {
// Establece la lógica según el estado
switch (state_) {
case TitleState::LOGO_ANIMATING: {
game_logo_->update(); // Mantener frame-based para consistencia del estado
if (game_logo_->hasFinished()) {
setState(TitleState::LOGO_FINISHED);
}
break;
}
case TitleState::LOGO_FINISHED: {
// Ya no se usa counter_ aquí, se usa updateState(deltaTime)
game_logo_->update();
tiled_bg_->update();
// Esta lógica se movió a updateState(deltaTime)
break;
}
case TitleState::START_HAS_BEEN_PRESSED: {
// Ya no se usa counter_ aquí, se usa updateState(deltaTime)
game_logo_->update();
tiled_bg_->update();
// Esta lógica se movió a updateState(deltaTime)
break;
}
default:
break;
}
}
// Actualiza el estado (time-based)
void Title::updateState(float deltaTime) {
// deltaTime ya está en segundos desde calculateDeltaTime()
game_logo_->update(deltaTime);
tiled_bg_->update(deltaTime);
// Establece la lógica según el estado
switch (state_) {
case TitleState::LOGO_ANIMATING: {
case State::LOGO_ANIMATING: {
if (game_logo_->hasFinished()) {
setState(TitleState::LOGO_FINISHED);
setState(State::LOGO_FINISHED);
}
break;
}
case TitleState::LOGO_FINISHED: {
counter_time_ += deltaTime; // deltaTime está en milisegundos
// param.title.title_duration está en frames (60fps), convertir a ms: frames * (1000ms/60fps)
float duration_ms = static_cast<float>(param.title.title_duration) * (1000.0f / 60.0f);
if (counter_time_ >= duration_ms) {
case State::LOGO_FINISHED: {
counter_time_ += deltaTime;
if (counter_time_ >= param.title.title_duration) {
// El menu ha hecho time out
fade_->setPostDuration(0);
fade_->activate();
@@ -452,11 +400,10 @@ void Title::updateState(float deltaTime) {
}
break;
}
case TitleState::START_HAS_BEEN_PRESSED: {
counter_time_ += deltaTime; // deltaTime está en milisegundos
// 100 frames a 60fps convertir a ms: 100 * (1000/60) = 1666.67 ms
if (counter_time_ >= (100.0f * 1000.0f / 60.0f)) {
case State::START_HAS_BEEN_PRESSED: {
counter_time_ += deltaTime;
if (counter_time_ >= START_PRESSED_DELAY_MS) {
fade_->activate();
}
break;
@@ -468,22 +415,16 @@ void Title::updateState(float deltaTime) {
}
void Title::updateStartPrompt() {
constexpr Uint32 LOGO_BLINK_PERIOD = 833; // milisegundos
constexpr Uint32 LOGO_BLINK_ON_TIME = 583; // 833 - 250
constexpr Uint32 START_BLINK_PERIOD = 167;
constexpr Uint32 START_BLINK_ON_TIME = 83; // 167 - 83
Uint32 time_ms = SDL_GetTicks();
bool condition_met = false;
switch (state_) {
case TitleState::LOGO_FINISHED:
condition_met = (time_ms % LOGO_BLINK_PERIOD) >= (LOGO_BLINK_PERIOD - LOGO_BLINK_ON_TIME);
case State::LOGO_FINISHED:
condition_met = (time_ms % LOGO_BLINK_PERIOD_MS) >= (LOGO_BLINK_PERIOD_MS - LOGO_BLINK_ON_TIME_MS);
break;
case TitleState::START_HAS_BEEN_PRESSED:
condition_met = (time_ms % START_BLINK_PERIOD) >= (START_BLINK_PERIOD - START_BLINK_ON_TIME);
case State::START_HAS_BEEN_PRESSED:
condition_met = (time_ms % START_BLINK_PERIOD_MS) >= (START_BLINK_PERIOD_MS - START_BLINK_ON_TIME_MS);
break;
default:
@@ -507,7 +448,7 @@ void Title::renderStartPrompt() {
}
void Title::renderCopyright() {
if (state_ != TitleState::LOGO_ANIMATING) {
if (state_ != State::LOGO_ANIMATING) {
// Mini logo
mini_logo_sprite_->render();
@@ -524,20 +465,20 @@ void Title::renderCopyright() {
}
// Cambia el estado
void Title::setState(TitleState state) {
void Title::setState(State state) {
if (state_ == state) {
return;
}
state_ = state;
switch (state_) {
case TitleState::LOGO_ANIMATING:
case State::LOGO_ANIMATING:
break;
case TitleState::LOGO_FINISHED:
case State::LOGO_FINISHED:
Audio::get()->playMusic("title.ogg");
break;
case TitleState::START_HAS_BEEN_PRESSED:
Audio::get()->fadeOutMusic(1500);
case State::START_HAS_BEEN_PRESSED:
Audio::get()->fadeOutMusic(MUSIC_FADE_OUT_LONG_MS);
break;
}
}
@@ -606,13 +547,6 @@ void Title::initPlayers() {
}
}
// Actualiza los jugadores
void Title::updatePlayers() {
for (auto& player : players_) {
player->update();
}
}
// Renderiza los jugadores
void Title::renderPlayers() {
for (auto const& player : players_) {