corregits bugs de toquetechar vectors i merdes i passats a lists

This commit is contained in:
2025-10-20 12:29:37 +02:00
parent 2b4523d644
commit c8bf9640cf
5 changed files with 62 additions and 63 deletions

View File

@@ -158,8 +158,9 @@ void BalloonManager::deployFormation(int formation_id, float y) {
// Vacia del vector de globos los globos que ya no sirven // Vacia del vector de globos los globos que ya no sirven
void BalloonManager::freeBalloons() { void BalloonManager::freeBalloons() {
auto result = std::ranges::remove_if(balloons_, [](const auto& balloon) { return !balloon->isEnabled(); }); std::erase_if(balloons_, [](const auto& balloon) {
balloons_.erase(result.begin(), balloons_.end()); return !balloon->isEnabled();
});
} }
// Actualiza el timer de despliegue de globos (time-based) // Actualiza el timer de despliegue de globos (time-based)
@@ -194,40 +195,40 @@ auto BalloonManager::createBalloon(Balloon::Config config) -> std::shared_ptr<Ba
} }
// Crea un globo a partir de otro globo // Crea un globo a partir de otro globo
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon>& balloon, const std::string& direction) { void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon>& parent_balloon, const std::string& direction) {
if (can_deploy_balloons_) { if (can_deploy_balloons_) {
// Calcula parametros // Calcula parametros
const int PARENT_HEIGHT = balloon->getHeight(); const int PARENT_HEIGHT = parent_balloon->getHeight();
const int CHILD_HEIGHT = Balloon::WIDTH.at(static_cast<int>(balloon->getSize()) - 1); const int CHILD_HEIGHT = Balloon::WIDTH.at(static_cast<size_t>(parent_balloon->getSize()) - 1);
const int CHILD_WIDTH = CHILD_HEIGHT; 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 MIN_X = play_area_.x;
const float MAX_X = play_area_.w - CHILD_WIDTH; const float MAX_X = play_area_.w - CHILD_WIDTH;
Balloon::Config config = { Balloon::Config config = {
.x = std::clamp(X - (CHILD_WIDTH / 2), MIN_X, MAX_X), .x = std::clamp(X - (CHILD_WIDTH / 2), MIN_X, MAX_X),
.y = balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2), .y = parent_balloon->getPosY() + ((PARENT_HEIGHT - CHILD_HEIGHT) / 2),
.type = balloon->getType(), .type = parent_balloon->getType(),
.size = static_cast<Balloon::Size>(static_cast<int>(balloon->getSize()) - 1), .size = static_cast<Balloon::Size>(static_cast<int>(parent_balloon->getSize()) - 1),
.vel_x = direction == "LEFT" ? Balloon::VELX_NEGATIVE : Balloon::VELX_POSITIVE, .vel_x = direction == "LEFT" ? Balloon::VELX_NEGATIVE : Balloon::VELX_POSITIVE,
.game_tempo = balloon_speed_, .game_tempo = balloon_speed_,
.creation_counter = 0}; .creation_counter = 0};
// Crea el globo // Crea el globo hijo
auto b = createBalloon(config); auto child_balloon = createBalloon(config);
// Configura el globo hijo
if (child_balloon != nullptr) {
// Establece parametros // Establece parametros
constexpr float VEL_Y_BALLOON_PER_S = -150.0F; constexpr float VEL_Y_BALLOON_PER_S = -150.0F;
switch (b->getType()) { switch (child_balloon->getType()) {
case Balloon::Type::BALLOON: { case Balloon::Type::BALLOON: {
b->setVelY(VEL_Y_BALLOON_PER_S); child_balloon->setVelY(VEL_Y_BALLOON_PER_S);
break; break;
} }
case Balloon::Type::FLOATER: { case Balloon::Type::FLOATER: {
const float MODIFIER = (rand() % 2 == 0) ? 1.0F : 1.0F; child_balloon->setVelY(Balloon::VELX_NEGATIVE * 2.0F);
b->setVelY(Balloon::VELX_NEGATIVE * 2.0F * MODIFIER);
(rand() % 2 == 0) ? b->alterVelX(1.0F) : b->alterVelX(1.0F);
break; break;
} }
default: default:
@@ -235,8 +236,9 @@ void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon>& balloon,
} }
// Herencia de estados // Herencia de estados
if (balloon->isStopped()) { b->stop(); } if (parent_balloon->isStopped()) { child_balloon->stop(); }
if (balloon->isUsingReversedColor()) { b->useReverseColor(); } if (parent_balloon->isUsingReversedColor()) { child_balloon->useReverseColor(); }
}
} }
} }

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h> // Para SDL_FRect #include <SDL3/SDL.h> // Para SDL_FRect
#include <array> // Para array #include <array> // Para array
#include <list> // Para list
#include <memory> // Para shared_ptr, unique_ptr #include <memory> // Para shared_ptr, unique_ptr
#include <string> // Para basic_string, string #include <string> // Para basic_string, string
#include <vector> // Para vector #include <vector> // Para vector
@@ -17,7 +18,7 @@ class IStageInfo;
class Texture; class Texture;
// --- Types --- // --- Types ---
using Balloons = std::vector<std::shared_ptr<Balloon>>; using Balloons = std::list<std::shared_ptr<Balloon>>;
// --- Clase BalloonManager: gestiona todos los globos del juego --- // --- Clase BalloonManager: gestiona todos los globos del juego ---
class BalloonManager { class BalloonManager {

View File

@@ -5,11 +5,12 @@
#include <functional> // Para function #include <functional> // Para function
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <vector> // Para vector #include <vector> // Para vector
#include <list> // Para list
#include "bullet.hpp" // for Bullet #include "bullet.hpp" // for Bullet
// --- Types --- // --- Types ---
using Bullets = std::vector<std::shared_ptr<Bullet>>; using Bullets = std::list<std::shared_ptr<Bullet>>;
// --- Clase BulletManager: gestiona todas las balas del juego --- // --- Clase BulletManager: gestiona todas las balas del juego ---
// //

View File

@@ -357,7 +357,7 @@ void Game::updateGameStateGameOver(float delta_time) {
updatePathSprites(delta_time); updatePathSprites(delta_time);
updateTimeStopped(delta_time); updateTimeStopped(delta_time);
bullet_manager_->checkCollisions(); bullet_manager_->checkCollisions();
cleanVectors(); cleanLists();
if (game_over_timer_ < GAME_OVER_DURATION_S) { if (game_over_timer_ < GAME_OVER_DURATION_S) {
game_over_timer_ += delta_time; // Incremento time-based primero game_over_timer_ += delta_time; // Incremento time-based primero
@@ -391,7 +391,7 @@ void Game::updateGameStateCompleted(float delta_time) {
updateItems(delta_time); updateItems(delta_time);
updateSmartSprites(delta_time); updateSmartSprites(delta_time);
updatePathSprites(delta_time); updatePathSprites(delta_time);
cleanVectors(); cleanLists();
// Maneja eventos del juego completado // Maneja eventos del juego completado
handleGameCompletedEvents(); handleGameCompletedEvents();
@@ -684,16 +684,18 @@ void Game::createItem(ItemType type, float x, float y) {
// Vacia el vector de items // Vacia el vector de items
void Game::freeItems() { void Game::freeItems() {
if (!items_.empty()) { std::erase_if(items_, [&](const auto& item) {
for (int i = items_.size() - 1; i >= 0; --i) { if (!item->isEnabled()) {
if (!items_[i]->isEnabled()) { // Comprobamos si hay que realizar una acción extra
if (items_[i]->getType() == ItemType::COFFEE_MACHINE) { if (item->getType() == ItemType::COFFEE_MACHINE) {
coffee_machine_enabled_ = false; coffee_machine_enabled_ = false;
} }
items_.erase(items_.begin() + i); // Devolvemos 'true' para indicar que este elemento debe ser borrado.
} return true;
}
} }
// Devolvemos 'false' para conservarlo.
return false;
});
} }
// Crea un objeto PathSprite // Crea un objeto PathSprite
@@ -730,26 +732,18 @@ void Game::createMessage(const std::vector<Path>& paths, const std::shared_ptr<T
path_sprites_.back()->enable(); path_sprites_.back()->enable();
} }
// Vacia el vector de smartsprites // Vacia la lista de smartsprites
void Game::freeSmartSprites() { void Game::freeSmartSprites() {
if (!smart_sprites_.empty()) { std::erase_if(smart_sprites_, [](const auto& sprite) {
for (int i = smart_sprites_.size() - 1; i >= 0; --i) { return sprite->hasFinished();
if (smart_sprites_[i]->hasFinished()) { });
smart_sprites_.erase(smart_sprites_.begin() + i);
}
}
}
} }
// Vacia el vector de pathsprites // Vacia la lista de pathsprites
void Game::freePathSprites() { void Game::freePathSprites() {
if (!path_sprites_.empty()) { std::erase_if(path_sprites_, [](const auto& sprite) {
for (int i = path_sprites_.size() - 1; i >= 0; --i) { return sprite->hasFinished();
if (path_sprites_[i]->hasFinished()) { });
path_sprites_.erase(path_sprites_.begin() + i);
}
}
}
} }
// Crea un SpriteSmart para arrojar el item café al recibir un impacto // Crea un SpriteSmart para arrojar el item café al recibir un impacto
@@ -1822,11 +1816,11 @@ void Game::updateGameStatePlaying(float delta_time) {
updateMenace(); updateMenace();
checkAndUpdateBalloonSpeed(); checkAndUpdateBalloonSpeed();
checkState(); checkState();
cleanVectors(); cleanLists();
} }
// Vacía los vectores de elementos deshabilitados // Vacía los vectores de elementos deshabilitados
void Game::cleanVectors() { void Game::cleanLists() {
bullet_manager_->freeBullets(); bullet_manager_->freeBullets();
balloon_manager_->freeBalloons(); balloon_manager_->freeBalloons();
freeItems(); freeItems();

View File

@@ -5,6 +5,7 @@
#include <memory> // Para shared_ptr, unique_ptr #include <memory> // Para shared_ptr, unique_ptr
#include <string> // Para string #include <string> // Para string
#include <vector> // Para vector #include <vector> // Para vector
#include <list> // Para list
#include "bullet.hpp" // for Bullet #include "bullet.hpp" // for Bullet
#include "demo.hpp" // for Demo #include "demo.hpp" // for Demo
@@ -121,9 +122,9 @@ class Game {
SDL_Texture* canvas_; // Textura para dibujar la zona de juego SDL_Texture* canvas_; // Textura para dibujar la zona de juego
std::vector<std::shared_ptr<Player>> players_; // Vector con los jugadores std::vector<std::shared_ptr<Player>> players_; // Vector con los jugadores
std::vector<std::unique_ptr<Item>> items_; // Vector con los items std::list<std::unique_ptr<Item>> items_; // Vector con los items
std::vector<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites std::list<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites
std::vector<std::unique_ptr<PathSprite>> path_sprites_; // Vector con los pathsprites std::list<std::unique_ptr<PathSprite>> path_sprites_; // Vector con los pathsprites
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
std::vector<std::vector<std::shared_ptr<Texture>>> player_textures_; // Vector con todas las texturas de los jugadores std::vector<std::vector<std::shared_ptr<Texture>>> 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 handleEvents(); // Procesa los eventos del sistema en cola
void checkState(); // Verifica y actualiza el estado actual del juego void checkState(); // Verifica y actualiza el estado actual del juego
void setState(State state); // Cambia el estado 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 --- // --- Gestión de estados del juego ---
void updateGameStates(float delta_time); // Actualiza todos los estados del juego void updateGameStates(float delta_time); // Actualiza todos los estados del juego