Continue amb BalloonManager

This commit is contained in:
2024-11-07 20:56:56 +01:00
parent 0e527ff9d9
commit 2fb7e88e4b
12 changed files with 226 additions and 178 deletions

View File

@@ -1,14 +1,17 @@
#include "balloon_manager.h"
#include "stage.h"
#include <stdlib.h> // Para rand
#include <algorithm> // Para remove_if
#include <numeric> // Para accumulate
#include <algorithm> // Para find_if, clamp, min, remove_if
#include "balloon.h" // Para Balloon, BALLOON_SCORE, BALLOON_VE...
#include "balloon_formations.h" // Para BalloonFormations, BalloonFormatio...
#include "resource.h"
#include "game.h"
#include "screen.h"
#include "explosions.h" // Para Explosions
#include "jail_audio.h"
#include "balloon.h" // Para Balloon, BALLOON_SCORE, BALLOON_VELX...
#include "balloon_formations.h" // Para BalloonFormationParams, BalloonForma...
#include "explosions.h" // Para Explosions
#include "jail_audio.h" // Para JA_PlaySound
#include "param.h" // Para Param, ParamGame, param
#include "resource.h" // Para Resource
#include "screen.h" // Para Screen
#include "stage.h" // Para power
#include "texture.h" // Para Texture
#include "utils.h" // Para Zone, BLOCK, Color, flash_color
// Constructor
BalloonManager::BalloonManager()
@@ -58,19 +61,22 @@ void BalloonManager::update()
{
balloon->update();
}
updateBalloonDeployCounter();
explosions_->update();
}
// Renderiza los globos
void BalloonManager::renderBalloons()
// Renderiza los objetos
void BalloonManager::render()
{
for (auto &balloon : balloons_)
{
balloon->render();
}
explosions_->render();
}
// Crea una formación de enemigos
void BalloonManager::deployBalloonFormation()
void BalloonManager::deployBalloonFormation(int stage)
{
// Solo despliega una formación enemiga si ha pasado cierto tiempo desde la última
if (balloon_deploy_counter_ == 0)
@@ -100,7 +106,7 @@ void BalloonManager::deployBalloonFormation()
last_balloon_deploy_ = formation;
const auto set = balloon_formations_->getStage(game_.getCurrentStage()).balloon_pool.set[formation];
const auto set = balloon_formations_->getSet(stage, formation);
const auto numEnemies = set.number_of_balloons;
for (int i = 0; i < numEnemies; ++i)
{
@@ -125,7 +131,9 @@ void BalloonManager::freeBalloons()
void BalloonManager::updateBalloonDeployCounter()
{
if (balloon_deploy_counter_ > 0)
{
--balloon_deploy_counter_;
}
}
// Indica si se puede crear una powerball
@@ -184,33 +192,17 @@ void BalloonManager::createPowerBall()
// Establece la velocidad de los globos
void BalloonManager::setBalloonSpeed(float speed)
{
balloon_speed_ = speed;
for (auto &balloon : balloons_)
{
balloon->setSpeed(speed);
}
// Actualiza la velocidad de los globos en funcion del poder acumulado de la fase
void BalloonManager::checkAndUpdateBalloonSpeed()
{
if (difficulty_ != GameDifficulty::NORMAL)
return;
const float percent = static_cast<float>(game_.getCurrentPower()) / balloon_formations_->getStage(game_.getCurrentStage()).power_to_complete;
const float thresholds[] = {0.2f, 0.4f, 0.6f, 0.8f};
for (size_t i = 0; i < std::size(thresholds); ++i)
if (balloon_speed_ == BALLOON_SPEED[i] && percent > thresholds[i])
{
balloon_speed_ = BALLOON_SPEED[i + 1];
setBalloonSpeed(balloon_speed_);
break; // Salir del bucle una vez actualizada la velocidad y aplicada
}
}
}
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
void BalloonManager::popBalloon(std::shared_ptr<Balloon> balloon)
{
game_.increaseStageCurrentPower(1);
balloons_popped_++;
++Stage::power;
if (balloon->getType() == BalloonType::POWERBALL)
{
@@ -230,13 +222,10 @@ void BalloonManager::popBalloon(std::shared_ptr<Balloon> balloon)
explosions_->add(balloon->getPosX(), balloon->getPosY(), static_cast<int>(balloon->getSize()));
balloon->pop();
}
// Recalcula el nivel de amenaza
evaluateAndSetMenace();
}
// Explosiona un globo. Lo destruye = no crea otros globos
void BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon)
int BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon)
{
int score = 0;
@@ -261,32 +250,41 @@ void BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon)
}
// Otorga los puntos correspondientes al globo
/*
for (auto &player : players_)
{
player->addScore(score * player->getScoreMultiplier() * difficulty_score_multiplier_);
}
updateHiScore();
*/
// Aumenta el poder de la fase
const auto power = balloon->getPower();
game_.increaseStageCurrentPower(power);
balloons_popped_ += power;
Stage::power += power;
// balloons_popped_ += power;
// Destruye el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), static_cast<int>(balloon->getSize()));
balloon->pop();
return score;
}
// Destruye todos los globos
void BalloonManager::destroyAllBalloons()
int BalloonManager::destroyAllBalloons()
{
int score = 0;
for (auto &balloon : balloons_)
{
destroyBalloon(balloon);
score += destroyBalloon(balloon);
}
balloon_deploy_counter_ = 300;
JA_PlaySound(Resource::get()->getSound("powerball.wav"));
Screen::get()->flash(flash_color, 100);
Screen::get()->shake();
return score;
}
// Detiene todos los globos
@@ -350,4 +348,11 @@ void BalloonManager::createTwoBigBalloons()
auto p = set.init[i];
createBalloon(p.x, p.y, p.type, p.size, p.vel_x, balloon_speed_, p.creation_counter);
}
}
// Obtiene el nivel de ameza actual generado por los globos
int BalloonManager::getMenace()
{
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon)
{ return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
}