diff --git a/source/balloon_manager.cpp b/source/balloon_manager.cpp index 2fd5e07..2b9c9d6 100644 --- a/source/balloon_manager.cpp +++ b/source/balloon_manager.cpp @@ -158,8 +158,9 @@ void BalloonManager::deployFormation(int formation_id, float y) { // Vacia del vector de globos los globos que ya no sirven void BalloonManager::freeBalloons() { - auto result = std::ranges::remove_if(balloons_, [](const auto& balloon) { return !balloon->isEnabled(); }); - balloons_.erase(result.begin(), balloons_.end()); + std::erase_if(balloons_, [](const auto& balloon) { + return !balloon->isEnabled(); + }); } // Actualiza el timer de despliegue de globos (time-based) @@ -194,49 +195,50 @@ auto BalloonManager::createBalloon(Balloon::Config config) -> std::shared_ptr& balloon, const std::string& direction) { +void BalloonManager::createChildBalloon(const std::shared_ptr& parent_balloon, const std::string& direction) { if (can_deploy_balloons_) { // Calcula parametros - const int PARENT_HEIGHT = balloon->getHeight(); - const int CHILD_HEIGHT = Balloon::WIDTH.at(static_cast(balloon->getSize()) - 1); + const int PARENT_HEIGHT = parent_balloon->getHeight(); + const int CHILD_HEIGHT = Balloon::WIDTH.at(static_cast(parent_balloon->getSize()) - 1); const int CHILD_WIDTH = CHILD_HEIGHT; - const float X = direction == "LEFT" ? balloon->getPosX() + (balloon->getWidth() / 3) : balloon->getPosX() + (2 * (balloon->getWidth() / 3)); + const float X = direction == "LEFT" ? parent_balloon->getPosX() + (parent_balloon->getWidth() / 3) : parent_balloon->getPosX() + (2 * (parent_balloon->getWidth() / 3)); const float MIN_X = play_area_.x; const float MAX_X = play_area_.w - CHILD_WIDTH; Balloon::Config config = { .x = std::clamp(X - (CHILD_WIDTH / 2), MIN_X, MAX_X), - .y = balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2), - .type = balloon->getType(), - .size = static_cast(static_cast(balloon->getSize()) - 1), + .y = parent_balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2), + .type = parent_balloon->getType(), + .size = static_cast(static_cast(parent_balloon->getSize()) - 1), .vel_x = direction == "LEFT" ? Balloon::VELX_NEGATIVE : Balloon::VELX_POSITIVE, .game_tempo = balloon_speed_, .creation_counter = 0}; - // Crea el globo - auto b = createBalloon(config); + // Crea el globo hijo + auto child_balloon = createBalloon(config); - // Establece parametros - constexpr float VEL_Y_BALLOON_PER_S = -150.0F; - switch (b->getType()) { - case Balloon::Type::BALLOON: { - b->setVelY(VEL_Y_BALLOON_PER_S); - break; + // Configura el globo hijo + if (child_balloon != nullptr) { + // Establece parametros + constexpr float VEL_Y_BALLOON_PER_S = -150.0F; + switch (child_balloon->getType()) { + case Balloon::Type::BALLOON: { + child_balloon->setVelY(VEL_Y_BALLOON_PER_S); + break; + } + case Balloon::Type::FLOATER: { + child_balloon->setVelY(Balloon::VELX_NEGATIVE * 2.0F); + break; + } + default: + break; } - case Balloon::Type::FLOATER: { - const float MODIFIER = (rand() % 2 == 0) ? 1.0F : 1.0F; - b->setVelY(Balloon::VELX_NEGATIVE * 2.0F * MODIFIER); - (rand() % 2 == 0) ? b->alterVelX(1.0F) : b->alterVelX(1.0F); - break; - } - default: - break; + + // Herencia de estados + if (parent_balloon->isStopped()) { child_balloon->stop(); } + if (parent_balloon->isUsingReversedColor()) { child_balloon->useReverseColor(); } } - - // Herencia de estados - if (balloon->isStopped()) { b->stop(); } - if (balloon->isUsingReversedColor()) { b->useReverseColor(); } } } diff --git a/source/balloon_manager.hpp b/source/balloon_manager.hpp index 813980b..0666054 100644 --- a/source/balloon_manager.hpp +++ b/source/balloon_manager.hpp @@ -3,6 +3,7 @@ #include // Para SDL_FRect #include // Para array +#include // Para list #include // Para shared_ptr, unique_ptr #include // Para basic_string, string #include // Para vector @@ -17,7 +18,7 @@ class IStageInfo; class Texture; // --- Types --- -using Balloons = std::vector>; +using Balloons = std::list>; // --- Clase BalloonManager: gestiona todos los globos del juego --- class BalloonManager { diff --git a/source/bullet_manager.hpp b/source/bullet_manager.hpp index 556f6a0..d4467e3 100644 --- a/source/bullet_manager.hpp +++ b/source/bullet_manager.hpp @@ -5,11 +5,12 @@ #include // Para function #include // Para shared_ptr #include // Para vector +#include // Para list #include "bullet.hpp" // for Bullet // --- Types --- -using Bullets = std::vector>; +using Bullets = std::list>; // --- Clase BulletManager: gestiona todas las balas del juego --- // diff --git a/source/sections/game.cpp b/source/sections/game.cpp index e052532..517548f 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -357,7 +357,7 @@ void Game::updateGameStateGameOver(float delta_time) { updatePathSprites(delta_time); updateTimeStopped(delta_time); bullet_manager_->checkCollisions(); - cleanVectors(); + cleanLists(); if (game_over_timer_ < GAME_OVER_DURATION_S) { game_over_timer_ += delta_time; // Incremento time-based primero @@ -391,7 +391,7 @@ void Game::updateGameStateCompleted(float delta_time) { updateItems(delta_time); updateSmartSprites(delta_time); updatePathSprites(delta_time); - cleanVectors(); + cleanLists(); // Maneja eventos del juego completado handleGameCompletedEvents(); @@ -684,16 +684,18 @@ void Game::createItem(ItemType type, float x, float y) { // Vacia el vector de items void Game::freeItems() { - if (!items_.empty()) { - for (int i = items_.size() - 1; i >= 0; --i) { - if (!items_[i]->isEnabled()) { - if (items_[i]->getType() == ItemType::COFFEE_MACHINE) { - coffee_machine_enabled_ = false; - } - items_.erase(items_.begin() + i); + std::erase_if(items_, [&](const auto& item) { + if (!item->isEnabled()) { + // Comprobamos si hay que realizar una acción extra + if (item->getType() == ItemType::COFFEE_MACHINE) { + coffee_machine_enabled_ = false; } + // Devolvemos 'true' para indicar que este elemento debe ser borrado. + return true; } - } + // Devolvemos 'false' para conservarlo. + return false; + }); } // Crea un objeto PathSprite @@ -730,26 +732,18 @@ void Game::createMessage(const std::vector& paths, const std::shared_ptrenable(); } -// Vacia el vector de smartsprites +// Vacia la lista de smartsprites void Game::freeSmartSprites() { - if (!smart_sprites_.empty()) { - for (int i = smart_sprites_.size() - 1; i >= 0; --i) { - if (smart_sprites_[i]->hasFinished()) { - smart_sprites_.erase(smart_sprites_.begin() + i); - } - } - } + std::erase_if(smart_sprites_, [](const auto& sprite) { + return sprite->hasFinished(); + }); } -// Vacia el vector de pathsprites +// Vacia la lista de pathsprites void Game::freePathSprites() { - if (!path_sprites_.empty()) { - for (int i = path_sprites_.size() - 1; i >= 0; --i) { - if (path_sprites_[i]->hasFinished()) { - path_sprites_.erase(path_sprites_.begin() + i); - } - } - } + std::erase_if(path_sprites_, [](const auto& sprite) { + return sprite->hasFinished(); + }); } // Crea un SpriteSmart para arrojar el item café al recibir un impacto @@ -1822,11 +1816,11 @@ void Game::updateGameStatePlaying(float delta_time) { updateMenace(); checkAndUpdateBalloonSpeed(); checkState(); - cleanVectors(); + cleanLists(); } // Vacía los vectores de elementos deshabilitados -void Game::cleanVectors() { +void Game::cleanLists() { bullet_manager_->freeBullets(); balloon_manager_->freeBalloons(); freeItems(); diff --git a/source/sections/game.hpp b/source/sections/game.hpp index 0fb90fa..ef40682 100644 --- a/source/sections/game.hpp +++ b/source/sections/game.hpp @@ -5,6 +5,7 @@ #include // Para shared_ptr, unique_ptr #include // Para string #include // Para vector +#include // Para list #include "bullet.hpp" // for Bullet #include "demo.hpp" // for Demo @@ -121,9 +122,9 @@ class Game { SDL_Texture* canvas_; // Textura para dibujar la zona de juego std::vector> players_; // Vector con los jugadores - std::vector> items_; // Vector con los items - std::vector> smart_sprites_; // Vector con los smartsprites - std::vector> path_sprites_; // Vector con los pathsprites + std::list> items_; // Vector con los items + std::list> smart_sprites_; // Vector con los smartsprites + std::list> path_sprites_; // Vector con los pathsprites std::vector> item_textures_; // Vector con las texturas de los items std::vector>> player_textures_; // Vector con todas las texturas de los jugadores @@ -210,7 +211,7 @@ class Game { void handleEvents(); // Procesa los eventos del sistema en cola void checkState(); // Verifica y actualiza el estado actual del juego void setState(State state); // Cambia el estado del juego - void cleanVectors(); // Limpia vectores de elementos deshabilitados + void cleanLists(); // Limpia vectores de elementos deshabilitados // --- Gestión de estados del juego --- void updateGameStates(float delta_time); // Actualiza todos los estados del juego