Trabajando en la clase animatedsprite
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user