balloon: readability-function-cognitive-complexity

balloon: eliminat l'efecte de rebot per als globos mes xicotets
moving_sprite: canviats variables i metodes zoomW a horizontal_zoom i zoomH a vertical_zoom
This commit is contained in:
2025-07-23 18:03:49 +02:00
parent b0564ceccf
commit 5f170ee44e
4 changed files with 166 additions and 119 deletions

View File

@@ -140,80 +140,102 @@ void Balloon::render() {
// Actualiza la posición y estados del globo
void Balloon::move() {
// Comprueba si se puede mover
if (!isStopped()) {
// Mueve el globo en horizontal
x_ += vx_ * speed_;
if (isStopped()) {
return;
}
// Colisión en las partes laterales de la zona de juego
const int CLIP = 2;
const float MIN_X = play_area_.x - CLIP;
const float MAX_X = play_area_.x + play_area_.w - w_ + CLIP;
if (x_ < MIN_X || x_ > MAX_X) {
if (bouncing_sound_enabled_) {
playSound(bouncing_sound_);
}
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();
}
handleHorizontalMovement();
handleVerticalMovement();
applyGravity();
}
void Balloon::handleHorizontalMovement() {
x_ += vx_ * speed_;
const int CLIP = 2;
const float MIN_X = play_area_.x - CLIP;
const float MAX_X = play_area_.x + play_area_.w - w_ + CLIP;
if (isOutOfHorizontalBounds(MIN_X, MAX_X)) {
handleHorizontalBounce(MIN_X, MAX_X);
}
}
void Balloon::handleVerticalMovement() {
y_ += vy_ * speed_;
if (shouldCheckTopCollision()) {
handleTopCollision();
}
handleBottomCollision();
}
bool Balloon::isOutOfHorizontalBounds(float minX, float maxX) const {
return x_ < minX || x_ > maxX;
}
void Balloon::handleHorizontalBounce(float minX, float maxX) {
playBouncingSound();
x_ = std::clamp(x_, minX, maxX);
vx_ = -vx_;
if (type_ == BalloonType::POWERBALL) {
sprite_->switchRotate();
} else {
enableBounceEffect();
}
}
bool Balloon::shouldCheckTopCollision() const {
// Colisión en la parte superior solo si el globo va de subida
return vy_ < 0;
}
void Balloon::handleTopCollision() {
const int MIN_Y = play_area_.y;
if (y_ < MIN_Y) {
playBouncingSound();
y_ = MIN_Y;
vy_ = -vy_;
enableBounceEffect();
}
}
void Balloon::handleBottomCollision() {
const int MAX_Y = play_area_.y + play_area_.h - h_;
if (y_ > MAX_Y) {
playBouncingSound();
y_ = MAX_Y;
vy_ = -default_vy_;
if (type_ != BalloonType::POWERBALL) {
enableBounceEffect();
} else {
setInvulnerable(false);
}
}
}
// Mueve el globo en vertical
y_ += vy_ * speed_;
void Balloon::applyGravity() {
/*
Para aplicar la gravedad, el diseño original la aplicaba en cada iteración del bucle
Al añadir el modificador de velocidad se reduce la distancia que recorre el objeto y por
tanto recibe mas gravedad. Para solucionarlo se va a aplicar la gravedad cuando se haya
recorrido una distancia igual a la velocidad en Y, que era el cálculo inicial
*/
// Colisión en la parte superior solo si el globo va de subida
if (vy_ < 0) {
const int MIN_Y = play_area_.y;
if (y_ < MIN_Y) {
if (bouncing_sound_enabled_) {
playSound(bouncing_sound_);
}
y_ = MIN_Y;
vy_ = -vy_;
enableBounce();
}
}
travel_y_ += speed_;
// Colisión en la parte inferior de la zona de juego
const int MAX_Y = play_area_.y + play_area_.h - h_;
if (y_ > MAX_Y) {
if (bouncing_sound_enabled_) {
playSound(bouncing_sound_);
}
y_ = MAX_Y;
vy_ = -default_vy_;
if (type_ != BalloonType::POWERBALL) {
enableBounce();
} else {
setInvulnerable(false);
}
}
if (travel_y_ >= 1.0F) {
travel_y_ -= 1.0F;
vy_ += gravity_;
}
}
/*
Para aplicar la gravedad, el diseño original la aplicaba en cada iteración del bucle
Al añadir el modificador de velocidad se reduce la distancia que recorre el objeto y por
tanto recibe mas gravedad. Para solucionarlo se va a aplicar la gravedad cuando se haya
recorrido una distancia igual a la velocidad en Y, que era el cálculo inicial
*/
// Incrementa la variable que calcula la distancia acumulada en Y
travel_y_ += speed_;
// Si la distancia acumulada en Y es igual a la velocidad, se aplica la gravedad
if (travel_y_ >= 1.0F) {
// Quita el excedente
travel_y_ -= 1.0F;
// Aplica la gravedad al objeto sin pasarse de una velocidad máxima
vy_ += gravity_;
}
void Balloon::playBouncingSound() {
if (bouncing_sound_enabled_) {
playSound(bouncing_sound_);
}
}
@@ -221,7 +243,7 @@ void Balloon::move() {
void Balloon::update() {
move();
updateState();
updateBounce();
updateBounceEffect();
shiftSprite();
shiftColliders();
sprite_->update();
@@ -321,37 +343,41 @@ void Balloon::shiftSprite() {
sprite_->setPosY(y_);
}
// Establece el nivel de zoom del sprite
void Balloon::zoomSprite() {
sprite_->setZoomW(bouncing_.horizontal_zoom);
sprite_->setZoomH(bouncing_.verical_zoom);
// Aplica la deformación visual causada por el efecto de rebote
void Balloon::applyBounceEffect() {
sprite_->setHorizontalZoom(bounce_effect_.horizontal_zoom);
sprite_->setVerticalZoom(bounce_effect_.verical_zoom);
}
// Activa el efecto
void Balloon::enableBounce() {
bouncing_.enabled = true;
bouncing_.reset();
zoomSprite();
void Balloon::enableBounceEffect() {
// Los globos pequeños no tienen efecto de rebote
if (size_ == BalloonSize::SIZE1) {
return;
}
bounce_effect_.enabled = true;
bounce_effect_.reset();
applyBounceEffect();
}
// Detiene el efecto
void Balloon::disableBounce() {
bouncing_.enabled = false;
bouncing_.reset();
zoomSprite();
void Balloon::disableBounceEffect() {
bounce_effect_.enabled = false;
bounce_effect_.reset();
applyBounceEffect();
}
// Aplica el efecto
void Balloon::updateBounce() {
if (bouncing_.enabled) {
const int INDEX = bouncing_.counter / bouncing_.speed;
bouncing_.horizontal_zoom = bouncing_.horizontal_zoom_values[INDEX];
bouncing_.verical_zoom = bouncing_.vertical_zoom_values[INDEX];
void Balloon::updateBounceEffect() {
if (bounce_effect_.enabled) {
const int INDEX = bounce_effect_.counter / bounce_effect_.speed;
bounce_effect_.horizontal_zoom = bounce_effect_.horizontal_zoom_values[INDEX];
bounce_effect_.verical_zoom = bounce_effect_.vertical_zoom_values[INDEX];
zoomSprite();
applyBounceEffect();
if (++bouncing_.counter / bouncing_.speed >= MAX_BOUNCE) {
disableBounce();
if (++bounce_effect_.counter / bounce_effect_.speed >= MAX_BOUNCE) {
disableBounceEffect();
}
}
}