From 54ef4c85ebc4d7171c3b7a004f14fd96ee219599 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 19 May 2026 17:52:33 +0200 Subject: [PATCH] time-based: Player::setAnimation(dt_s) propaga dt_s als animate() dels sprites (corregeix animacio del jugador a 144Hz) --- source/game/entities/player.cpp | 40 ++++++++++++++++++++++++++++++++- source/game/entities/player.h | 3 ++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/source/game/entities/player.cpp b/source/game/entities/player.cpp index 98e1a2f..030643b 100644 --- a/source/game/entities/player.cpp +++ b/source/game/entities/player.cpp @@ -280,6 +280,44 @@ void Player::setAnimation() { fire_sprite_->setFlip(FLIP_WALK); } +// Establece la animación correspondiente al estado (time-based) +void Player::setAnimation(float dt_s) { + std::string body_coffees; + std::string head_coffees; + if (coffees_ > 0) { + body_coffees = coffees_ == 1 ? "_1C" : "_2C"; + head_coffees = "_1C"; + } + + const std::string POWER_UP = power_up_ ? "_pwr" : ""; + const std::string WALKING = status_walking_ == STATUS_WALKING_STOP ? "stand" : "walk"; + const std::string FIRING = status_firing_ == STATUS_FIRING_UP ? "centershoot" : "sideshoot"; + + const SDL_FlipMode FLIP_WALK = status_walking_ == STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; + const SDL_FlipMode FLIP_FIRE = status_firing_ == STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; + + legs_sprite_->setCurrentAnimation(WALKING); + legs_sprite_->setFlip(FLIP_WALK); + if (status_firing_ == STATUS_FIRING_NO) { + body_sprite_->setCurrentAnimation(WALKING + body_coffees + POWER_UP); + body_sprite_->setFlip(FLIP_WALK); + head_sprite_->setCurrentAnimation(WALKING + head_coffees + POWER_UP); + head_sprite_->setFlip(FLIP_WALK); + } else { + body_sprite_->setCurrentAnimation(FIRING + body_coffees + POWER_UP); + body_sprite_->setFlip(FLIP_FIRE); + head_sprite_->setCurrentAnimation(FIRING + head_coffees + POWER_UP); + head_sprite_->setFlip(FLIP_FIRE); + } + + legs_sprite_->animate(dt_s); + body_sprite_->animate(dt_s); + head_sprite_->animate(dt_s); + + fire_sprite_->animate(dt_s); + fire_sprite_->setFlip(FLIP_WALK); +} + // Obtiene el valor de la variable auto Player::getPosX() const -> int { return int(pos_x_); @@ -357,7 +395,7 @@ void Player::update() { // Actualiza al jugador (time-based) void Player::update(float dt_s) { move(dt_s); - setAnimation(); + setAnimation(dt_s); shiftColliders(); updateCooldown(dt_s); updatePowerUpCounter(dt_s); diff --git a/source/game/entities/player.h b/source/game/entities/player.h index 78806f5..558ca46 100644 --- a/source/game/entities/player.h +++ b/source/game/entities/player.h @@ -30,7 +30,8 @@ class Player { void setPlayerTextures(const std::vector &texture); // Pone las texturas del jugador void setInput(Input::Action input); // Actua en consecuencia de la entrada recibida - void setAnimation(); // Establece la animación correspondiente al estado + void setAnimation(); // Establece la animación correspondiente al estado (frame-based) + void setAnimation(float dt_s); // Establece la animación correspondiente al estado (time-based) [[nodiscard]] auto getPosX() const -> int; // Obtiene el valor de la variable [[nodiscard]] auto getPosY() const -> int; // Obtiene el valor de la variable