From 6235d0b6848b48f71f6feb1d79fe480c5bc9b305 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 26 Oct 2024 18:08:04 +0200 Subject: [PATCH] Revisada la classe Balloon --- source/balloon.cpp | 404 +++++++++++++++++---------------------------- source/balloon.h | 92 ++++++----- source/game.cpp | 66 +++----- 3 files changed, 218 insertions(+), 344 deletions(-) diff --git a/source/balloon.cpp b/source/balloon.cpp index a44b30c..60243b5 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -1,5 +1,6 @@ #include "balloon.h" #include // para abs +#include // para clamp #include "animated_sprite.h" // para SpriteAnimated #include "moving_sprite.h" // para MovingSprite #include "param.h" // para param @@ -9,9 +10,9 @@ // Constructor Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr texture, const std::vector &animation) : sprite_(std::make_unique(texture, animation)), - pos_x_(x), - pos_y_(y), - vel_x_(vel_x), + x_(x), + y_(y), + vx_(vel_x), being_created_(creation_timer > 0), invulnerable_(creation_timer > 0), creation_counter_(creation_timer), @@ -24,13 +25,13 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel { case BalloonType::BALLOON: { - vel_y_ = 0; - max_vel_y_ = 3.0f; + vy_ = 0; + max_vy_ = 3.0f; const int size = static_cast(size_); gravity_ = param.balloon.at(size).grav; - default_vel_y_ = param.balloon.at(size).vel; - height_ = width_ = BALLOON_SIZE[size]; + default_vy_ = param.balloon.at(size).vel; + h_ = w_ = BALLOON_SIZE[size]; power_ = BALLOON_POWER[size]; menace_ = BALLOON_MENACE[size]; score_ = BALLOON_SCORE[size]; @@ -40,11 +41,11 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel case BalloonType::FLOATER: { - default_vel_y_ = max_vel_y_ = vel_y_ = fabs(vel_x_ * 2.0f); + default_vy_ = max_vy_ = vy_ = fabs(vx_ * 2.0f); gravity_ = 0.00f; const int size = static_cast(size_); - height_ = width_ = BALLOON_SIZE[size]; + h_ = w_ = BALLOON_SIZE[size]; power_ = BALLOON_POWER[size]; menace_ = BALLOON_MENACE[size]; score_ = BALLOON_SCORE[size]; @@ -55,20 +56,17 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel case BalloonType::POWERBALL: { const int size = 3; - // Alto y ancho del objeto - height_ = width_ = BALLOON_SIZE[size]; + h_ = w_ = BALLOON_SIZE[size]; power_ = score_ = menace_ = 0; - // Inicializa los valores de velocidad y gravedad - vel_y_ = 0; - max_vel_y_ = 3.0f; + vy_ = 0; + max_vy_ = 3.0f; gravity_ = param.balloon.at(size).grav; - default_vel_y_ = param.balloon.at(size).vel; + default_vy_ = param.balloon.at(size).vel; - // Añade rotación al sprite_ sprite_->disableRotate(); sprite_->setRotateSpeed(0); - sprite_->setRotateAmount(vel_x_ > 0.0f ? 2.0 : -2.0); + sprite_->setRotateAmount(vx_ > 0.0f ? 2.0 : -2.0); break; } @@ -77,82 +75,54 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel break; } - // Valores para el efecto de rebote - bouncing_.enabled = false; - bouncing_.counter = 0; - bouncing_.speed = 2; - bouncing_.zoomW = 1.0f; - bouncing_.zoomH = 1.0f; - bouncing_.despX = 0.0f; - bouncing_.despY = 0.0f; - bouncing_.w = {1.10f, 1.05f, 1.00f, 0.95f, 0.90f, 0.95f, 1.00f, 1.02f, 1.05f, 1.02f}; - bouncing_.h = {0.90f, 0.95f, 1.00f, 1.05f, 1.10f, 1.05f, 1.00f, 0.98f, 0.95f, 0.98f}; - // Configura el sprite - sprite_->setPos({static_cast(pos_x_), static_cast(pos_y_), width_, height_}); + sprite_->setWidth(w_); + sprite_->setHeight(h_); + shiftSprite(); // Alinea el circulo de colisión con el objeto - collider_.r = width_ / 2; + collider_.r = w_ / 2; shiftColliders(); } // Centra el globo en la posición X -void Balloon::allignTo(int x) +void Balloon::alignTo(int x) { - pos_x_ = float(x - (width_ / 2)); - - if (pos_x_ < param.game.play_area.rect.x) - { - pos_x_ = param.game.play_area.rect.x + 1; - } - else if ((pos_x_ + width_) > param.game.play_area.rect.w) - { - pos_x_ = float(param.game.play_area.rect.w - width_ - 1); - } - - // Posición X,Y del sprite_ - sprite_->setPosX(getPosX()); - sprite_->setPosY(getPosY()); - - // Alinea el circulo de colisión con el objeto - shiftColliders(); + x_ = static_cast(x - (w_ / 2)); + const int min_x = param.game.play_area.rect.x; + const int max_x = param.game.play_area.rect.w - w_; + x_ = std::clamp(x_, static_cast(min_x), static_cast(max_x)); } // Pinta el globo en la pantalla void Balloon::render() { - if (visible_ && enabled_) + if (isBeingCreated()) + { + // Aplica alpha blending + sprite_->getTexture()->setAlpha(255 - (int)((float)creation_counter_ * (255.0f / (float)creation_counter_ini_))); + sprite_->render(); + sprite_->getTexture()->setAlpha(255); + } + else { if (bouncing_.enabled) { - if (type_ != BalloonType::POWERBALL) - { - // Aplica desplazamiento para el zoom - sprite_->setPosX(getPosX() + bouncing_.despX); - sprite_->setPosY(getPosY() + bouncing_.despY); - sprite_->render(); - sprite_->setPosX(getPosX() - bouncing_.despX); - sprite_->setPosY(getPosY() - bouncing_.despY); - } - } - else if (isBeingCreated()) - { - // Aplica alpha blending - sprite_->getTexture()->setAlpha(255 - (int)((float)creation_counter_ * (255.0f / (float)creation_counter_ini_))); + // Aplica efecto de bouncing + sprite_->setPos(x_ + bouncing_.despX, y_ + bouncing_.despY); sprite_->render(); - sprite_->getTexture()->setAlpha(255); + sprite_->setPos(x_ - bouncing_.despX, y_ - bouncing_.despY); } else - { sprite_->render(); - } + } - if (type_ == BalloonType::POWERBALL && !isBeingCreated()) - { - auto sp = std::make_unique(sprite_->getTexture(), sprite_->getPosition()); - sp->setSpriteClip(BALLOON_SIZE[3], 0, BALLOON_SIZE[3], BALLOON_SIZE[3]); - sp->render(); - } + // Añade la máscara del borde a la PowerBall + if (type_ == BalloonType::POWERBALL && !isBeingCreated()) + { + auto sp = std::make_unique(sprite_->getTexture(), sprite_->getPosition()); + sp->setSpriteClip(BALLOON_SIZE[3], 0, BALLOON_SIZE[3], BALLOON_SIZE[3]); + sp->render(); } } @@ -162,61 +132,45 @@ void Balloon::move() // Comprueba si se puede mover if (!isStopped()) { - // Lo mueve a izquierda o derecha - pos_x_ += (vel_x_ * speed_); + // Mueve el globo en horizontal + x_ += vx_ * speed_; - // Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido - if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ + width_ > param.game.play_area.rect.w)) + // Colisión en las partes laterales de la zona de juego + const int clip = 2; + const float min_x = param.game.play_area.rect.x - clip; + const float max_x = param.game.play_area.rect.w - w_ + clip; + if (x_ < min_x || x_ > max_x) { - // Corrige posición - pos_x_ -= (vel_x_ * speed_); - - // Invierte sentido - vel_x_ = -vel_x_; - - // Invierte la rotación - sprite_->switchRotate(); - - // Activa el efecto de rebote - if (type_ != BalloonType::POWERBALL) - { - bounceStart(); - } + x_ = std::clamp(x_, min_x, max_x); + vx_ = -vx_; + // Activa el efecto de rebote o invierte la rotación + if (type_ == BalloonType::POWERBALL) + sprite_->switchRotate(); + else + enableBounce(); } - // Mueve el globo hacia arriba o hacia abajo - pos_y_ += (vel_y_ * speed_); + // Mueve el globo en vertical + y_ += vy_ * speed_; - // Si se sale por arriba - if (pos_y_ < param.game.play_area.rect.y) + // Colisión en la parte superior de la zona de juego + const int min_y = param.game.play_area.rect.y; + if (y_ < min_y) { - // Corrige - pos_y_ = param.game.play_area.rect.y; - - // Invierte sentido - vel_y_ = -vel_y_; - - // Activa el efecto de rebote + y_ = min_y; + vy_ = -vy_; if (type_ != BalloonType::POWERBALL) - { - bounceStart(); - } + enableBounce(); } - // Si el globo se sale por la parte inferior - if (pos_y_ + height_ > param.game.play_area.rect.h) + // Colisión en la parte inferior de la zona de juego + const int max_y = param.game.play_area.rect.h - h_; + if (y_ > max_y) { - // Corrige - pos_y_ = param.game.play_area.rect.h - height_; - - // Invierte colocando una velocidad por defecto - vel_y_ = -default_vel_y_; - - // Activa el efecto de rebote + y_ = max_y; + vy_ = -default_vy_; if (type_ != BalloonType::POWERBALL) - { - bounceStart(); - } + enableBounce(); } /* @@ -238,46 +192,15 @@ void Balloon::move() travel_y_ -= 1.0f; // Aplica la gravedad al objeto sin pasarse de una velocidad máxima - vel_y_ += gravity_; + vy_ += gravity_; } - - // Actualiza la posición del sprite_ - sprite_->setPosX(getPosX()); - sprite_->setPosY(getPosY()); } } // Deshabilita el globo y pone a cero todos los valores void Balloon::disable() { - being_created_ = false; - blinking_ = false; - collider_.r = 0; - collider_.x = 0; - collider_.y = 0; - counter_ = 0; - creation_counter_ = 0; - creation_counter_ini_ = 0; - default_vel_y_ = 0.0f; enabled_ = false; - gravity_ = 0.0f; - height_ = 0; - invulnerable_ = false; - max_vel_y_ = 0.0f; - menace_ = 0; - pos_x_ = 0.0f; - pos_y_ = 0.0f; - power_ = 0; - score_ = 0; - speed_ = 0; - stopped_ = false; - stopped_counter_ = 0; - travel_y_ = 0; - vel_x_ = 0.0f; - vel_y_ = 0.0f; - visible_ = false; - width_ = 0; - sprite_->clear(); } // Explosiona el globo @@ -289,16 +212,14 @@ void Balloon::pop() // Actualiza al globo a su posicion, animación y controla los contadores void Balloon::update() { - if (enabled_) - { - sprite_->update(); - move(); - updateAnimation(); - shiftColliders(); - updateState(); - updateBounce(); - ++counter_; - } + move(); + updateAnimation(); + updateState(); + updateBounce(); + shiftSprite(); + shiftColliders(); + sprite_->update(); + ++counter_; } // Actualiza los estados del globo @@ -311,70 +232,54 @@ void Balloon::updateState() setStop(true); setInvulnerable(true); - // Todavia tiene tiempo en el contador if (creation_counter_ > 0) { // Desplaza lentamente el globo hacia abajo y hacia un lado if (creation_counter_ % 10 == 0) { - pos_y_++; - pos_x_ += vel_x_; + y_++; + x_ += vx_; // Comprueba no se salga por los laterales - if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ > (param.game.play_area.rect.w - width_))) + const int min_x = param.game.play_area.rect.x; + const int max_x = param.game.play_area.rect.w - w_; + + if (x_ < min_x || x_ > max_x) { // Corrige y cambia el sentido de la velocidad - pos_x_ -= vel_x_; - vel_x_ = -vel_x_; + x_ -= vx_; + vx_ = -vx_; } - - // Actualiza la posición del sprite_ - sprite_->setPosX(getPosX()); - sprite_->setPosY(getPosY()); - - // Actualiza la posición del circulo de colisión - shiftColliders(); } - - creation_counter_--; + --creation_counter_; } - // El contador ha llegado a cero + else { - setBeingCreated(false); + // El contador ha llegado a cero + being_created_ = false; + // visible_ = true; setStop(false); - setVisible(true); setInvulnerable(false); if (type_ == BalloonType::POWERBALL) - { sprite_->enableRotate(); - } } } - // Solo comprueba el estado detenido cuando no se está creando else if (isStopped()) { - // Si es una powerball deja de rodar + // Solo comprueba el estado detenido cuando no se está creando if (type_ == BalloonType::POWERBALL) - { sprite_->disableRotate(); - } // Reduce el contador if (stopped_counter_ > 0) - { - stopped_counter_--; - } - // Si el contador ha llegado a cero + --stopped_counter_; else - { // Quitarles el estado "detenido" + { + // Si el contador ha llegado a cero setStop(false); - - // Si es una powerball vuelve a rodar if (type_ == BalloonType::POWERBALL) - { sprite_->enableRotate(); - } } } } @@ -382,23 +287,27 @@ void Balloon::updateState() // Establece la animación correspondiente al estado void Balloon::updateAnimation() { - std::string creating_animation = "blue"; - std::string normal_animation = "orange"; + std::string creating_animation; + std::string normal_animation; - if (type_ == BalloonType::POWERBALL) + switch (type_) { + case BalloonType::POWERBALL: creating_animation = "powerball"; normal_animation = "powerball"; - } - else if (type_ == BalloonType::FLOATER) - { + break; + case BalloonType::FLOATER: creating_animation = "red"; normal_animation = "green"; + break; + default: + creating_animation = "blue"; + normal_animation = "orange"; + break; } // Establece el frame de animación sprite_->setCurrentAnimation(isBeingCreated() ? creating_animation : normal_animation); - sprite_->update(); } // Comprueba si el globo está habilitado @@ -410,37 +319,31 @@ bool Balloon::isEnabled() const // Obtiene del valor de la variable float Balloon::getPosX() const { - return pos_x_; + return x_; } // Obtiene del valor de la variable float Balloon::getPosY() const { - return pos_y_; -} - -// Obtiene del valor de la variable -float Balloon::getVelY() const -{ - return vel_y_; + return y_; } // Obtiene del valor de la variable int Balloon::getWidth() const { - return width_; + return w_; } // Obtiene del valor de la variable int Balloon::getHeight() const { - return height_; + return h_; } // Establece el valor de la variable void Balloon::setVelY(float vel_y) { - vel_y_ = vel_y; + vy_ = vel_y; } // Establece el valor de la variable @@ -485,18 +388,6 @@ bool Balloon::isBlinking() const return blinking_; } -// Establece el valor de la variable -void Balloon::setVisible(bool value) -{ - visible_ = value; -} - -// Obtiene del valor de la variable -bool Balloon::isVisible() const -{ - return visible_; -} - // Establece el valor de la variable void Balloon::setInvulnerable(bool value) { @@ -509,12 +400,6 @@ bool Balloon::isInvulnerable() const return invulnerable_; } -// Establece el valor de la variable -void Balloon::setBeingCreated(bool value) -{ - being_created_ = value; -} - // Obtiene del valor de la variable bool Balloon::isBeingCreated() const { @@ -548,8 +433,22 @@ Circle &Balloon::getCollider() // Alinea el circulo de colisión con la posición del objeto globo void Balloon::shiftColliders() { - collider_.x = Uint16(pos_x_ + collider_.r); - collider_.y = pos_y_ + collider_.r; + collider_.x = static_cast(x_) + collider_.r; + collider_.y = static_cast(y_) + collider_.r; +} + +// Alinea el sprite con la posición del objeto globo +void Balloon::shiftSprite() +{ + sprite_->setPosX(x_); + sprite_->setPosY(y_); +} + +// Establece el nivel de zoom del sprite +void Balloon::zoomSprite() +{ + sprite_->setZoomW(bouncing_.zoomW); + sprite_->setZoomH(bouncing_.zoomH); } // Obtiene le valor de la variable @@ -564,44 +463,39 @@ Uint8 Balloon::getPower() const return power_; } -void Balloon::bounceStart() +// Activa el efecto +void Balloon::enableBounce() { bouncing_.enabled = true; - bouncing_.zoomW = 1; - bouncing_.zoomH = 1; - sprite_->setZoomW(bouncing_.zoomW); - sprite_->setZoomH(bouncing_.zoomH); - bouncing_.despX = 0; - bouncing_.despY = 0; + bouncing_.reset(); + zoomSprite(); } -void Balloon::bounceStop() +// Detiene el efecto +void Balloon::disableBounce() { bouncing_.enabled = false; - bouncing_.counter = 0; - bouncing_.zoomW = 1.0f; - bouncing_.zoomH = 1.0f; - sprite_->setZoomW(bouncing_.zoomW); - sprite_->setZoomH(bouncing_.zoomH); - bouncing_.despX = 0.0f; - bouncing_.despY = 0.0f; + bouncing_.reset(); + zoomSprite(); } +// Aplica el efecto void Balloon::updateBounce() { if (bouncing_.enabled) { - bouncing_.zoomW = bouncing_.w[bouncing_.counter / bouncing_.speed]; - bouncing_.zoomH = bouncing_.h[bouncing_.counter / bouncing_.speed]; - sprite_->setZoomW(bouncing_.zoomW); - sprite_->setZoomH(bouncing_.zoomH); - bouncing_.despX = (sprite_->getSpriteClip().w - (sprite_->getSpriteClip().w * bouncing_.zoomW)); - bouncing_.despY = (sprite_->getSpriteClip().h - (sprite_->getSpriteClip().h * bouncing_.zoomH)); - ++bouncing_.counter; - if ((bouncing_.counter / bouncing_.speed) > (MAX_BOUNCE - 1)) - { - bounceStop(); - } + const int index = bouncing_.counter / bouncing_.speed; + bouncing_.zoomW = bouncing_.w[index]; + bouncing_.zoomH = bouncing_.h[index]; + + zoomSprite(); + + const auto spriteClip = sprite_->getSpriteClip(); + bouncing_.despX = spriteClip.w * (1.0f - bouncing_.zoomW); + bouncing_.despY = spriteClip.h * (1.0f - bouncing_.zoomH); + + if (++bouncing_.counter / bouncing_.speed >= MAX_BOUNCE) + disableBounce(); } } diff --git a/source/balloon.h b/source/balloon.h index 682c056..a8b0cf5 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -13,10 +13,10 @@ class Texture; constexpr int MAX_BOUNCE = 10; // Puntos de globo -constexpr int BALLOON_SCORE[4] = {50, 100, 200, 400}; -constexpr int BALLOON_POWER[4] = {1, 3, 7, 15}; -constexpr int BALLOON_MENACE[4] = {1, 2, 4, 8}; -constexpr int BALLOON_SIZE[4] = {10, 16, 26, 46}; +constexpr int BALLOON_SCORE[] = {50, 100, 200, 400}; +constexpr int BALLOON_POWER[] = {1, 3, 7, 15}; +constexpr int BALLOON_MENACE[] = {1, 2, 4, 8}; +constexpr int BALLOON_SIZE[] = {10, 16, 26, 46}; // Tamaños de globo enum class BalloonSize : Uint8 @@ -45,11 +45,7 @@ constexpr int BALLOON_POP_ANIMATION = 1; constexpr int BALLOON_BORN_ANIMATION = 2; // Velocidades a las que se mueven los globos -constexpr float BALLOON_SPEED_1 = 0.60f; -constexpr float BALLOON_SPEED_2 = 0.70f; -constexpr float BALLOON_SPEED_3 = 0.80f; -constexpr float BALLOON_SPEED_4 = 0.90f; -constexpr float BALLOON_SPEED_5 = 1.00f; +constexpr float BALLOON_SPEED[] = {0.60f, 0.70f, 0.80f, 0.90f, 1.00f}; // PowerBall constexpr int POWERBALL_SCREENPOWER_MINIMUM = 10; @@ -59,39 +55,52 @@ constexpr int POWERBALL_COUNTER = 8; class Balloon { private: - // Estructura para las variables para el efecto de los rebotes + // Estructura para el efecto de los rebotes en los globos struct Bouncing { - bool enabled; // Si el efecto está activo - Uint8 counter; // Countador para el efecto - Uint8 speed; // Velocidad a la que transcurre el efecto - float zoomW; // Zoom aplicado a la anchura - float zoomH; // Zoom aplicado a la altura - float despX; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom - float despY; // Desplazamiento de pixeles en el eje Y antes de pintar el objeto con zoom - std::vector w; // Vector con los valores de zoom para el ancho del globo - std::vector h; // Vector con los valores de zoom para el alto del globo - }; + bool enabled = false; // Si el efecto está activo + Uint8 counter = 0; // Contador para el efecto + Uint8 speed = 2; // Velocidad a la que transcurre el efecto + float zoomW = 1.0f; // Zoom aplicado a la anchura + float zoomH = 1.0f; // Zoom aplicado a la altura + float despX = 0.0f; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom + float despY = 0.0f; // Desplazamiento de pixeles en el eje Y antes de pintar el objeto con zoom + + float w[MAX_BOUNCE] = {1.10f, 1.05f, 1.00f, 0.95f, 0.90f, 0.95f, 1.00f, 1.02f, 1.05f, 1.02f}; // Vector con los valores de zoom para el ancho del globo + float h[MAX_BOUNCE] = {0.90f, 0.95f, 1.00f, 1.05f, 1.10f, 1.05f, 1.00f, 0.98f, 0.95f, 0.98f}; // Vector con los valores de zoom para el alto del globo + + // Constructor por defecto + Bouncing() = default; + + // Método reset + void reset() + { + counter = 0; + zoomW = 1.0f; + zoomH = 1.0f; + despX = 0.0f; + despY = 0.0f; + } + } bouncing_; // Objetos y punteros std::unique_ptr sprite_; // Sprite del objeto globo // Variables - float pos_x_; // Posición en el eje X - float pos_y_; // Posición en el eje Y - Uint8 width_; // Ancho - Uint8 height_; // Alto - float vel_x_; // Velocidad en el eje X. Cantidad de pixeles a desplazarse - float vel_y_; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse + float x_; // Posición en el eje X + float y_; // Posición en el eje Y + Uint8 w_; // Ancho + Uint8 h_; // Alto + float vx_; // Velocidad en el eje X. Cantidad de pixeles a desplazarse + float vy_; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse float gravity_; // Aceleración en el eje Y. Modifica la velocidad - float default_vel_y_; // Velocidad inicial que tienen al rebotar contra el suelo - float max_vel_y_; // Máxima velocidad que puede alcanzar el objeto en el eje Y + float default_vy_; // Velocidad inicial que tienen al rebotar contra el suelo + float max_vy_; // Máxima velocidad que puede alcanzar el objeto en el eje Y bool being_created_; // Indica si el globo se está creando bool blinking_ = false; // Indica si el globo está intermitente bool enabled_ = true; // Indica si el globo esta activo bool invulnerable_; // Indica si el globo es invulnerable bool stopped_ = true; // Indica si el globo está parado - bool visible_ = true; // Indica si el globo es visible Circle collider_; // Circulo de colisión del objeto Uint16 creation_counter_; // Temporizador para controlar el estado "creandose" Uint16 creation_counter_ini_; // Valor inicial para el temporizador para controlar el estado "creandose" @@ -104,16 +113,21 @@ private: float travel_y_ = 1.0f; // Distancia que ha de recorrer el globo en el eje Y antes de que se le aplique la gravedad float speed_; // Velocidad a la que se mueven los globos Uint8 power_; // Cantidad de poder que alberga el globo - Bouncing bouncing_; // Contiene las variables para el efecto de rebote // Alinea el circulo de colisión con la posición del objeto globo void shiftColliders(); + // Alinea el sprite con la posición del objeto globo + void shiftSprite(); + + // Establece el nivel de zoom del sprite + void zoomSprite(); + // Activa el efecto - void bounceStart(); + void enableBounce(); // Detiene el efecto - void bounceStop(); + void disableBounce(); // Aplica el efecto void updateBounce(); @@ -124,9 +138,6 @@ private: // Establece la animación correspondiente void updateAnimation(); - // Establece el valor de la variable - void setBeingCreated(bool value); - public: // Constructor Balloon(float x, float y, BalloonType type, BalloonSize size, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr texture, const std::vector &animation); @@ -135,7 +146,7 @@ public: ~Balloon() = default; // Centra el globo en la posición X - void allignTo(int x); + void alignTo(int x); // Pinta el globo en la pantalla void render(); @@ -161,9 +172,6 @@ public: // Obtiene del valor de la variable float getPosY() const; - // Obtiene del valor de la variable - float getVelY() const; - // Obtiene del valor de la variable int getWidth() const; @@ -194,12 +202,6 @@ public: // Obtiene del valor de la variable bool isBlinking() const; - // Establece el valor de la variable - void setVisible(bool value); - - // Obtiene del valor de la variable - bool isVisible() const; - // Establece el valor de la variable void setInvulnerable(bool value); diff --git a/source/game.cpp b/source/game.cpp index 93bd407..9f41d9d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -230,27 +230,22 @@ void Game::deployBalloonFormation() power_ball_counter_ = (power_ball_counter_ > 0) ? (power_ball_counter_ - 1) : 0; // Elige una formación enemiga la azar - auto set = rand() % 10; + auto formation = rand() % 10; // Evita repetir la ultima formación enemiga desplegada - if (set == last_balloon_deploy_) + if (formation == last_balloon_deploy_) { - ++set %= 10; + ++formation %= 10; } - last_balloon_deploy_ = set; + last_balloon_deploy_ = formation; - const Stage stage = balloon_formations_->getStage(current_stage_); - const auto numEnemies = stage.balloon_pool.set[set].number_of_balloons; + const auto set = balloon_formations_->getStage(0).balloon_pool.set[formation]; + 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].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); + auto p = set.init[i]; + createBalloon(p.x, p.y, p.type, p.size, p.vel_x, balloon_speed_, p.creation_counter); } balloon_deploy_counter_ = 300; @@ -476,34 +471,17 @@ void Game::setBalloonSpeed(float speed) // Actualiza la velocidad de los globos en funcion del poder acumulado de la fase void Game::updateBalloonSpeed() { - const float percent = (float)current_power_ / (float)balloon_formations_->getStage(current_stage_).power_to_complete; - float old_balloon_speed = balloon_speed_; + const float percent = static_cast(current_power_) / balloon_formations_->getStage(current_stage_).power_to_complete; + const float thresholds[] = {0.2f, 0.4f, 0.6f, 0.8f}; - // Comprueba si se ha de modificar la velocidad de los globos - if (balloon_speed_ == BALLOON_SPEED_1 && percent > 0.2f) + for (size_t i = 0; i < std::size(thresholds); ++i) { - balloon_speed_ = BALLOON_SPEED_2; - } - - else if (balloon_speed_ == BALLOON_SPEED_2 && percent > 0.4f) - { - balloon_speed_ = BALLOON_SPEED_3; - } - - else if (balloon_speed_ == BALLOON_SPEED_3 && percent > 0.6f) - { - balloon_speed_ = BALLOON_SPEED_4; - } - - else if (balloon_speed_ == BALLOON_SPEED_4 && percent > 0.8f) - { - balloon_speed_ = BALLOON_SPEED_5; - } - - // Si ha habido cambio, se aplica a todos los globos - if (old_balloon_speed != balloon_speed_) - { - setBalloonSpeed(balloon_speed_); + 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 + } } } @@ -534,11 +512,11 @@ void Game::popBalloon(std::shared_ptr balloon) const auto lower_size = static_cast(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->alignTo(balloon->getPosX() + (balloon->getWidth() / 2)); balloon_left->setVelY(balloon_left->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f); 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->alignTo(balloon->getPosX() + (balloon->getWidth() / 2)); balloon_right->setVelY(balloon_right->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f); // Elimina el globo @@ -2167,7 +2145,7 @@ void Game::initDifficultyVars() { case GameDifficulty::EASY: { - default_balloon_speed_ = BALLOON_SPEED_1; + default_balloon_speed_ = BALLOON_SPEED[0]; difficulty_score_multiplier_ = 0.5f; scoreboard_->setColor(scoreboard_easy_color); break; @@ -2175,7 +2153,7 @@ void Game::initDifficultyVars() case GameDifficulty::NORMAL: { - default_balloon_speed_ = BALLOON_SPEED_1; + default_balloon_speed_ = BALLOON_SPEED[0]; difficulty_score_multiplier_ = 1.0f; scoreboard_->setColor(scoreboard_normal_color); break; @@ -2183,7 +2161,7 @@ void Game::initDifficultyVars() case GameDifficulty::HARD: { - default_balloon_speed_ = BALLOON_SPEED_5; + default_balloon_speed_ = BALLOON_SPEED[4]; difficulty_score_multiplier_ = 1.5f; scoreboard_->setColor(scoreboard_hard_color); break;