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] [animation]
name=walk name=walk
speed=8 speed=8
loop=yes loop=0
frames=0,1,2,3,4,5 frames=0,1,2,3,4,5
[/animation] [/animation]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -18,7 +18,7 @@ private:
std::string name; // Nombre de la animacion std::string name; // Nombre de la animacion
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
int speed; // Velocidad de 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 bool completed; // Indica si ha finalizado la animación
int currentFrame; // Frame actual int currentFrame; // Frame actual
int counter; // Contador para las animaciones int counter; // Contador para las animaciones
@@ -45,8 +45,8 @@ public:
// Establece la velocidad de una animación // Establece la velocidad de una animación
void setAnimationSpeed(std::string name, int speed); void setAnimationSpeed(std::string name, int speed);
// Establece si la animación se reproduce en bucle // Establece el frame al que vuelve la animación al finalizar
void setAnimationLoop(std::string name, bool loop); void setAnimationLoop(std::string name, int loop);
// Establece el valor de la variable // Establece el valor de la variable
void setAnimationCompleted(std::string name, bool value); void setAnimationCompleted(std::string name, bool value);