Trabajando en la carga de las animaciones
This commit is contained in:
@@ -36,7 +36,7 @@ void AnimatedSprite::animate(std::string name)
|
||||
const int index = getIndex(name);
|
||||
|
||||
// Calcula el frame actual a partir del contador
|
||||
animation[index].currentFrame = animation[index].counter/animation[index].speed;
|
||||
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
|
||||
@@ -50,10 +50,10 @@ void AnimatedSprite::animate(std::string name)
|
||||
// En caso contrario
|
||||
else
|
||||
{
|
||||
// Escogemos el frame correspondiente de la animación
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[index].frames[animation[index].currentFrame]);
|
||||
|
||||
// Incrementamos el contador de la animacion
|
||||
// Incrementa el contador de la animacion
|
||||
animation[index].counter++;
|
||||
}
|
||||
}
|
||||
@@ -71,48 +71,90 @@ 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;
|
||||
animation[getIndex(name)].counter = speed;
|
||||
}
|
||||
|
||||
// Establece si la animación se reproduce en bucle
|
||||
void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop)
|
||||
void AnimatedSprite::setAnimationLoop(std::string name, bool loop)
|
||||
{
|
||||
mAnimation[index].loop = loop;
|
||||
animation[getIndex(name)].loop = loop;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void AnimatedSprite::setCompleted(Uint8 index, bool value)
|
||||
void AnimatedSprite::setCompleted(std::string name, bool value)
|
||||
{
|
||||
mAnimation[index].completed = value;
|
||||
animation[getIndex(name)].completed = value;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
bool AnimatedSprite::isCompleted(Uint8 index)
|
||||
bool AnimatedSprite::isCompleted(std::string name)
|
||||
{
|
||||
return mAnimation[index].completed;
|
||||
return animation[getIndex(name)].completed;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame)
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
|
||||
{
|
||||
return mAnimation[index_animation].frames[index_frame];
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user