diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 3ad7cf0..0ce978f 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -7,8 +7,6 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer) // Copia los punteros setTexture(texture); setRenderer(renderer); - - init(); } // Destructor @@ -16,66 +14,61 @@ AnimatedSprite::~AnimatedSprite() { } -// Iniciador -void AnimatedSprite::init() +// Obtiene el indice de la animación a partir del nombre +int AnimatedSprite::getIndex(std::string name) { - for (int i = 0; i < 20; i++) + int result = -1; + for (int i = 0; i < animation.size(), i++) { - mAnimation[i].numFrames = 0; - mAnimation[i].speed = 0; - mAnimation[i].loop = true; - mAnimation[i].completed = false; - for (int j = 0; i < 20; i++) + if (animation[i].name == name) { - mAnimation[i].frames[j].x = 0; - mAnimation[i].frames[j].y = 0; - mAnimation[i].frames[j].w = 0; - mAnimation[i].frames[j].h = 0; + result = i; } } - mCurrentFrame = 0; - mAnimationCounter = 0; + return result; } // Calcula el frame correspondiente a la animación -void AnimatedSprite::animate(int index) +void AnimatedSprite::animate(std::string name) { if (mEnabled) { - // Calculamos el frame actual a partir del contador - mCurrentFrame = mAnimationCounter / mAnimation[index].speed; + const int index = getIndex(name); - // Si alcanzamos el final de la animación, reiniciamos el contador de la animación + // 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 (mCurrentFrame >= mAnimation[index].numFrames) + if (animation[index].currentFrame >= animation[index].frames.size()) { - if (mAnimation[index].loop) - mAnimationCounter = 0; + if (animation[index].loop) + animation[index].counter = 0; else - mCurrentFrame = mAnimation[index].numFrames; + animation[index].currentFrame = animation[index].frames.size(); } // En caso contrario else { // Escogemos el frame correspondiente de la animación - setSpriteClip(mAnimation[index].frames[mCurrentFrame]); + setSpriteClip(animation[index].frames[animation[index].currentFrame]); // Incrementamos el contador de la animacion - mAnimationCounter++; + animation[index].counter++; } } } // Establece el frame actual de la animación -void AnimatedSprite::setCurrentFrame(Uint8 num) +void AnimatedSprite::setCurrentFrame(std::string name, int num) { - mCurrentFrame = num; + animation[getIndex(name)].currentFrame = num; } // Establece el valor del contador -void AnimatedSprite::setAnimationCounter(Uint16 num) +void AnimatedSprite::setAnimationCounter(std::string name, int num) { - mAnimationCounter = num; + animation[getIndex(name)].counter = num; } // Establece el rectangulo para un frame de una animación @@ -88,8 +81,9 @@ void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame } // Establece la velocidad de una animación -void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed) +void AnimatedSprite::setAnimationSpeed(std::string name, int speed) { + animation[getIndex(name)].counter = num; mAnimation[index].speed = speed; } diff --git a/source/animatedsprite.h b/source/animatedsprite.h index 81c4a83..6e13097 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -1,6 +1,8 @@ #pragma once #include #include "movingsprite.h" +#include +#include #ifndef ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H @@ -9,18 +11,17 @@ class AnimatedSprite : public MovingSprite { private: - struct sAnimation + struct t_animation { - SDL_Rect frames[MAX_FRAMES]; // Cada uno de los frames que componen la animación - Uint8 numFrames; // Numero de frames que componen la animación - Uint8 speed; // Velocidad de la animación - bool loop; // Indica si la animación se reproduce en bucle - bool completed; // Indica si ha finalizado la animación + std::string name; // Nombre de la animacion + std::vector frames; // Cada uno de los frames que componen la animación + int speed; // Velocidad de la animación + bool loop; // Indica si la animación se reproduce en bucle + bool completed; // Indica si ha finalizado la animación + int currentFrame; // Frame actual + int counter; // Contador para las animaciones }; - sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las diferentes animaciones - - Uint8 mCurrentFrame; // Frame actual - Uint16 mAnimationCounter; // Contador para las animaciones + std::vector animation; // Vector con las diferentes animaciones public: // Constructor @@ -29,23 +30,20 @@ public: // Destructor ~AnimatedSprite(); - // Iniciador - void init(); - // Calcula el frame correspondiente a la animación - void animate(int index); + void animate(std::string name); // Establece el frame actual de la animación - void setCurrentFrame(Uint8 num); + void setCurrentFrame(std::string name, int num); // Establece el valor del contador - void setAnimationCounter(Uint16 num); + void setAnimationCounter(std::string name, int num); // Establece el rectangulo para un frame de una animación void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); // Establece la velocidad de una animación - void setAnimationSpeed(Uint8 index, Uint8 speed); + void setAnimationSpeed(std::string name, int speed); // Establece el numero de frames de una animación void setAnimationNumFrames(Uint8 index, Uint8 num); @@ -61,6 +59,9 @@ public: // Devuelve el rectangulo de una animación y frame concreto SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame); + + // Obtiene el indice de la animación a partir del nombre + int getIndex(std::string name); }; #endif \ No newline at end of file