Implementar cambio de sprite dinámico con hot-swap (Tecla N)

Sistema de múltiples texturas:
- Carga ball.png (10x10) y ball_small.png (6x6) al inicio
- Variable current_ball_size_ obtiene tamaño desde texture->getWidth()
- Eliminar constante BALL_SIZE hardcoded

Cambio de tamaño con ajuste de posiciones:
- updateBallSizes() ajusta pos según gravedad y superficie
- DOWN: mueve Y hacia abajo si crece
- UP: mueve Y hacia arriba si crece
- LEFT/RIGHT: mueve X correspondiente
- Solo ajusta pelotas en superficie (isOnSurface())

Ball class actualizada:
- Constructor recibe ball_size como parámetro
- updateSize(new_size): actualiza hitbox y sprite
- setTexture(texture): cambia textura del sprite
- setPosition() usa setRotoBallScreenPosition()

Sprite class:
- Añadido setTexture() inline para hot-swap

Tecla N:
- Cicla entre texturas disponibles
- Actualiza todas las pelotas sin reiniciar física
- Texto informativo "SPRITE: NORMAL" / "SPRITE: SMALL"

Fix bug initBalls():
- Ahora usa current_ball_size_ en constructor
- Pelotas nuevas tienen tamaño correcto según textura activa

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-04 08:08:00 +02:00
parent 6bb814e61c
commit dcd05e502f
6 changed files with 133 additions and 12 deletions

View File

@@ -4,7 +4,7 @@
#include <cmath> // for fabs
#include "defines.h" // for BALL_SIZE, Color, SCREEN_HEIGHT, GRAVITY_FORCE
#include "defines.h" // for Color, SCREEN_HEIGHT, GRAVITY_FORCE
class Texture;
// Función auxiliar para generar pérdida aleatoria en rebotes
@@ -22,15 +22,15 @@ float generateLateralLoss() {
}
// Constructor
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, GravityDirection gravity_dir, float mass_factor)
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir, float mass_factor)
: sprite_(std::make_unique<Sprite>(texture)),
pos_({x, 0.0f, BALL_SIZE, BALL_SIZE}) {
pos_({x, 0.0f, static_cast<float>(ball_size), static_cast<float>(ball_size)}) {
// Convertir velocidades de píxeles/frame a píxeles/segundo (multiplicar por 60)
vx_ = vx * 60.0f;
vy_ = vy * 60.0f;
sprite_->setPos({pos_.x, pos_.y});
sprite_->setSize(BALL_SIZE, BALL_SIZE);
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
sprite_->setSize(ball_size, ball_size);
sprite_->setClip({0.0f, 0.0f, static_cast<float>(ball_size), static_cast<float>(ball_size)});
color_ = color;
// Convertir gravedad de píxeles/frame² a píxeles/segundo² (multiplicar por 60²)
gravity_force_ = GRAVITY_FORCE * 60.0f * 60.0f;
@@ -378,4 +378,20 @@ void Ball::applyRotoBallForce(float target_x, float target_y, float sphere_radiu
// Actualizar sprite para renderizado
sprite_->setPos({pos_.x, pos_.y});
}
// Sistema de cambio de sprite dinámico
void Ball::updateSize(int new_size) {
// Actualizar tamaño del hitbox
pos_.w = static_cast<float>(new_size);
pos_.h = static_cast<float>(new_size);
// Actualizar sprite
sprite_->setSize(new_size, new_size);
sprite_->setClip({0.0f, 0.0f, static_cast<float>(new_size), static_cast<float>(new_size)});
}
void Ball::setTexture(std::shared_ptr<Texture> texture) {
// Actualizar textura del sprite
sprite_->setTexture(texture);
}