Revisant la classe Balloon

This commit is contained in:
2024-10-26 11:38:08 +02:00
parent d44bfd51de
commit f99f908c11
6 changed files with 415 additions and 449 deletions

View File

@@ -241,15 +241,16 @@ void Game::deployBalloonFormation()
last_balloon_deploy_ = set;
const Stage stage = balloon_formations_->getStage(current_stage_);
const auto numEnemies = stage.balloon_pool->set[set]->number_of_balloons;
const auto numEnemies = stage.balloon_pool.set[set].number_of_balloons;
for (int i = 0; i < numEnemies; ++i)
{
createBalloon(stage.balloon_pool->set[set]->init[i].x,
stage.balloon_pool->set[set]->init[i].y,
stage.balloon_pool->set[set]->init[i].kind,
stage.balloon_pool->set[set]->init[i].vel_x,
createBalloon(stage.balloon_pool.set[set].init[i].x,
stage.balloon_pool.set[set].init[i].y,
stage.balloon_pool.set[set].init[i].type,
stage.balloon_pool.set[set].init[i].size,
stage.balloon_pool.set[set].init[i].vel_x,
balloon_speed_,
stage.balloon_pool->set[set]->init[i].creation_counter);
stage.balloon_pool.set[set].init[i].creation_counter);
}
balloon_deploy_counter_ = 300;
@@ -432,10 +433,10 @@ void Game::renderBalloons()
}
// Crea un globo nuevo en el vector de globos
std::shared_ptr<Balloon> Game::createBalloon(float x, int y, int kind, float velx, float speed, int creation_timer)
std::shared_ptr<Balloon> Game::createBalloon(float x, int y, BalloonType type, BalloonSize size, float velx, float speed, int creation_timer)
{
const auto index = (kind - 1) % 4;
balloons_.emplace_back(std::make_shared<Balloon>(x, y, kind, velx, speed, creation_timer, balloon_textures_[index], balloon_animations_[index]));
const int index = static_cast<int>(size);
balloons_.emplace_back(std::make_shared<Balloon>(x, y, type, size, velx, speed, creation_timer, balloon_textures_.at(index), balloon_animations_.at(index)));
return balloons_.back();
}
@@ -443,21 +444,18 @@ std::shared_ptr<Balloon> Game::createBalloon(float x, int y, int kind, float vel
void Game::createPowerBall()
{
constexpr auto values = 6;
constexpr auto posY = -BLOCK;
constexpr auto pos_y = -BLOCK;
constexpr int creation_time = 300;
const auto left = param.game.play_area.rect.x;
const auto center = param.game.play_area.center_x - (BALLOON_WIDTH_4 / 2);
const auto right = param.game.play_area.rect.w - BALLOON_WIDTH_4;
const auto vel_pos = BALLOON_VELX_POSITIVE;
const auto vel_neg = BALLOON_VELX_NEGATIVE;
const auto luck = rand() % values;
const int x[values] = {left, left, center, center, right, right};
const float vx[values] = {vel_pos, vel_pos, vel_pos, vel_neg, vel_neg, vel_neg};
const float vx[values] = {BALLOON_VELX_POSITIVE, BALLOON_VELX_POSITIVE, BALLOON_VELX_POSITIVE, BALLOON_VELX_NEGATIVE, BALLOON_VELX_NEGATIVE, BALLOON_VELX_NEGATIVE};
auto b = std::make_unique<Balloon>(x[luck], posY, POWER_BALL, vx[luck], balloon_speed_, 300, balloon_textures_[4], balloon_animations_[4]);
balloons_.push_back(std::move(b));
balloons_.emplace_back(std::make_unique<Balloon>(x[luck], pos_y, BalloonType::POWERBALL, BalloonSize::SIZE4, vx[luck], balloon_speed_, creation_time, balloon_textures_[4], balloon_animations_[4]));
power_ball_enabled_ = true;
power_ball_counter_ = POWERBALL_COUNTER;
@@ -516,8 +514,7 @@ void Game::popBalloon(std::shared_ptr<Balloon> balloon)
increaseStageCurrentPower(1);
balloons_popped_++;
const auto kind = balloon->getKind();
if (kind == POWER_BALL)
if (balloon->getType() == BalloonType::POWERBALL)
{
destroyAllBalloons();
power_ball_enabled_ = false;
@@ -525,21 +522,24 @@ void Game::popBalloon(std::shared_ptr<Balloon> balloon)
}
else
{
const auto size = balloon->getSize();
if (size == BALLOON_SIZE_1)
{ // Si es del tipo más pequeño, simplemente elimina el globo
const int size = static_cast<int>(balloon->getSize());
if (size == static_cast<int>(BalloonSize::SIZE1))
{
// Si es del tipo más pequeño, simplemente elimina el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), size);
balloon->pop();
}
else
{ // En cualquier otro caso, crea dos globos de un tipo inferior
auto balloon_left = createBalloon(0, balloon->getPosY(), balloon->getKind() - 1, BALLOON_VELX_NEGATIVE, balloon_speed_, 0);
{
const auto lower_size = static_cast<BalloonSize>(size - 1);
// En cualquier otro caso, crea dos globos de un tipo inferior
auto balloon_left = createBalloon(0, balloon->getPosY(), balloon->getType(), lower_size, BALLOON_VELX_NEGATIVE, balloon_speed_, 0);
balloon_left->allignTo(balloon->getPosX() + (balloon->getWidth() / 2));
balloon_left->setVelY(balloon_left->getClass() == BALLOON_CLASS ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f);
balloon_left->setVelY(balloon_left->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f);
auto balloon_right = createBalloon(0, balloon->getPosY(), balloon->getKind() - 1, BALLOON_VELX_POSITIVE, balloon_speed_, 0);
auto balloon_right = createBalloon(0, balloon->getPosY(), balloon->getType(), lower_size, BALLOON_VELX_POSITIVE, balloon_speed_, 0);
balloon_right->allignTo(balloon->getPosX() + (balloon->getWidth() / 2));
balloon_right->setVelY(balloon_right->getClass() == BALLOON_CLASS ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f);
balloon_right->setVelY(balloon_right->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f);
// Elimina el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), size);
@@ -554,42 +554,31 @@ void Game::popBalloon(std::shared_ptr<Balloon> balloon)
// Explosiona un globo. Lo destruye = no crea otros globos
void Game::destroyBalloon(std::shared_ptr<Balloon> &balloon)
{
auto score = 0;
int score = 0;
// Calcula la puntuación y el poder que generaria el globo en caso de romperlo a él y a sus hijos
const auto size = balloon->getSize();
switch (size)
{
case BALLOON_SIZE_4:
switch (balloon->getSize())
{
case BalloonSize::SIZE4:
score = BALLOON_SCORE_4 + (2 * BALLOON_SCORE_3) + (4 * BALLOON_SCORE_2) + (8 * BALLOON_SCORE_1);
break;
}
case BALLOON_SIZE_3:
{
case BalloonSize::SIZE3:
score = BALLOON_SCORE_3 + (2 * BALLOON_SCORE_2) + (4 * BALLOON_SCORE_1);
break;
}
case BALLOON_SIZE_2:
{
case BalloonSize::SIZE2:
score = BALLOON_SCORE_2 + (2 * BALLOON_SCORE_1);
break;
}
case BALLOON_SIZE_1:
{
case BalloonSize::SIZE1:
score = BALLOON_SCORE_1;
break;
}
default:
{
score = 0;
break;
}
}
// Otorga los puntos correspondientes al globo
for (auto &player : players_)
@@ -604,7 +593,7 @@ void Game::destroyBalloon(std::shared_ptr<Balloon> &balloon)
balloons_popped_ += power;
// Destruye el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), size);
explosions_->add(balloon->getPosX(), balloon->getPosY(), static_cast<int>(balloon->getSize()));
balloon->pop();
// Recalcula el nivel de amenaza
@@ -1705,17 +1694,12 @@ void Game::checkEvents()
// Crea dos BALLON4
case SDLK_2:
{
const auto set = 0;
const auto stage = balloon_formations_->getStage(0);
const auto numEnemies = stage.balloon_pool->set[set]->number_of_balloons;
const auto set = balloon_formations_->getStage(0).balloon_pool.set[0];
const auto numEnemies = set.number_of_balloons;
for (int i = 0; i < numEnemies; ++i)
{
createBalloon(stage.balloon_pool->set[set]->init[i].x,
stage.balloon_pool->set[set]->init[i].y,
stage.balloon_pool->set[set]->init[i].kind,
stage.balloon_pool->set[set]->init[i].vel_x,
balloon_speed_,
stage.balloon_pool->set[set]->init[i].creation_counter);
auto p = set.init[i];
createBalloon(p.x, p.y, p.type, p.size, p.vel_x, balloon_speed_, p.creation_counter);
}
}
break;