diff --git a/data/animations/player.ani b/data/animations/player.ani new file mode 100644 index 0000000..ac153c7 --- /dev/null +++ b/data/animations/player.ani @@ -0,0 +1,31 @@ +png_width_in_tiles=8 +tile_width=16 +tile_height=24 + +[animation] +name=stand +speed=10 +loop=yes +frames=0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,3,4,5,6,7,0,0 +[/animation] + +[animation] +name=walk +speed=10 +loop=yes +frames=8,9,10,10,9,8,11,12,13,13,14,15 +[/animation] + +[animation] +name=jump +speed=10 +loop=yes +frames=16 +[/animation] + +[animation] +name=death +speed=10 +loop=no +frames=24,25,26,27,28,29,30,31 +[/animation] \ No newline at end of file diff --git a/media/gfx/player.png b/media/gfx/player.png index 29a7476..59e22be 100644 Binary files a/media/gfx/player.png and b/media/gfx/player.png differ diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 0ce978f..9c0f427 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -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; } \ No newline at end of file diff --git a/source/animatedsprite.h b/source/animatedsprite.h index 6e13097..72850ca 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -3,6 +3,7 @@ #include "movingsprite.h" #include #include +#include #ifndef ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H @@ -39,29 +40,29 @@ public: // Establece el valor del contador 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(std::string name, int speed); - // Establece el numero de frames de una animación - void setAnimationNumFrames(Uint8 index, Uint8 num); - // Establece si la animación se reproduce en bucle - void setAnimationLoop(Uint8 index, bool loop); + void setAnimationLoop(std::string name, bool loop); // Establece el valor de la variable - void setCompleted(Uint8 index, bool value); + void setCompleted(std::string name, bool value); // Comprueba si ha terminado la animación - bool isCompleted(Uint8 index); + bool isCompleted(std::string name); // Devuelve el rectangulo de una animación y frame concreto - SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame); + SDL_Rect getAnimationClip(std::string name, Uint8 index); // Obtiene el indice de la animación a partir del nombre int getIndex(std::string name); + + // Carga la animación desde un fichero + bool load(std::string filePath); + + // Asigna variables a partir de dos cadenas + bool setVars(int index, std::string var, std::string value); }; #endif \ No newline at end of file