AnimatedSprite: posibilitat de resetejar o no el estat de la animació en setCurrentAnimation();

This commit is contained in:
2025-07-11 22:57:51 +02:00
parent 7ac46876ca
commit 66d55061dd
2 changed files with 40 additions and 20 deletions

View File

@@ -82,7 +82,7 @@ void AnimatedSprite::animate()
// Si alcanza el final de la animación, reinicia el contador de la animación
// en función de la variable loop y coloca el nuevo frame
if (animations_[current_animation_].current_frame >= (int)animations_[current_animation_].frames.size())
if (animations_[current_animation_].current_frame >= animations_[current_animation_].frames.size())
{
if (animations_[current_animation_].loop == -1)
{ // Si no hay loop, deja el último frame
@@ -113,29 +113,49 @@ bool AnimatedSprite::animationIsCompleted()
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(const std::string &name)
void AnimatedSprite::setCurrentAnimation(const std::string &name, bool reset)
{
const auto new_animation = getIndex(name);
if (current_animation_ != new_animation)
const auto NEW_ANIMATION = getIndex(name);
if (current_animation_ != NEW_ANIMATION)
{
const auto OLD_ANIMATION = current_animation_;
current_animation_ = NEW_ANIMATION;
if (reset)
{
current_animation_ = new_animation;
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
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_].completed = animations_[OLD_ANIMATION].completed;
}
}
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(int index)
void AnimatedSprite::setCurrentAnimation(int index, bool reset)
{
const auto new_animation = index;
if (current_animation_ != new_animation)
const auto NEW_ANIMATION = index;
if (current_animation_ != NEW_ANIMATION)
{
const auto OLD_ANIMATION = current_animation_;
current_animation_ = NEW_ANIMATION;
if (reset)
{
current_animation_ = new_animation;
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
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_].completed = animations_[OLD_ANIMATION].completed;
}
}
}
// Actualiza las variables del objeto

View File

@@ -18,7 +18,7 @@ struct Animation
int speed; // Velocidad de reproducción
int loop; // Frame de vuelta al terminar (-1 para no repetir)
bool completed; // Indica si la animación ha finalizado
int current_frame; // Frame actual en reproducción
size_t current_frame; // Frame actual en reproducción
int counter; // Contador para la animación
Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
@@ -44,8 +44,8 @@ public:
void update() override; // Actualiza la animación
// --- Control de animaciones ---
void setCurrentAnimation(const std::string &name = "default"); // Establece la animación por nombre
void setCurrentAnimation(int index = 0); // Establece la animación por índice
void setCurrentAnimation(const std::string &name = "default", bool reset = true); // Establece la animación por nombre
void setCurrentAnimation(int index = 0, bool reset = true); // Establece la animación por índice
void resetAnimation(); // Reinicia la animación actual
void setAnimationSpeed(size_t value); // Establece la velocidad de la animación