#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 { // Escogemos el frame correspondiente de la animación setSpriteClip(animation[index].frames[animation[index].currentFrame]); // Incrementamos 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 el rectangulo para un frame de una animación void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h) { mAnimation[index_animation].frames[index_frame].x = x; mAnimation[index_animation].frames[index_frame].y = y; mAnimation[index_animation].frames[index_frame].w = w; mAnimation[index_animation].frames[index_frame].h = h; } // Establece la velocidad de una animación void AnimatedSprite::setAnimationSpeed(std::string name, int speed) { animation[getIndex(name)].counter = num; mAnimation[index].speed = speed; } // Establece el numero de frames de una animación void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num) { mAnimation[index].numFrames = num; } // Establece si la animación se reproduce en bucle void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop) { mAnimation[index].loop = loop; } // Establece el valor de la variable void AnimatedSprite::setCompleted(Uint8 index, bool value) { mAnimation[index].completed = value; } // Comprueba si ha terminado la animación bool AnimatedSprite::isCompleted(Uint8 index) { return mAnimation[index].completed; } // Devuelve el rectangulo de una animación y frame concreto SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame) { return mAnimation[index_animation].frames[index_frame]; }