Modificado el loop de las animaciones

This commit is contained in:
2022-08-22 17:42:26 +02:00
parent 7c7dcf1a12
commit debdc861d5
7 changed files with 23 additions and 25 deletions

View File

@@ -5,6 +5,6 @@ tile_height=16
[animation]
name=walk
speed=8
loop=yes
loop=0
frames=0,1,2,3,4,5
[/animation]

View File

@@ -5,6 +5,6 @@ tile_height=16
[animation]
name=walk
speed=8
loop=yes
loop=0
frames=0,1,2,3,4,5
[/animation]

View File

@@ -5,6 +5,6 @@ tile_height=16
[animation]
name=walk
speed=6
loop=yes
loop=0
frames=0,1,2,3,4,5
[/animation]

View File

@@ -5,6 +5,6 @@ tile_height=16
[animation]
name=walk
speed=8
loop=yes
loop=0
frames=0,1,2,3,4,5
[/animation]

View File

@@ -5,27 +5,27 @@ tile_height=24
[animation]
name=stand
speed=8
loop=yes
loop=0
frames=0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,3,4,5,4,5,5,5,6,6,7,7,0,0
[/animation]
[animation]
name=walk
speed=4
loop=yes
loop=0
frames=8,9,10,10,9,8,11,12,13,13,14,15
[/animation]
[animation]
name=jump
speed=10
loop=no
loop=-1
frames=16,17,18,17,16
[/animation]
[animation]
name=death
speed=10
loop=no
loop=-1
frames=24,25,26,27,28,29,30,31
[/animation]

View File

@@ -45,13 +45,18 @@ void AnimatedSprite::animate()
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
// en función de la variable loop y coloca el nuevo frame
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
{
if (animation[currentAnimation].loop)
animation[currentAnimation].counter = 0;
else
if (animation[currentAnimation].loop == -1)
{ // Si no hay loop, deja el último frame
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
}
else
{ // Si hay loop, vuelve al frame indicado
animation[currentAnimation].counter = 0;
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
}
}
// En caso contrario
else
@@ -84,7 +89,7 @@ void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
}
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(std::string name, bool loop)
void AnimatedSprite::setAnimationLoop(std::string name, int loop)
{
animation[getIndex(name)].loop = loop;
}
@@ -155,14 +160,7 @@ bool AnimatedSprite::load(std::string filePath)
}
else if (line.substr(0, pos) == "loop")
{
if (line.substr(pos + 1, line.length()) == "yes" || line.substr(pos + 1, line.length()) == "true")
{
buffer.loop = true;
}
else
{
buffer.loop = false;
}
buffer.loop = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frames")
{

View File

@@ -18,7 +18,7 @@ private:
std::string name; // Nombre de la animacion
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
int speed; // Velocidad de la animación
bool loop; // Indica si la animación se reproduce en bucle
int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
bool completed; // Indica si ha finalizado la animación
int currentFrame; // Frame actual
int counter; // Contador para las animaciones
@@ -28,7 +28,7 @@ private:
public:
// Constructor
AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file="");
AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "");
// Destructor
~AnimatedSprite();
@@ -45,8 +45,8 @@ public:
// Establece la velocidad de una animación
void setAnimationSpeed(std::string name, int speed);
// Establece si la animación se reproduce en bucle
void setAnimationLoop(std::string name, bool loop);
// Establece el frame al que vuelve la animación al finalizar
void setAnimationLoop(std::string name, int loop);
// Establece el valor de la variable
void setAnimationCompleted(std::string name, bool value);