Actualizado animatedsprite para no requerir frames_per_row

This commit is contained in:
2022-09-22 08:19:16 +02:00
parent 170b245573
commit e517ef62c8
11 changed files with 60 additions and 48 deletions

View File

@@ -1,4 +1,3 @@
frames_per_row=6
frame_width=16
frame_height=16

View File

@@ -1,4 +1,3 @@
frames_per_row=6
frame_width=16
frame_height=16

View File

@@ -1,4 +1,3 @@
frames_per_row=6
frame_width=16
frame_height=16

View File

@@ -1,4 +1,3 @@
frames_per_row=6
frame_width=16
frame_height=16

View File

@@ -1,4 +1,3 @@
frames_per_row=8
frame_width=16
frame_height=16

View File

@@ -1,4 +1,3 @@
frames_per_row=1
frame_width=16
frame_height=8

View File

@@ -1,5 +1,5 @@
fullScreenMode=0
windowSize=3
windowSize=2
filter=FILTER_NEAREST
vSync=true
integerScale=true

View File

@@ -1,4 +1,3 @@
frames_per_row=4
frame_width=320
frame_height=240

View File

@@ -1,4 +1,3 @@
frames_per_row=14
frame_width=16
frame_height=24

View File

@@ -1,4 +1,4 @@
#include "const.h"
#include "animatedsprite.h"
// Constructor
@@ -29,27 +29,29 @@ 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;
return -1;
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate()
{
if (enabled)
if (!enabled || animation[currentAnimation].speed == 0)
{
return;
}
// Calcula el frame actual a partir del contador
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
@@ -78,12 +80,21 @@ void AnimatedSprite::animate()
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());

View File

@@ -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);