72 lines
2.6 KiB
C++
72 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
|
#include <memory> // Para shared_ptr
|
|
#include <string> // Para string
|
|
#include <vector> // Para vector
|
|
#include "s_moving_sprite.h" // Para SMovingSprite
|
|
class Surface; // lines 9-9
|
|
|
|
struct AnimationData
|
|
{
|
|
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
|
|
|
|
AnimationData() : name(std::string()), speed(5), loop(0), completed(false), current_frame(0), counter(0) {}
|
|
};
|
|
|
|
using Animations = std::vector<std::string>;
|
|
|
|
// Carga las animaciones en un vector(Animations) desde un fichero
|
|
Animations loadAnimationsFromFile(const std::string &file_path);
|
|
|
|
class SAnimatedSprite : public SMovingSprite
|
|
{
|
|
protected:
|
|
// Variables
|
|
std::vector<AnimationData> animations_; // Vector con las diferentes animaciones
|
|
int current_animation_ = 0; // Animacion activa
|
|
|
|
// Calcula el frame correspondiente a la animación actual
|
|
void animate();
|
|
|
|
// Carga la animación desde un vector de cadenas
|
|
void setAnimations(const Animations &animations);
|
|
|
|
public:
|
|
// Constructor
|
|
SAnimatedSprite(std::shared_ptr<Surface> surface, const std::string &file_path);
|
|
SAnimatedSprite(std::shared_ptr<Surface> surface, const Animations &animations);
|
|
explicit SAnimatedSprite(std::shared_ptr<Surface> surface)
|
|
: SMovingSprite(surface) {}
|
|
|
|
// Destructor
|
|
virtual ~SAnimatedSprite() override = default;
|
|
|
|
// Actualiza las variables del objeto
|
|
void update() override;
|
|
|
|
// Comprueba si ha terminado la animación
|
|
bool animationIsCompleted();
|
|
|
|
// 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);
|
|
|
|
// Reinicia la animación
|
|
void resetAnimation();
|
|
|
|
// Establece el frame actual de la animación
|
|
void setCurrentAnimationFrame(int num);
|
|
|
|
// Obtiene el numero de frames de la animación actual
|
|
int getCurrentAnimationSize() { return static_cast<int>(animations_[current_animation_].frames.size()); }
|
|
}; |