From 2db2ba420a3da63eec9b666786420fa4f49b75b8 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 23 Jul 2025 17:12:56 +0200 Subject: [PATCH] game_logo: readability-function-cognitive-complexity --- source/game_logo.cpp | 194 ++++++++++++++++++++++++---------------- source/game_logo.h | 25 +++++- source/sections/title.h | 44 +++++---- 3 files changed, 167 insertions(+), 96 deletions(-) diff --git a/source/game_logo.cpp b/source/game_logo.cpp index 0335572..a8e3c7a 100644 --- a/source/game_logo.cpp +++ b/source/game_logo.cpp @@ -111,98 +111,138 @@ void GameLogo::render() { // Actualiza la lógica de la clase void GameLogo::update() { + updateCoffeeCrisis(); + updateArcadeEdition(); + updatePostFinishedCounter(); +} + +void GameLogo::updateCoffeeCrisis() { switch (coffee_crisis_status_) { - case Status::MOVING: { - coffee_sprite_->update(); - crisis_sprite_->update(); - - // Si los objetos han llegado a su destino, cambia el estado - if (coffee_sprite_->hasFinished() && crisis_sprite_->hasFinished()) { - coffee_crisis_status_ = Status::SHAKING; - - // Reproduce el efecto sonoro - Audio::get()->playSound("title.wav"); - Screen::get()->flash(Color(0xFF, 0xFF, 0xFF), FLASH_LENGHT, FLASH_DELAY); - Screen::get()->shake(); - } - + case Status::MOVING: + handleCoffeeCrisisMoving(); break; - } - - case Status::SHAKING: { - // Agita "COFFEE CRISIS" - if (shake_.remaining > 0) { - if (shake_.counter > 0) { - shake_.counter--; - } else { - shake_.counter = shake_.delay; - const auto DESP = shake_.remaining % 2 == 0 ? shake_.desp * (-1) : shake_.desp; - coffee_sprite_->setPosX(shake_.origin + DESP); - crisis_sprite_->setPosX(shake_.origin + DESP + 15); - shake_.remaining--; - } - } else { - coffee_sprite_->setPosX(shake_.origin); - crisis_sprite_->setPosX(shake_.origin + 15); - coffee_crisis_status_ = Status::FINISHED; - arcade_edition_status_ = Status::MOVING; - } - - dust_right_sprite_->update(); - dust_left_sprite_->update(); - + case Status::SHAKING: + handleCoffeeCrisisShaking(); break; - } - - case Status::FINISHED: { - dust_right_sprite_->update(); - dust_left_sprite_->update(); - + case Status::FINISHED: + handleCoffeeCrisisFinished(); break; - } - default: break; } +} +void GameLogo::updateArcadeEdition() { switch (arcade_edition_status_) { - case Status::MOVING: { - zoom_ -= 0.1F * ZOOM_FACTOR; - arcade_edition_sprite_->setZoom(zoom_); - if (zoom_ <= 1.0F) { - arcade_edition_status_ = Status::SHAKING; - zoom_ = 1.0F; - arcade_edition_sprite_->setZoom(zoom_); - shake_.init(1, 2, 8, arcade_edition_sprite_->getX()); - Audio::get()->playSound("title.wav"); - Screen::get()->flash(Color(0xFF, 0xFF, 0xFF), FLASH_LENGHT, FLASH_DELAY); - Screen::get()->shake(); - } + case Status::MOVING: + handleArcadeEditionMoving(); break; - } - - case Status::SHAKING: { - // Agita "ARCADE EDITION" - if (shake_.remaining > 0) { - if (shake_.counter > 0) { - shake_.counter--; - } else { - shake_.counter = shake_.delay; - const auto DESP = shake_.remaining % 2 == 0 ? shake_.desp * (-1) : shake_.desp; - arcade_edition_sprite_->setX(shake_.origin + DESP); - shake_.remaining--; - } - } else { - arcade_edition_sprite_->setX(shake_.origin); - arcade_edition_status_ = Status::FINISHED; - } + case Status::SHAKING: + handleArcadeEditionShaking(); break; - } - default: break; } +} +void GameLogo::handleCoffeeCrisisMoving() { + coffee_sprite_->update(); + crisis_sprite_->update(); + + if (coffee_sprite_->hasFinished() && crisis_sprite_->hasFinished()) { + coffee_crisis_status_ = Status::SHAKING; + playTitleEffects(); + } +} + +void GameLogo::handleCoffeeCrisisShaking() { + if (shake_.remaining > 0) { + processShakeEffect(coffee_sprite_.get(), crisis_sprite_.get()); + } else { + finishCoffeeCrisisShaking(); + } + + updateDustSprites(); +} + +void GameLogo::handleCoffeeCrisisFinished() { + updateDustSprites(); +} + +void GameLogo::handleArcadeEditionMoving() { + zoom_ -= 0.1F * ZOOM_FACTOR; + arcade_edition_sprite_->setZoom(zoom_); + + if (zoom_ <= 1.0F) { + finishArcadeEditionMoving(); + } +} + +void GameLogo::handleArcadeEditionShaking() { + if (shake_.remaining > 0) { + processArcadeEditionShake(); + } else { + arcade_edition_sprite_->setX(shake_.origin); + arcade_edition_status_ = Status::FINISHED; + } +} + +void GameLogo::processShakeEffect(SmartSprite* primary_sprite, SmartSprite* secondary_sprite) { + if (shake_.counter > 0) { + shake_.counter--; + } else { + shake_.counter = shake_.delay; + const auto displacement = calculateShakeDisplacement(); + primary_sprite->setPosX(shake_.origin + displacement); + if (secondary_sprite) { + secondary_sprite->setPosX(shake_.origin + displacement + 15); + } + shake_.remaining--; + } +} + +void GameLogo::processArcadeEditionShake() { + if (shake_.counter > 0) { + shake_.counter--; + } else { + shake_.counter = shake_.delay; + const auto displacement = calculateShakeDisplacement(); + arcade_edition_sprite_->setX(shake_.origin + displacement); + shake_.remaining--; + } +} + +int GameLogo::calculateShakeDisplacement() const { + return shake_.remaining % 2 == 0 ? shake_.desp * (-1) : shake_.desp; +} + +void GameLogo::finishCoffeeCrisisShaking() { + coffee_sprite_->setPosX(shake_.origin); + crisis_sprite_->setPosX(shake_.origin + 15); + coffee_crisis_status_ = Status::FINISHED; + arcade_edition_status_ = Status::MOVING; +} + +void GameLogo::finishArcadeEditionMoving() { + arcade_edition_status_ = Status::SHAKING; + zoom_ = 1.0F; + arcade_edition_sprite_->setZoom(zoom_); + shake_.init(1, 2, 8, arcade_edition_sprite_->getX()); + playTitleEffects(); +} + +void GameLogo::playTitleEffects() { + Audio::get()->playSound("title.wav"); + Screen::get()->flash(Color(0xFF, 0xFF, 0xFF), FLASH_LENGHT, FLASH_DELAY); + Screen::get()->shake(); +} + +void GameLogo::updateDustSprites() { + dust_right_sprite_->update(); + dust_left_sprite_->update(); +} + +void GameLogo::updatePostFinishedCounter() { if (coffee_crisis_status_ == Status::FINISHED && arcade_edition_status_ == Status::FINISHED && post_finished_counter_ > 0) { diff --git a/source/game_logo.h b/source/game_logo.h index 6efe8fa..b09e7b0 100644 --- a/source/game_logo.h +++ b/source/game_logo.h @@ -76,7 +76,30 @@ class GameLogo { Status arcade_edition_status_ = Status::DISABLED; // Estado de "ARCADE EDITION" Shake shake_; // Efecto de agitación - // --- Métodos internos --- + // --- Inicialización --- void init(); // Inicializa las variables [[nodiscard]] auto getInitialVerticalDesp() const -> int; // Calcula el desplazamiento vertical inicial + + // --- Actualización de estados específicos --- + void updateCoffeeCrisis(); // Actualiza el estado de "Coffee Crisis" + void updateArcadeEdition(); // Actualiza el estado de "Arcade Edition" + void updatePostFinishedCounter(); // Actualiza el contador tras finalizar una animación + + // --- Efectos visuales: movimiento y sacudidas --- + void handleCoffeeCrisisMoving(); // Maneja el movimiento de "Coffee Crisis" + void handleCoffeeCrisisShaking(); // Maneja la sacudida de "Coffee Crisis" + void handleArcadeEditionMoving(); // Maneja el movimiento de "Arcade Edition" + void handleArcadeEditionShaking(); // Maneja la sacudida de "Arcade Edition" + void processShakeEffect(SmartSprite* primary_sprite, SmartSprite* secondary_sprite = nullptr); // Procesa el efecto de sacudida en sprites + void processArcadeEditionShake(); // Procesa la sacudida específica de "Arcade Edition" + int calculateShakeDisplacement() const; // Calcula el desplazamiento de la sacudida + + // --- Gestión de finalización de efectos --- + void handleCoffeeCrisisFinished(); // Maneja el final de la animación "Coffee Crisis" + void finishCoffeeCrisisShaking(); // Finaliza la sacudida de "Coffee Crisis" + void finishArcadeEditionMoving(); // Finaliza el movimiento de "Arcade Edition" + + // --- Utilidades --- + void playTitleEffects(); // Reproduce efectos visuales/sonoros del título + void updateDustSprites(); // Actualiza los sprites de polvo }; \ No newline at end of file diff --git a/source/sections/title.h b/source/sections/title.h index c80878b..95753a6 100644 --- a/source/sections/title.h +++ b/source/sections/title.h @@ -77,26 +77,15 @@ class Title { // -- Variables de diseño --- Anchor anchor_; // Anclas para definir la posición de los elementos del titulo - // --- Métodos internos --- - // --- Métodos internos --- - void update(); // Actualiza las variables del objeto - void render(); // Dibuja el objeto en pantalla + // --- Ciclo de vida del título --- + void update(); // Actualiza las variables del objeto + void updateState(); // Actualiza el estado actual del título + void setState(TitleState state); // Cambia el estado del título + void resetCounter(); // Reinicia el contador interno + + // --- Entrada de usuario --- void checkEvents(); // Comprueba los eventos void checkInput(); // Comprueba las entradas - void resetCounter(); // Reinicia el contador interno - static void swapControllers(); // Intercambia la asignación de mandos a los jugadores - static void swapKeyboard(); // Intercambia el teclado de jugador - static void showControllers(); // Muestra información sobre los controles y los jugadores - void updateFade(); // Actualiza el efecto de fundido (fade in/out) - void updateState(); // Actualiza el estado actual del título - void updateStartPrompt(); // Actualiza el mensaje de "Pulsa Start" - void renderStartPrompt(); // Dibuja el mensaje de "Pulsa Start" en pantalla - void renderCopyright(); // Dibuja el aviso de copyright - void setState(TitleState state); // Cambia el estado del título - void initPlayers(); // Inicializa los jugadores - void renderPlayers(); // Renderiza los jugadores - void updatePlayers(); // Actualiza los jugadores - auto getPlayer(int id) -> std::shared_ptr; // Obtiene un jugador a partir de su "id" void handleKeyDownEvent(const SDL_Event& event); // Maneja el evento de tecla presionada void handleControlKeys(SDL_Keycode key); // Maneja las teclas de control específicas bool shouldSkipInputCheck() const; // Determina si se debe omitir la comprobación de entrada @@ -108,6 +97,25 @@ class Title { void processPlayer2Start(); // Procesa el inicio del jugador 2 void activatePlayerAndSetState(int player_id); // Activa al jugador y cambia el estado del título + // --- Gestión de jugadores --- + void initPlayers(); // Inicializa los jugadores + void updatePlayers(); // Actualiza los jugadores + void renderPlayers(); // Renderiza los jugadores + auto getPlayer(int id) -> std::shared_ptr; // Obtiene un jugador a partir de su "id" + + // --- Visualización / Renderizado --- + void render(); // Dibuja el objeto en pantalla + void updateFade(); // Actualiza el efecto de fundido (fade in/out) + void updateStartPrompt(); // Actualiza el mensaje de "Pulsa Start" + void renderStartPrompt(); // Dibuja el mensaje de "Pulsa Start" en pantalla + void renderCopyright(); // Dibuja el aviso de copyright + + // --- Utilidades estáticas --- + static void swapControllers(); // Intercambia la asignación de mandos a los jugadores + static void swapKeyboard(); // Intercambia el teclado de jugador + static void showControllers(); // Muestra información sobre los controles y los jugadores + +// --- Depuración (solo en modo DEBUG) --- #ifdef _DEBUG void handleDebugColorKeys(SDL_Keycode key); // Maneja las teclas de depuración para colores void adjustColorComponent(SDL_Keycode key, Color& color); // Ajusta un componente del color según la tecla