Actualizado animatedsprite para no requerir frames_per_row
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=6
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=16
|
frame_height=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=6
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=16
|
frame_height=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=6
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=16
|
frame_height=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=6
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=16
|
frame_height=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=8
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=16
|
frame_height=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=1
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=8
|
frame_height=8
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
fullScreenMode=0
|
fullScreenMode=0
|
||||||
windowSize=3
|
windowSize=2
|
||||||
filter=FILTER_NEAREST
|
filter=FILTER_NEAREST
|
||||||
vSync=true
|
vSync=true
|
||||||
integerScale=true
|
integerScale=true
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=4
|
|
||||||
frame_width=320
|
frame_width=320
|
||||||
frame_height=240
|
frame_height=240
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
frames_per_row=14
|
|
||||||
frame_width=16
|
frame_width=16
|
||||||
frame_height=24
|
frame_height=24
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "const.h"
|
|
||||||
#include "animatedsprite.h"
|
#include "animatedsprite.h"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -29,61 +29,72 @@ AnimatedSprite::~AnimatedSprite()
|
|||||||
int AnimatedSprite::getIndex(std::string name)
|
int AnimatedSprite::getIndex(std::string name)
|
||||||
{
|
{
|
||||||
int index = -1;
|
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());
|
||||||
{
|
|
||||||
printf("** Warning: could not find \"%s\" animation\n", name.c_str());
|
return -1;
|
||||||
index = 0;
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcula el frame correspondiente a la animación
|
// Calcula el frame correspondiente a la animación
|
||||||
void AnimatedSprite::animate()
|
void AnimatedSprite::animate()
|
||||||
{
|
{
|
||||||
if (enabled)
|
if (!enabled || animation[currentAnimation].speed == 0)
|
||||||
{
|
{
|
||||||
// Calcula el frame actual a partir del contador
|
return;
|
||||||
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
|
}
|
||||||
|
|
||||||
// Si alcanza el final de la animación, reinicia el contador de la animación
|
// Calcula el frame actual a partir del contador
|
||||||
// en función de la variable loop y coloca el nuevo frame
|
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
|
||||||
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
|
|
||||||
{
|
// Si alcanza el final de la animación, reinicia el contador de la animación
|
||||||
if (animation[currentAnimation].loop == -1)
|
// en función de la variable loop y coloca el nuevo frame
|
||||||
{ // Si no hay loop, deja el último frame
|
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
|
||||||
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
{
|
||||||
animation[currentAnimation].completed = true;
|
if (animation[currentAnimation].loop == -1)
|
||||||
}
|
{ // Si no hay loop, deja el último frame
|
||||||
else
|
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
||||||
{ // Si hay loop, vuelve al frame indicado
|
animation[currentAnimation].completed = true;
|
||||||
animation[currentAnimation].counter = 0;
|
|
||||||
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// En caso contrario
|
|
||||||
else
|
else
|
||||||
{
|
{ // Si hay loop, vuelve al frame indicado
|
||||||
// Escoge el frame correspondiente de la animación
|
animation[currentAnimation].counter = 0;
|
||||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
||||||
|
|
||||||
// Incrementa el contador de la animacion
|
|
||||||
animation[currentAnimation].counter++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 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
|
// 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
|
// Establece el valor del contador
|
||||||
@@ -143,7 +154,7 @@ bool AnimatedSprite::load(std::string filePath)
|
|||||||
printf("Reading file %s\n", filename.c_str());
|
printf("Reading file %s\n", filename.c_str());
|
||||||
while (std::getline(file, line))
|
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]")
|
if (line == "[animation]")
|
||||||
{
|
{
|
||||||
t_animation buffer;
|
t_animation buffer;
|
||||||
@@ -195,7 +206,7 @@ bool AnimatedSprite::load(std::string filePath)
|
|||||||
}
|
}
|
||||||
} while (line != "[/animation]");
|
} while (line != "[/animation]");
|
||||||
|
|
||||||
// Añade el enemigo al vector de enemigos
|
// Añade la animación al vector de animaciones
|
||||||
animation.push_back(buffer);
|
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()));
|
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (line.substr(0, pos) == "frame_width")
|
else if (line.substr(0, pos) == "frame_width")
|
||||||
{
|
{
|
||||||
frame_width = std::stoi(line.substr(pos + 1, line.length()));
|
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")
|
else if (line.substr(0, pos) == "frame_height")
|
||||||
{
|
{
|
||||||
frame_height = std::stoi(line.substr(pos + 1, line.length()));
|
frame_height = std::stoi(line.substr(pos + 1, line.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public:
|
|||||||
void animate();
|
void animate();
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Establece el frame actual de la animación
|
||||||
void setCurrentFrame(std::string name, int num);
|
void setCurrentFrame(int num);
|
||||||
|
|
||||||
// Establece el valor del contador
|
// Establece el valor del contador
|
||||||
void setAnimationCounter(std::string name, int num);
|
void setAnimationCounter(std::string name, int num);
|
||||||
|
|||||||
Reference in New Issue
Block a user