From 1a6ef794664584084a5ee0aeb0973894240f602b Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 23 Sep 2025 08:29:29 +0200 Subject: [PATCH] revisat moving i animated sprite --- deltatime_cleanup_plan.md | 17 ++++++++++++++--- source/animated_sprite.cpp | 12 ++---------- source/animated_sprite.h | 5 ++--- source/moving_sprite.cpp | 5 +---- source/moving_sprite.h | 1 - 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/deltatime_cleanup_plan.md b/deltatime_cleanup_plan.md index a7cd4f8..44b86bd 100644 --- a/deltatime_cleanup_plan.md +++ b/deltatime_cleanup_plan.md @@ -11,6 +11,7 @@ - [ ] Convertir a timers basados en deltaTime - [ ] Eliminar variables como `counter_`, `frame_counter_`, etc. - [ ] Patrón: `if (counter-- <= 0)` → `if (timer >= DURATION_MS)` +- [ ] **IMPORTANTE**: Todos los contadores han de ser crecientes, de cero hasta llegar a la constante que define su tope ### 2. Revisar Inicializaciones de Aceleraciones MovingSprite - [ ] Buscar todas las llamadas a `setAccelX()`, `setAccelY()` @@ -28,8 +29,16 @@ - [ ] Buscar `1000.0f / 60.0f` hardcodeado - [ ] Buscar `16.67f` hardcodeado - [ ] Buscar comentarios con "frame" o "60fps" +- [ ] Localizar magic numbers y convertirlos a constantes con nombres descriptivos +- [ ] **IMPORTANTE**: Modificar speed en ficheros .ani - está en frames, hay que pasarlo a milisegundos (multiplicar speed por 1000/60 = 16.67) -### 5. Archivos Prioritarios a Revisar +### 5. Cambio de Unidades de Tiempo en sections/* +- [ ] Cambiar el cálculo de deltatime en source/sections/* para que devuelva segundos (float) en lugar de milisegundos +- [ ] Cambiar velocidades de pixeles/ms a pixeles/segundos para evitar valores absurdamente pequeños +- [ ] Cambiar aceleraciones de pixeles/ms² a pixeles/segundos² +- [ ] Actualizar todas las constantes de tiempo en archivos de sections + +### 6. Archivos Prioritarios a Revisar - [ ] **player.cpp** - puede tener aceleraciones - [ ] **balloon.cpp** - contadores de estado - [ ] **stage.cpp** - timers de nivel @@ -37,7 +46,7 @@ - [ ] **tabe.cpp** - movimiento del protagonista - [ ] **sections/*.cpp** - transiciones y efectos -### 6. Validación Final +### 7. Validación Final - [ ] Compilar sin warnings - [ ] Probar gameplay normal - [ ] Probar modo demo @@ -57,7 +66,9 @@ rg "60\.0|16\.67|1000\.0.*60" ``` ## Notas -- Mantener consistencia: velocidades en `pixels/ms`, aceleraciones en `pixels/ms²` +- **Para archivos sections/***: velocidades en `pixels/segundos`, aceleraciones en `pixels/segundos²` +- **Para resto del código**: mantener velocidades en `pixels/ms`, aceleraciones en `pixels/ms²` +- Todos los contadores deben ser crecientes (0 → constante_tope) - Documentar las conversiones en comentarios - Crear constantes para valores repetidos - Evitar números mágicos \ No newline at end of file diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 201c80a..20fa412 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -92,15 +92,12 @@ void AnimatedSprite::animate(float deltaTime) { return; } - // Convertir speed (frames) a tiempo: speed frames = speed * (1000ms/60fps) milisegundos - float frameTime = static_cast(animations_[current_animation_].speed) * (1000.0f / 60.0f); - // Acumular tiempo transcurrido animations_[current_animation_].time_accumulator += deltaTime; // Verificar si es momento de cambiar frame - if (animations_[current_animation_].time_accumulator >= frameTime) { - animations_[current_animation_].time_accumulator -= frameTime; + if (animations_[current_animation_].time_accumulator >= animations_[current_animation_].speed) { + animations_[current_animation_].time_accumulator -= animations_[current_animation_].speed; animations_[current_animation_].current_frame++; // Si alcanza el final de la animación @@ -132,12 +129,10 @@ void AnimatedSprite::setCurrentAnimation(const std::string& name, bool reset) { current_animation_ = NEW_ANIMATION; if (reset) { animations_[current_animation_].current_frame = 0; - animations_[current_animation_].counter = 0; animations_[current_animation_].time_accumulator = 0.0f; animations_[current_animation_].completed = false; } else { animations_[current_animation_].current_frame = std::min(animations_[OLD_ANIMATION].current_frame, animations_[current_animation_].frames.size() - 1); - animations_[current_animation_].counter = animations_[OLD_ANIMATION].counter; animations_[current_animation_].time_accumulator = animations_[OLD_ANIMATION].time_accumulator; animations_[current_animation_].completed = animations_[OLD_ANIMATION].completed; } @@ -153,12 +148,10 @@ void AnimatedSprite::setCurrentAnimation(int index, bool reset) { current_animation_ = NEW_ANIMATION; if (reset) { animations_[current_animation_].current_frame = 0; - animations_[current_animation_].counter = 0; animations_[current_animation_].time_accumulator = 0.0f; animations_[current_animation_].completed = false; } else { animations_[current_animation_].current_frame = std::min(animations_[OLD_ANIMATION].current_frame, animations_[current_animation_].frames.size()); - animations_[current_animation_].counter = animations_[OLD_ANIMATION].counter; animations_[current_animation_].time_accumulator = animations_[OLD_ANIMATION].time_accumulator; animations_[current_animation_].completed = animations_[OLD_ANIMATION].completed; } @@ -175,7 +168,6 @@ void AnimatedSprite::update(float deltaTime) { // Reinicia la animación void AnimatedSprite::resetAnimation() { animations_[current_animation_].current_frame = 0; - animations_[current_animation_].counter = 0; animations_[current_animation_].time_accumulator = 0.0f; animations_[current_animation_].completed = false; animations_[current_animation_].paused = false; diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 46584ba..fad43db 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -17,15 +17,14 @@ class Texture; // --- Estructuras --- struct Animation { - static constexpr int DEFAULT_SPEED = 5; + static constexpr float DEFAULT_SPEED = 80.0F; std::string name; // Nombre de la animación std::vector frames; // Frames que componen la animación - int speed{DEFAULT_SPEED}; // Velocidad de reproducción (frame-based) + float speed{DEFAULT_SPEED}; // Velocidad de reproducción (ms entre frames) int loop{0}; // Frame de vuelta al terminar (-1 para no repetir) bool completed{false}; // Indica si la animación ha finalizado size_t current_frame{0}; // Frame actual en reproducción - int counter{0}; // Contador para la animación (frame-based) float time_accumulator{0.0f}; // Acumulador de tiempo para animaciones time-based bool paused{false}; // La animación no avanza diff --git a/source/moving_sprite.cpp b/source/moving_sprite.cpp index 2cb128d..49d4c8c 100644 --- a/source/moving_sprite.cpp +++ b/source/moving_sprite.cpp @@ -81,16 +81,13 @@ void MovingSprite::render() { // Establece la rotacion (time-based) void MovingSprite::rotate(float deltaTime) { if (rotate_.enabled) { - // DeltaTime puro: velocidad de rotación debe estar en unidades/ms - float rotationFrameTime = static_cast(rotate_.speed) * (1000.0f / 60.0f); - rotate_.angle += rotate_.amount * (deltaTime / rotationFrameTime); + rotate_.angle += rotate_.amount * deltaTime; } } // Activa o desactiva el efecto de rotación void MovingSprite::setRotate(bool enable) { rotate_.enabled = enable; - rotate_.counter = 0; } // Establece la posición y_ el tamaño del objeto diff --git a/source/moving_sprite.h b/source/moving_sprite.h index 0cdf014..7ab190e 100644 --- a/source/moving_sprite.h +++ b/source/moving_sprite.h @@ -15,7 +15,6 @@ class MovingSprite : public Sprite { // --- Estructuras --- struct Rotate { bool enabled{false}; // Indica si ha de rotar - int counter{0}; // Contador int speed{1}; // Velocidad de giro double angle{0.0}; // Ángulo para dibujarlo float amount{0.0F}; // Cantidad de grados a girar en cada iteración