95 lines
3.4 KiB
C++
95 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <SDL2/SDL_rect.h> // for SDL_Rect
|
|
#include <SDL2/SDL_stdinc.h> // for Uint8
|
|
#include <memory> // for shared_ptr
|
|
#include <string> // for string
|
|
#include <vector> // for vector
|
|
#include "moving_sprite.h" // for MovingSprite
|
|
class Texture;
|
|
|
|
struct Animation
|
|
{
|
|
std::string name; // Nombre de la animacion
|
|
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
|
|
int speed; // Velocidad de la animación
|
|
int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
|
|
bool completed; // Indica si ha finalizado la animación
|
|
int current_frame; // Frame actual
|
|
int counter; // Contador para las animaciones
|
|
|
|
Animation() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
|
|
};
|
|
|
|
class AnimatedSprite : public MovingSprite
|
|
{
|
|
protected:
|
|
// Variables
|
|
std::vector<Animation> animations_; // Vector con las diferentes animaciones
|
|
int current_animation_; // Animacion activa
|
|
|
|
// Calcula el frame correspondiente a la animación actual
|
|
void animate();
|
|
|
|
// Carga la animación desde un fichero
|
|
std::vector<Animation> loadFromFile(std::string filePath);
|
|
|
|
// Carga la animación desde un vector
|
|
bool loadFromVector(std::vector<std::string> *source);
|
|
|
|
public:
|
|
// Constructor
|
|
AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path);
|
|
AnimatedSprite(std::shared_ptr<Texture> texture, std::vector<std::string> *animations);
|
|
explicit AnimatedSprite(std::shared_ptr<Texture> texture);
|
|
|
|
// Destructor
|
|
virtual ~AnimatedSprite();
|
|
|
|
// Actualiza las variables del objeto
|
|
void update() override;
|
|
|
|
// Obtiene el número de frames de la animación actual
|
|
int getNumFrames();
|
|
|
|
// Establece el frame actual de la animación
|
|
void setCurrentFrame(int num);
|
|
|
|
// Establece el valor del contador
|
|
void setAnimationCounter(const std::string &name, int num);
|
|
|
|
// Establece la velocidad de una animación
|
|
void setAnimationSpeed(const std::string &name, int speed);
|
|
void setAnimationSpeed(int index, int speed);
|
|
|
|
// Establece el frame al que vuelve la animación al finalizar
|
|
void setAnimationLoop(const std::string &name, int loop);
|
|
void setAnimationLoop(int index, int loop);
|
|
|
|
// Establece el valor de la variable
|
|
void setAnimationCompleted(const std::string &name, bool value);
|
|
void setAnimationCompleted(int index, bool value);
|
|
|
|
// Comprueba si ha terminado la animación
|
|
bool animationIsCompleted();
|
|
|
|
// Devuelve el rectangulo de una animación y frame concreto
|
|
SDL_Rect getAnimationClip(const std::string &name = "default", Uint8 index = 0);
|
|
SDL_Rect getAnimationClip(int indexA = 0, Uint8 indexF = 0);
|
|
|
|
// Obtiene el indice de la animación a partir del nombre
|
|
int getIndex(const std::string &name);
|
|
|
|
// Establece la animacion actual
|
|
void setCurrentAnimation(const std::string &name = "default");
|
|
void setCurrentAnimation(int index = 0);
|
|
|
|
// OLD - 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);
|
|
|
|
// OLD - Establece el contador para todas las animaciones
|
|
void setAnimationCounter(int value);
|
|
|
|
// Reinicia la animación
|
|
void resetAnimation();
|
|
}; |