From 877d54c555eb9dadc5d59d7f1c4f75eb6a4c1d23 Mon Sep 17 00:00:00 2001 From: Sergio Date: Wed, 23 Jul 2025 16:53:30 +0200 Subject: [PATCH] Title: clang-tidy readability-function-cognitive-complexity --- source/sections/game.cpp | 24 ++- source/sections/game.h | 4 +- source/sections/intro.cpp | 6 +- source/sections/intro.h | 12 +- source/sections/title.cpp | 300 +++++++++++++++++++++----------------- source/sections/title.h | 60 +++++--- 6 files changed, 235 insertions(+), 171 deletions(-) diff --git a/source/sections/game.cpp b/source/sections/game.cpp index d2e1ae8..de3d3e9 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -496,7 +496,9 @@ void Game::checkPlayerItemCollision(std::shared_ptr &player) { // Comprueba y procesa la colisión de las balas void Game::checkBulletCollision() { for (auto &bullet : bullets_) { - if (!bullet->isEnabled()) continue; + if (!bullet->isEnabled()) { + continue; + } if (checkBulletTabeCollision(bullet)) { break; // Exit early if bullet hit Tabe @@ -509,8 +511,10 @@ void Game::checkBulletCollision() { } // Maneja la colisión entre bala y Tabe -bool Game::checkBulletTabeCollision(std::shared_ptr bullet) { - if (!tabe_->isEnabled()) return false; +auto Game::checkBulletTabeCollision(std::shared_ptr bullet) -> bool { + if (!tabe_->isEnabled()) { + return false; + } if (!checkCollision(bullet->getCollider(), tabe_->getCollider())) { return false; @@ -539,11 +543,15 @@ void Game::handleTabeHitEffects() { } // Maneja la colisión entre bala y globos -bool Game::checkBulletBalloonCollision(std::shared_ptr bullet) { +auto Game::checkBulletBalloonCollision(std::shared_ptr bullet) -> bool { for (auto &balloon : balloon_manager_->getBalloons()) { - if (!balloon->isEnabled() || balloon->isInvulnerable()) continue; + if (!balloon->isEnabled() || balloon->isInvulnerable()) { + continue; + } - if (!checkCollision(balloon->getCollider(), bullet->getCollider())) continue; + if (!checkCollision(balloon->getCollider(), bullet->getCollider())) { + continue; + } processBalloonHit(bullet, balloon); return true; @@ -564,7 +572,9 @@ void Game::processBalloonHit(std::shared_ptr bullet, std::shared_ptr balloon, std::shared_ptr player) { const auto DROPPED_ITEM = dropItem(); - if (DROPPED_ITEM == ItemType::NONE || demo_.recording) return; + if (DROPPED_ITEM == ItemType::NONE || demo_.recording) { + return; + } if (DROPPED_ITEM != ItemType::COFFEE_MACHINE) { createItem(DROPPED_ITEM, balloon->getPosX(), balloon->getPosY()); diff --git a/source/sections/game.h b/source/sections/game.h index 55f406a..afc66a9 100644 --- a/source/sections/game.h +++ b/source/sections/game.h @@ -237,9 +237,9 @@ class Game { void setState(GameState state); // Cambia el estado del juego void movePlayersToFront(); // Organiza los jugadores para que los vivos se pinten sobre los muertos void checkServiceMenu(); // Comprueba si está activo el menu de servicio para poner el juego en pausa - bool checkBulletTabeCollision(std::shared_ptr bullet); + auto checkBulletTabeCollision(std::shared_ptr bullet) -> bool; void handleTabeHitEffects(); - bool checkBulletBalloonCollision(std::shared_ptr bullet); + auto checkBulletBalloonCollision(std::shared_ptr bullet) -> bool; void processBalloonHit(std::shared_ptr bullet, std::shared_ptr balloon); void handleItemDrop(std::shared_ptr balloon, std::shared_ptr player); void handleBalloonDestruction(std::shared_ptr balloon, std::shared_ptr player); diff --git a/source/sections/intro.cpp b/source/sections/intro.cpp index dc9d324..25eafbc 100644 --- a/source/sections/intro.cpp +++ b/source/sections/intro.cpp @@ -214,9 +214,9 @@ void Intro::enableCardAndShadow(int index) { shadow_sprites_.at(index)->enable(); } -void Intro::switchText(int fromIndex, int toIndex) { - texts_.at(fromIndex)->setEnabled(false); - texts_.at(toIndex)->setEnabled(true); +void Intro::switchText(int from_index, int to_index) { + texts_.at(from_index)->setEnabled(false); + texts_.at(to_index)->setEnabled(true); } // Actualiza las variables del objeto diff --git a/source/sections/intro.h b/source/sections/intro.h index e6cdc68..38cd7da 100644 --- a/source/sections/intro.h +++ b/source/sections/intro.h @@ -1,10 +1,10 @@ #pragma once #include // Para SDL_Keycode, Uint32, Uint64 -#include // Para uint8_t -#include // Para unique_ptr -#include // Para vector +#include // Para uint8_t +#include // Para unique_ptr +#include // Para vector #include "color.h" // Para Color #include "param.h" // Para Param, ParamIntro, param @@ -71,10 +71,10 @@ class Intro { static void renderTextRect(); // Dibuja el rectangulo de fondo del texto; void updatePostState(); // Actualiza el estado POST #ifdef _DEBUG - void adjustColorComponent(uint8_t& component, bool increase); + static void adjustColorComponent(uint8_t& component, bool increase); void adjustAllColorComponents(Color& color, bool increase); void handleDebugColorKeys(SDL_Keycode key, Color& color); - void printColorDebugInfo(const Color& color); + static void printColorDebugInfo(const Color& color); #endif // --- Métodos para manejar cada escena individualmente --- @@ -87,5 +87,5 @@ class Intro { // --- Métodos auxiliares para reducir duplicación de código --- void enableCardAndShadow(int index); - void switchText(int fromIndex, int toIndex); + void switchText(int from_index, int to_index); }; diff --git a/source/sections/title.cpp b/source/sections/title.cpp index da4ddf7..c8b1327 100644 --- a/source/sections/title.cpp +++ b/source/sections/title.cpp @@ -108,160 +108,190 @@ void Title::render() { void Title::checkEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { -#ifdef _DEBUG - if (event.type == SDL_EVENT_KEY_DOWN && static_cast(event.key.repeat) == 1) { - static Color color_ = param.title.bg_color; - switch (event.key.key) { - case SDLK_A: - if (color_.r < 255) { - ++color_.r; - } - break; - - case SDLK_Z: - if (color_.r > 0) { - --color_.r; - } - break; - - case SDLK_S: - if (color_.g < 255) { - ++color_.g; - } - break; - - case SDLK_X: - if (color_.g > 0) { - --color_.g; - } - break; - - case SDLK_D: - if (color_.b < 255) { - ++color_.b; - } - break; - - case SDLK_C: - if (color_.b > 0) { - --color_.b; - } - break; - - case SDLK_F: - if (color_.r < 255) { - ++color_.r; - } - if (color_.g < 255) { - ++color_.g; - } - if (color_.b < 255) { - ++color_.b; - } - break; - - case SDLK_V: - if (color_.r > 0) { - --color_.r; - } - if (color_.g > 0) { - --color_.g; - } - if (color_.b > 0) { - --color_.b; - } - break; - - default: - break; - } - counter_ = 0; - tiled_bg_->setColor(color_); - std::cout << "#" - << std::hex << std::setw(2) << std::setfill('0') << (int)color_.r - << std::setw(2) << std::setfill('0') << (int)color_.g - << std::setw(2) << std::setfill('0') << (int)color_.b - << std::endl; + if (event.type == SDL_EVENT_KEY_DOWN) { + handleKeyDownEvent(event); } -#endif - if (event.type == SDL_EVENT_KEY_DOWN && static_cast(event.key.repeat) == 0) { - switch (event.key.key) { - case SDLK_1: // Redefine los botones del mando #0 - define_buttons_->enable(0); - resetCounter(); - break; - - case SDLK_2: // Redefine los botones del mando #1 - define_buttons_->enable(1); - resetCounter(); - break; - - case SDLK_3: // Intercambia los mandos entre los dos jugadores - swapControllers(); - resetCounter(); - break; - - case SDLK_4: // Intercambia la asignación del teclado - swapKeyboard(); - resetCounter(); - break; - - case SDLK_5: // Muestra la asignación de mandos y teclado - showControllers(); - resetCounter(); - break; - - default: - break; - } - } - + GlobalEvents::check(event); define_buttons_->checkEvents(event); } } +void Title::handleKeyDownEvent(const SDL_Event& event) { + bool isFirstPress = static_cast(event.key.repeat) == 0; + if (isFirstPress) { + handleControlKeys(event.key.key); + } +#ifdef _DEBUG + bool isRepeat = static_cast(event.key.repeat) == 1; + if (isRepeat) { + handleDebugColorKeys(event.key.key); + } +#endif +} + +#ifdef _DEBUG +void Title::handleDebugColorKeys(SDL_Keycode key) { + static Color color_ = param.title.bg_color; + + adjustColorComponent(key, color_); + + counter_ = 0; + tiled_bg_->setColor(color_); + printColorValue(color_); +} + +void Title::adjustColorComponent(SDL_Keycode key, Color& color) { + switch (key) { + case SDLK_A: incrementColorComponent(color.r); break; + case SDLK_Z: decrementColorComponent(color.r); break; + case SDLK_S: incrementColorComponent(color.g); break; + case SDLK_X: decrementColorComponent(color.g); break; + case SDLK_D: incrementColorComponent(color.b); break; + case SDLK_C: decrementColorComponent(color.b); break; + case SDLK_F: incrementAllComponents(color); break; + case SDLK_V: decrementAllComponents(color); break; + default: break; + } +} + +void Title::incrementColorComponent(uint8_t& component) { + if (component < 255) { + ++component; + } +} + +void Title::decrementColorComponent(uint8_t& component) { + if (component > 0) { + --component; + } +} + +void Title::incrementAllComponents(Color& color) { + incrementColorComponent(color.r); + incrementColorComponent(color.g); + incrementColorComponent(color.b); +} + +void Title::decrementAllComponents(Color& color) { + decrementColorComponent(color.r); + decrementColorComponent(color.g); + decrementColorComponent(color.b); +} + +void Title::printColorValue(const Color& color) { + std::cout << "#" + << std::hex << std::setw(2) << std::setfill('0') << (int)color.r + << std::setw(2) << std::setfill('0') << (int)color.g + << std::setw(2) << std::setfill('0') << (int)color.b + << std::endl; +} +#endif + +void Title::handleControlKeys(SDL_Keycode key) { + switch (key) { + case SDLK_1: + define_buttons_->enable(0); + resetCounter(); + break; + + case SDLK_2: + define_buttons_->enable(1); + resetCounter(); + break; + + case SDLK_3: + swapControllers(); + resetCounter(); + break; + + case SDLK_4: + swapKeyboard(); + resetCounter(); + break; + + case SDLK_5: + showControllers(); + resetCounter(); + break; + + default: + break; + } +} + // Comprueba las entradas void Title::checkInput() { - // Comprueba las entradas solo si no se estan definiendo los botones - if (define_buttons_->isEnabled()) { + if (shouldSkipInputCheck()) { return; } Input::get()->update(); if (!ServiceMenu::get()->isEnabled()) { - // Comprueba todos los métodos de control - for (const auto &controller : Options::controllers) { - // Boton START - if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index)) { - if ((state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP)) { - if (controller.player_id == 1) { - if (!player1_start_pressed_) { - player1_start_pressed_ = true; - getPlayer(1)->setPlayingState(PlayerState::TITLE_ANIMATION); - setState(TitleState::START_HAS_BEEN_PRESSED); - counter_ = 0; - } - } - - if (controller.player_id == 2) { - if (!player2_start_pressed_) { - player2_start_pressed_ = true; - getPlayer(2)->setPlayingState(PlayerState::TITLE_ANIMATION); - setState(TitleState::START_HAS_BEEN_PRESSED); - counter_ = 0; - } - } - } - } - } + processControllerInputs(); } - // Comprueba los inputs que se pueden introducir en cualquier sección del juego GlobalInputs::check(); } +bool Title::shouldSkipInputCheck() const { + return define_buttons_->isEnabled(); +} + +void Title::processControllerInputs() { + for (const auto &controller : Options::controllers) { + if (isStartButtonPressed(controller)) { + handleStartButtonPress(controller); + } + } +} + +bool Title::isStartButtonPressed(const Options::GamepadOptions &controller) const { + return Input::get()->checkInput( + InputAction::START, + INPUT_DO_NOT_ALLOW_REPEAT, + controller.type, + controller.index + ); +} + +void Title::handleStartButtonPress(const Options::GamepadOptions &controller) { + if (!canProcessStartButton()) { + return; + } + + if (controller.player_id == 1) { + processPlayer1Start(); + } else if (controller.player_id == 2) { + processPlayer2Start(); + } +} + +bool Title::canProcessStartButton() const { + return (state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP); +} + +void Title::processPlayer1Start() { + if (!player1_start_pressed_) { + player1_start_pressed_ = true; + activatePlayerAndSetState(1); + } +} + +void Title::processPlayer2Start() { + if (!player2_start_pressed_) { + player2_start_pressed_ = true; + activatePlayerAndSetState(2); + } +} + +void Title::activatePlayerAndSetState(int player_id) { + getPlayer(player_id)->setPlayingState(PlayerState::TITLE_ANIMATION); + setState(TitleState::START_HAS_BEEN_PRESSED); + counter_ = 0; +} + // Bucle para el titulo del juego void Title::run() { while (Section::name == Section::Name::TITLE) { diff --git a/source/sections/title.h b/source/sections/title.h index 82763d2..c80878b 100644 --- a/source/sections/title.h +++ b/source/sections/title.h @@ -1,10 +1,13 @@ #pragma once #include // Para Uint32 + #include // Para unique_ptr, shared_ptr #include #include +#include "color.h" // Para Color +#include "options.h" // Para Options #include "section.hpp" // Para Options class DefineButtons; @@ -75,22 +78,43 @@ class Title { Anchor anchor_; // Anclas para definir la posición de los elementos del titulo // --- Métodos internos --- - void update(); // Actualiza las variables del objeto - void render(); // Dibuja el objeto en pantalla - 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(); // Actualza los jugadores - auto getPlayer(int id) -> std::shared_ptr; // Obtiene un jugador a partir de su "id" + // --- Métodos internos --- + void update(); // Actualiza las variables del objeto + void render(); // Dibuja el objeto en pantalla + 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 + void processControllerInputs(); // Procesa las entradas de los mandos + bool isStartButtonPressed(const Options::GamepadOptions& controller) const; // Comprueba si se ha pulsado el botón Start + void handleStartButtonPress(const Options::GamepadOptions& controller); // Maneja la pulsación del botón Start + bool canProcessStartButton() const; // Verifica si se puede procesar la pulsación del botón Start + void processPlayer1Start(); // Procesa el inicio del jugador 1 + void processPlayer2Start(); // Procesa el inicio del jugador 2 + void activatePlayerAndSetState(int player_id); // Activa al jugador y cambia el estado del título + +#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 + void incrementColorComponent(uint8_t& component); // Incrementa un componente de color + void decrementColorComponent(uint8_t& component); // Decrementa un componente de color + void incrementAllComponents(Color& color); // Incrementa todos los componentes del color + void decrementAllComponents(Color& color); // Decrementa todos los componentes del color + void printColorValue(const Color& color); // Imprime el valor actual del color en consola +#endif }; \ No newline at end of file