160 lines
3.5 KiB
C++
160 lines
3.5 KiB
C++
#include "const.h"
|
|
#include "animatedsprite.h"
|
|
|
|
// Constructor
|
|
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
|
|
{
|
|
// Copia los punteros
|
|
setTexture(texture);
|
|
setRenderer(renderer);
|
|
}
|
|
|
|
// Destructor
|
|
AnimatedSprite::~AnimatedSprite()
|
|
{
|
|
}
|
|
|
|
// Obtiene el indice de la animación a partir del nombre
|
|
int AnimatedSprite::getIndex(std::string name)
|
|
{
|
|
int result = -1;
|
|
for (int i = 0; i < animation.size(), i++)
|
|
{
|
|
if (animation[i].name == name)
|
|
{
|
|
result = i;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Calcula el frame correspondiente a la animación
|
|
void AnimatedSprite::animate(std::string name)
|
|
{
|
|
if (mEnabled)
|
|
{
|
|
const int index = getIndex(name);
|
|
|
|
// Calcula el frame actual a partir del contador
|
|
animation[index].currentFrame = animation[index].counter / animation[index].speed;
|
|
|
|
// Si alcanza el final de la animación, reinicia el contador de la animación
|
|
// en función de la variable loop
|
|
if (animation[index].currentFrame >= animation[index].frames.size())
|
|
{
|
|
if (animation[index].loop)
|
|
animation[index].counter = 0;
|
|
else
|
|
animation[index].currentFrame = animation[index].frames.size();
|
|
}
|
|
// En caso contrario
|
|
else
|
|
{
|
|
// Escoge el frame correspondiente de la animación
|
|
setSpriteClip(animation[index].frames[animation[index].currentFrame]);
|
|
|
|
// Incrementa el contador de la animacion
|
|
animation[index].counter++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Establece el frame actual de la animación
|
|
void AnimatedSprite::setCurrentFrame(std::string name, int num)
|
|
{
|
|
animation[getIndex(name)].currentFrame = num;
|
|
}
|
|
|
|
// Establece el valor del contador
|
|
void AnimatedSprite::setAnimationCounter(std::string name, int num)
|
|
{
|
|
animation[getIndex(name)].counter = num;
|
|
}
|
|
|
|
// Establece la velocidad de una animación
|
|
void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
|
|
{
|
|
animation[getIndex(name)].counter = speed;
|
|
}
|
|
|
|
// Establece si la animación se reproduce en bucle
|
|
void AnimatedSprite::setAnimationLoop(std::string name, bool loop)
|
|
{
|
|
animation[getIndex(name)].loop = loop;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void AnimatedSprite::setCompleted(std::string name, bool value)
|
|
{
|
|
animation[getIndex(name)].completed = value;
|
|
}
|
|
|
|
// Comprueba si ha terminado la animación
|
|
bool AnimatedSprite::isCompleted(std::string name)
|
|
{
|
|
return animation[getIndex(name)].completed;
|
|
}
|
|
|
|
// Devuelve el rectangulo de una animación y frame concreto
|
|
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
|
|
{
|
|
return animation[getIndex(name)].frames[index];
|
|
}
|
|
|
|
// Carga la animación desde un fichero
|
|
bool AnimatedSprite::load(std::string filePath)
|
|
{
|
|
}
|
|
|
|
// Asigna variables a partir de dos cadenas
|
|
bool AnimatedSprite::setVars(int index, std::string var, std::string value)
|
|
{
|
|
// Indicador de éxito en la asignación
|
|
bool success = true;
|
|
|
|
if (var == "name")
|
|
{
|
|
animation[index].name = value;
|
|
}
|
|
else if (var == "speed")
|
|
{
|
|
animation[index].speed = std::stoi(value);
|
|
}
|
|
else if (var == "loop")
|
|
{
|
|
if (value == "yes" || value == "true")
|
|
{
|
|
animation[index].loop = true;
|
|
}
|
|
else
|
|
{
|
|
animation[index].loop = false;
|
|
}
|
|
}
|
|
else if (var == "frames")
|
|
{
|
|
const int w = 16;
|
|
const int h = 24;
|
|
const int png_width_tiles = 8;
|
|
// Se introducen los valores separados por comas en un vector
|
|
std::stringstream ss(value);
|
|
std::string tmp;
|
|
SDL_Rect rect = {0, 0, w, h};
|
|
while (getline(ss, tmp, ','))
|
|
{
|
|
int num_tile = std::stoi(tmp);
|
|
rect.x = (num_tile % png_width_tiles) * w;
|
|
rect.y = (num_tile / png_width_tiles) * h;
|
|
animation[index].frames.push_back(rect);
|
|
}
|
|
}
|
|
else if (var == "")
|
|
{
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
}
|
|
|
|
return success;
|
|
} |