AnimatedSprite: posibilitat de resetejar o no el estat de la animació en setCurrentAnimation();
This commit is contained in:
@@ -82,7 +82,7 @@ void AnimatedSprite::animate()
|
|||||||
|
|
||||||
// Si alcanza el final de la animación, reinicia el contador de la animación
|
// 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
|
// 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)
|
if (animations_[current_animation_].loop == -1)
|
||||||
{ // Si no hay loop, deja el último frame
|
{ // Si no hay loop, deja el último frame
|
||||||
@@ -113,28 +113,48 @@ bool AnimatedSprite::animationIsCompleted()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Establece la animacion actual
|
// 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);
|
const auto NEW_ANIMATION = getIndex(name);
|
||||||
if (current_animation_ != new_animation)
|
if (current_animation_ != NEW_ANIMATION)
|
||||||
{
|
{
|
||||||
current_animation_ = new_animation;
|
const auto OLD_ANIMATION = current_animation_;
|
||||||
animations_[current_animation_].current_frame = 0;
|
current_animation_ = NEW_ANIMATION;
|
||||||
animations_[current_animation_].counter = 0;
|
if (reset)
|
||||||
animations_[current_animation_].completed = false;
|
{
|
||||||
|
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
|
// Establece la animacion actual
|
||||||
void AnimatedSprite::setCurrentAnimation(int index)
|
void AnimatedSprite::setCurrentAnimation(int index, bool reset)
|
||||||
{
|
{
|
||||||
const auto new_animation = index;
|
const auto NEW_ANIMATION = index;
|
||||||
if (current_animation_ != new_animation)
|
if (current_animation_ != NEW_ANIMATION)
|
||||||
{
|
{
|
||||||
current_animation_ = new_animation;
|
const auto OLD_ANIMATION = current_animation_;
|
||||||
animations_[current_animation_].current_frame = 0;
|
current_animation_ = NEW_ANIMATION;
|
||||||
animations_[current_animation_].counter = 0;
|
if (reset)
|
||||||
animations_[current_animation_].completed = false;
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ struct Animation
|
|||||||
int speed; // Velocidad de reproducción
|
int speed; // Velocidad de reproducción
|
||||||
int loop; // Frame de vuelta al terminar (-1 para no repetir)
|
int loop; // Frame de vuelta al terminar (-1 para no repetir)
|
||||||
bool completed; // Indica si la animación ha finalizado
|
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
|
int counter; // Contador para la animación
|
||||||
|
|
||||||
Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
|
Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
|
||||||
@@ -44,10 +44,10 @@ public:
|
|||||||
void update() override; // Actualiza la animación
|
void update() override; // Actualiza la animación
|
||||||
|
|
||||||
// --- Control de animaciones ---
|
// --- Control de animaciones ---
|
||||||
void setCurrentAnimation(const std::string &name = "default"); // Establece la animación por nombre
|
void setCurrentAnimation(const std::string &name = "default", bool reset = true); // Establece la animación por nombre
|
||||||
void setCurrentAnimation(int index = 0); // Establece la animación por índice
|
void setCurrentAnimation(int index = 0, bool reset = true); // Establece la animación por índice
|
||||||
void resetAnimation(); // Reinicia la animación actual
|
void resetAnimation(); // Reinicia la animación actual
|
||||||
void setAnimationSpeed(size_t value); // Establece la velocidad de la animación
|
void setAnimationSpeed(size_t value); // Establece la velocidad de la animación
|
||||||
|
|
||||||
// --- Consultas ---
|
// --- Consultas ---
|
||||||
bool animationIsCompleted(); // Comprueba si la animación ha terminado
|
bool animationIsCompleted(); // Comprueba si la animación ha terminado
|
||||||
|
|||||||
Reference in New Issue
Block a user