Arreglades les herencies de Sprite

Abans de llevar mil coses que sobren i replantejar-se estes 4 classes
This commit is contained in:
2024-10-13 10:01:07 +02:00
parent 33ea8d90ca
commit b060f21696
17 changed files with 1204 additions and 284 deletions

View File

@@ -15,7 +15,9 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
auto frame_height = 0;
auto max_tiles = 0;
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
std::ifstream file(file_path);
std::string line;
@@ -156,21 +158,20 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string
}
// Constructor
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::string file, std::vector<std::string> *buffer)
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file, std::vector<std::string> *buffer)
: MovingSprite(texture)
{
// Copia los punteros
setTexture(texture);
// Carga las animaciones
if (file != "")
{
AnimatedFile as = loadAnimationFromFile(texture, file);
// Copia los datos de las animaciones
for (auto animation : as.animations)
/*for (auto animation : as.animations)
{
animations_.push_back(animation);
}
}*/
std::copy(as.animations.begin(), as.animations.end(), std::back_inserter(animations_));
}
else if (buffer)
@@ -183,19 +184,14 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::string fil
}
// Constructor
AnimatedSprite::AnimatedSprite(AnimatedFile *animation)
AnimatedSprite::AnimatedSprite(const AnimatedFile *animation)
: MovingSprite(animation->texture)
{
// Copia los punteros
setTexture(animation->texture);
// Inicializa variables
current_animation_ = 0;
// Copia los datos de las animaciones
for (auto a : animation->animations)
{
animations_.push_back(a);
}
std::copy(animation->animations.begin(), animation->animations.end(), std::back_inserter(animations_));
}
// Destructor
@@ -205,11 +201,11 @@ AnimatedSprite::~AnimatedSprite()
}
// Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(std::string name)
int AnimatedSprite::getIndex(const std::string &name)
{
auto index = -1;
for (auto a : animations_)
for (const auto &a : animations_)
{
index++;
if (a.name == name)
@@ -226,7 +222,7 @@ int AnimatedSprite::getIndex(std::string name)
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate()
{
if (!enabled_ || animations_[current_animation_].speed == 0)
if (animations_[current_animation_].speed == 0)
{
return;
}
@@ -284,13 +280,13 @@ void AnimatedSprite::setCurrentFrame(int num)
}
// Establece el valor del contador
void AnimatedSprite::setAnimationCounter(std::string name, int num)
void AnimatedSprite::setAnimationCounter(const std::string &name, int num)
{
animations_[getIndex(name)].counter = num;
}
// Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
void AnimatedSprite::setAnimationSpeed(const std::string &name, int speed)
{
animations_[getIndex(name)].counter = speed;
}
@@ -302,7 +298,7 @@ void AnimatedSprite::setAnimationSpeed(int index, int speed)
}
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(std::string name, int loop)
void AnimatedSprite::setAnimationLoop(const std::string &name, int loop)
{
animations_[getIndex(name)].loop = loop;
}
@@ -314,7 +310,7 @@ void AnimatedSprite::setAnimationLoop(int index, int loop)
}
// Establece el valor de la variable
void AnimatedSprite::setAnimationCompleted(std::string name, bool value)
void AnimatedSprite::setAnimationCompleted(const std::string &name, bool value)
{
animations_[getIndex(name)].completed = value;
}
@@ -332,7 +328,7 @@ bool AnimatedSprite::animationIsCompleted()
}
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
SDL_Rect AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index)
{
return animations_[getIndex(name)].frames[index];
}
@@ -484,13 +480,13 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
}
// Pone un valor por defecto
setRect({0, 0, frame_width, frame_height});
setPos((SDL_Rect){0, 0, frame_width, frame_height});
return success;
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(std::string name)
void AnimatedSprite::setCurrentAnimation(const std::string &name)
{
const auto new_animation = getIndex(name);
if (current_animation_ != new_animation)
@@ -518,11 +514,8 @@ void AnimatedSprite::setCurrentAnimation(int index)
// Actualiza las variables del objeto
void AnimatedSprite::update()
{
if (enabled_)
{
animate();
MovingSprite::update();
}
animate();
MovingSprite::update();
}
// Establece el rectangulo para un frame de una animación