Implementadas las animaciones en el jugador

This commit is contained in:
2022-08-13 12:14:31 +02:00
parent e85f138be5
commit a1eaf8af07
7 changed files with 68 additions and 74 deletions

View File

@@ -2,11 +2,14 @@
#include "animatedsprite.h"
// Constructor
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file)
{
// Copia los punteros
setTexture(texture);
setRenderer(renderer);
// Carga las animaciones
load(file);
}
// Destructor
@@ -34,32 +37,30 @@ int AnimatedSprite::getIndex(std::string name)
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate(std::string name)
void AnimatedSprite::animate()
{
if (mEnabled)
{
const int index = getIndex(name);
// Calcula el frame actual a partir del contador
animation[index].currentFrame = animation[index].counter / animation[index].speed;
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
// Si alcanza el final de la animación, reinicia el contador de la animación
// en función de la variable loop
if (animation[index].currentFrame >= animation[index].frames.size())
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
{
if (animation[index].loop)
animation[index].counter = 0;
if (animation[currentAnimation].loop)
animation[currentAnimation].counter = 0;
else
animation[index].currentFrame = animation[index].frames.size();
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
}
// En caso contrario
else
{
// Escoge el frame correspondiente de la animación
setSpriteClip(animation[index].frames[animation[index].currentFrame]);
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
// Incrementa el contador de la animacion
animation[index].counter++;
animation[currentAnimation].counter++;
}
}
}
@@ -231,4 +232,24 @@ bool AnimatedSprite::load(std::string filePath)
}
return success;
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(std::string name)
{
const int newAnimation = getIndex(name);
if (currentAnimation != newAnimation)
{
currentAnimation = newAnimation;
animation[currentAnimation].currentFrame = 0;
animation[currentAnimation].counter = 0;
animation[currentAnimation].completed = false;
}
}
// Actualiza las variables del objeto
void AnimatedSprite::update()
{
animate();
MovingSprite::update();
}