Actualizado animatedsprite para no requerir frames_per_row
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "const.h"
|
||||
|
||||
#include "animatedsprite.h"
|
||||
|
||||
// Constructor
|
||||
@@ -29,61 +29,72 @@ AnimatedSprite::~AnimatedSprite()
|
||||
int AnimatedSprite::getIndex(std::string name)
|
||||
{
|
||||
int index = -1;
|
||||
for (int i = 0; i < animation.size(); i++)
|
||||
|
||||
for (auto a : animation)
|
||||
{
|
||||
if (animation[i].name == name)
|
||||
index++;
|
||||
if (a.name == name)
|
||||
{
|
||||
index = i;
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
printf("** Warning: could not find \"%s\" animation\n", name.c_str());
|
||||
index = 0;
|
||||
}
|
||||
return index;
|
||||
printf("** Warning: could not find \"%s\" animation\n", name.c_str());
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Calcula el frame correspondiente a la animación
|
||||
void AnimatedSprite::animate()
|
||||
{
|
||||
if (enabled)
|
||||
if (!enabled || animation[currentAnimation].speed == 0)
|
||||
{
|
||||
// Calcula el frame actual a partir del contador
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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 (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
|
||||
{
|
||||
if (animation[currentAnimation].loop == -1)
|
||||
{ // Si no hay loop, deja el último frame
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
||||
animation[currentAnimation].completed = true;
|
||||
}
|
||||
else
|
||||
{ // Si hay loop, vuelve al frame indicado
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
||||
}
|
||||
// Calcula el frame actual a partir del contador
|
||||
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 y coloca el nuevo frame
|
||||
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
|
||||
{
|
||||
if (animation[currentAnimation].loop == -1)
|
||||
{ // Si no hay loop, deja el último frame
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
||||
animation[currentAnimation].completed = true;
|
||||
}
|
||||
// En caso contrario
|
||||
else
|
||||
{
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||
|
||||
// Incrementa el contador de la animacion
|
||||
animation[currentAnimation].counter++;
|
||||
{ // Si hay loop, vuelve al frame indicado
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
||||
}
|
||||
}
|
||||
// En caso contrario
|
||||
else
|
||||
{
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||
|
||||
// Incrementa el contador de la animacion
|
||||
animation[currentAnimation].counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el frame actual de la animación
|
||||
void AnimatedSprite::setCurrentFrame(std::string name, int num)
|
||||
void AnimatedSprite::setCurrentFrame(int num)
|
||||
{
|
||||
animation[getIndex(name)].currentFrame = num;
|
||||
// Descarta valores fuera de rango
|
||||
if (num >= animation[currentAnimation].frames.size())
|
||||
{
|
||||
num = 0;
|
||||
}
|
||||
|
||||
// Cambia el valor de la variable
|
||||
animation[currentAnimation].counter = animation[currentAnimation].speed * num;
|
||||
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||
}
|
||||
|
||||
// Establece el valor del contador
|
||||
@@ -143,7 +154,7 @@ bool AnimatedSprite::load(std::string filePath)
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
|
||||
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||
if (line == "[animation]")
|
||||
{
|
||||
t_animation buffer;
|
||||
@@ -195,7 +206,7 @@ bool AnimatedSprite::load(std::string filePath)
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
// Añade el enemigo al vector de enemigos
|
||||
// Añade la animación al vector de animaciones
|
||||
animation.push_back(buffer);
|
||||
}
|
||||
|
||||
@@ -212,14 +223,23 @@ bool AnimatedSprite::load(std::string filePath)
|
||||
{
|
||||
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "frame_width")
|
||||
{
|
||||
frame_width = std::stoi(line.substr(pos + 1, line.length()));
|
||||
|
||||
// Normaliza valores
|
||||
if (frames_per_row == 0)
|
||||
{
|
||||
frames_per_row = texture->getWidth() / frame_width;
|
||||
}
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "frame_height")
|
||||
{
|
||||
frame_height = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
void animate();
|
||||
|
||||
// Establece el frame actual de la animación
|
||||
void setCurrentFrame(std::string name, int num);
|
||||
void setCurrentFrame(int num);
|
||||
|
||||
// Establece el valor del contador
|
||||
void setAnimationCounter(std::string name, int num);
|
||||
|
||||
Reference in New Issue
Block a user