forked from jaildesigner-jailgames/jaildoctors_dilemma
netejant capçaleres
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
#include "utils/utils.hpp" // Para printWithDots
|
#include "utils/utils.hpp" // Para printWithDots
|
||||||
|
|
||||||
// Carga las animaciones en un vector(Animations) desde un fichero
|
// Carga las animaciones en un vector(Animations) desde un fichero
|
||||||
auto loadAnimationsFromFile(const std::string& file_path) -> Animations {
|
auto SurfaceAnimatedSprite::loadAnimationsFromFile(const std::string& file_path) -> Animations {
|
||||||
// Load file using ResourceHelper (supports both filesystem and pack)
|
// Load file using ResourceHelper (supports both filesystem and pack)
|
||||||
auto file_data = Resource::Helper::loadFile(file_path);
|
auto file_data = Resource::Helper::loadFile(file_path);
|
||||||
if (file_data.empty()) {
|
if (file_data.empty()) {
|
||||||
@@ -46,7 +46,7 @@ auto loadAnimationsFromFile(const std::string& file_path) -> Animations {
|
|||||||
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const std::string& file_path) {
|
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const std::string& file_path) {
|
||||||
// Carga las animaciones
|
// Carga las animaciones
|
||||||
if (!file_path.empty()) {
|
if (!file_path.empty()) {
|
||||||
Animations v = loadAnimationsFromFile(file_path);
|
Animations v = SurfaceAnimatedSprite::loadAnimationsFromFile(file_path);
|
||||||
setAnimations(v);
|
setAnimations(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ SurfaceAnimatedSprite::SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, c
|
|||||||
: SurfaceMovingSprite(std::move(surface)) {
|
: SurfaceMovingSprite(std::move(surface)) {
|
||||||
// Carga las animaciones
|
// Carga las animaciones
|
||||||
if (!file_path.empty()) {
|
if (!file_path.empty()) {
|
||||||
Animations v = loadAnimationsFromFile(file_path);
|
Animations v = SurfaceAnimatedSprite::loadAnimationsFromFile(file_path);
|
||||||
setAnimations(v);
|
setAnimations(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,7 +211,7 @@ auto parseGlobalParameter(const std::string& line, std::shared_ptr<Surface>& sur
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Parsea los frames de una animación desde una cadena separada por comas
|
// Helper: Parsea los frames de una animación desde una cadena separada por comas
|
||||||
void parseAnimationFrames(const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) {
|
void parseAnimationFrames(const std::string& value, SurfaceAnimatedSprite::AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) {
|
||||||
std::stringstream ss(value);
|
std::stringstream ss(value);
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
SDL_FRect rect = {0.0F, 0.0F, frame_width, frame_height};
|
SDL_FRect rect = {0.0F, 0.0F, frame_width, frame_height};
|
||||||
@@ -227,7 +227,7 @@ void parseAnimationFrames(const std::string& value, AnimationData& animation, fl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Parsea un parámetro de animación individual
|
// Helper: Parsea un parámetro de animación individual
|
||||||
auto parseAnimationParameter(const std::string& key, const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool {
|
auto parseAnimationParameter(const std::string& key, const std::string& value, SurfaceAnimatedSprite::AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool {
|
||||||
if (key == "name") {
|
if (key == "name") {
|
||||||
animation.name = value;
|
animation.name = value;
|
||||||
return true;
|
return true;
|
||||||
@@ -251,8 +251,8 @@ auto parseAnimationParameter(const std::string& key, const std::string& value, A
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper: Parsea una animación completa
|
// Helper: Parsea una animación completa
|
||||||
auto parseAnimation(const Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> AnimationData {
|
auto parseAnimation(const SurfaceAnimatedSprite::Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> SurfaceAnimatedSprite::AnimationData {
|
||||||
AnimationData animation;
|
SurfaceAnimatedSprite::AnimationData animation;
|
||||||
std::string line;
|
std::string line;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -295,7 +295,7 @@ void SurfaceAnimatedSprite::setAnimations(const Animations& animations) {
|
|||||||
|
|
||||||
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||||
if (line == "[animation]") {
|
if (line == "[animation]") {
|
||||||
AnimationData animation = parseAnimation(animations, index, frame_width, frame_height, frames_per_row, max_tiles);
|
SurfaceAnimatedSprite::AnimationData animation = parseAnimation(animations, index, frame_width, frame_height, frames_per_row, max_tiles);
|
||||||
animations_.emplace_back(animation);
|
animations_.emplace_back(animation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,37 +8,17 @@
|
|||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
|
|
||||||
#include "core/rendering/surface_moving_sprite.hpp" // Para SMovingSprite
|
#include "core/rendering/surface_moving_sprite.hpp" // Para SMovingSprite
|
||||||
class Surface; // lines 9-9
|
|
||||||
|
|
||||||
struct AnimationData {
|
class Surface;
|
||||||
std::string name; // Nombre de la animacion
|
|
||||||
std::vector<SDL_FRect> frames; // Cada uno de los frames que componen la animación
|
|
||||||
float speed{0.083F}; // Velocidad de la animación (segundos por frame)
|
|
||||||
int loop{0}; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
|
|
||||||
bool completed{false}; // Indica si ha finalizado la animación
|
|
||||||
int current_frame{0}; // Frame actual
|
|
||||||
float accumulated_time{0.0F}; // Tiempo acumulado para las animaciones (time-based)
|
|
||||||
};
|
|
||||||
|
|
||||||
using Animations = std::vector<std::string>;
|
|
||||||
|
|
||||||
// Carga las animaciones en un vector(Animations) desde un fichero
|
|
||||||
auto loadAnimationsFromFile(const std::string& file_path) -> Animations;
|
|
||||||
|
|
||||||
class SurfaceAnimatedSprite : public SurfaceMovingSprite {
|
class SurfaceAnimatedSprite : public SurfaceMovingSprite {
|
||||||
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 (time-based)
|
|
||||||
void animate(float delta_time);
|
|
||||||
|
|
||||||
// Carga la animación desde un vector de cadenas
|
|
||||||
void setAnimations(const Animations& animations);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
using Animations = std::vector<std::string>; // Tipo para lista de animaciones
|
||||||
|
|
||||||
|
// Métodos estáticos
|
||||||
|
static auto loadAnimationsFromFile(const std::string& file_path) -> Animations; // Carga las animaciones desde fichero
|
||||||
|
|
||||||
|
// Constructores
|
||||||
explicit SurfaceAnimatedSprite(const std::string& file_path);
|
explicit SurfaceAnimatedSprite(const std::string& file_path);
|
||||||
explicit SurfaceAnimatedSprite(const Animations& animations);
|
explicit SurfaceAnimatedSprite(const Animations& animations);
|
||||||
SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path);
|
SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path);
|
||||||
@@ -46,28 +26,44 @@ class SurfaceAnimatedSprite : public SurfaceMovingSprite {
|
|||||||
explicit SurfaceAnimatedSprite(std::shared_ptr<Surface> surface)
|
explicit SurfaceAnimatedSprite(std::shared_ptr<Surface> surface)
|
||||||
: SurfaceMovingSprite(std::move(surface)) {}
|
: SurfaceMovingSprite(std::move(surface)) {}
|
||||||
|
|
||||||
// Destructor
|
~SurfaceAnimatedSprite() override = default; // Destructor
|
||||||
~SurfaceAnimatedSprite() override = default;
|
|
||||||
|
|
||||||
// Actualiza las variables del objeto (time-based)
|
void update(float delta_time) override; // Actualiza las variables del objeto (time-based)
|
||||||
void update(float delta_time) override;
|
|
||||||
|
|
||||||
// Comprueba si ha terminado la animación
|
// Consultas de estado
|
||||||
auto animationIsCompleted() -> bool;
|
auto animationIsCompleted() -> bool; // Comprueba si ha terminado la animación
|
||||||
|
auto getIndex(const std::string& name) -> int; // Obtiene el índice de la animación por nombre
|
||||||
|
auto getCurrentAnimationSize() -> int { return static_cast<int>(animations_[current_animation_].frames.size()); } // Número de frames de la animación actual
|
||||||
|
|
||||||
// Obtiene el indice de la animación a partir del nombre
|
// Modificadores de animación
|
||||||
auto getIndex(const std::string& name) -> int;
|
void setCurrentAnimation(const std::string& name = "default"); // Establece la animación actual por nombre
|
||||||
|
void setCurrentAnimation(int index = 0); // Establece la animación actual por índice
|
||||||
|
void resetAnimation(); // Reinicia la animación
|
||||||
|
void setCurrentAnimationFrame(int num); // Establece el frame actual de la animación
|
||||||
|
|
||||||
// Establece la animacion actual
|
protected:
|
||||||
void setCurrentAnimation(const std::string& name = "default");
|
// Métodos protegidos
|
||||||
void setCurrentAnimation(int index = 0);
|
void animate(float delta_time); // Calcula el frame correspondiente a la animación actual (time-based)
|
||||||
|
void setAnimations(const Animations& animations); // Carga la animación desde un vector de cadenas
|
||||||
|
|
||||||
// Reinicia la animación
|
private:
|
||||||
void resetAnimation();
|
// Estructura interna de datos de animación
|
||||||
|
struct AnimationData {
|
||||||
|
std::string name{}; // Nombre de la animacion
|
||||||
|
std::vector<SDL_FRect> frames; // Cada uno de los frames que componen la animación
|
||||||
|
float speed{0.083F}; // Velocidad de la animación (segundos por frame)
|
||||||
|
int loop{0}; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva
|
||||||
|
bool completed{false}; // Indica si ha finalizado la animación
|
||||||
|
int current_frame{0}; // Frame actual
|
||||||
|
float accumulated_time{0.0F}; // Tiempo acumulado para las animaciones (time-based)
|
||||||
|
};
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Funciones amigas (helper functions del .cpp)
|
||||||
void setCurrentAnimationFrame(int num);
|
friend void parseAnimationFrames(const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles);
|
||||||
|
friend auto parseAnimationParameter(const std::string& key, const std::string& value, AnimationData& animation, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> bool;
|
||||||
|
friend auto parseAnimation(const Animations& animations, size_t& index, float frame_width, float frame_height, int frames_per_row, int max_tiles) -> AnimationData;
|
||||||
|
|
||||||
// Obtiene el numero de frames de la animación actual
|
// Variables miembro
|
||||||
auto getCurrentAnimationSize() -> int { return static_cast<int>(animations_[current_animation_].frames.size()); }
|
std::vector<AnimationData> animations_; // Vector con las diferentes animaciones
|
||||||
|
int current_animation_{0}; // Animación activa
|
||||||
};
|
};
|
||||||
@@ -149,7 +149,7 @@ auto Cache::getText(const std::string& name) -> std::shared_ptr<Text> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene la animación a partir de un nombre
|
// Obtiene la animación a partir de un nombre
|
||||||
auto Cache::getAnimations(const std::string& name) -> Animations& {
|
auto Cache::getAnimations(const std::string& name) -> SurfaceAnimatedSprite::Animations& {
|
||||||
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
|
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
|
||||||
|
|
||||||
if (it != animations_.end()) {
|
if (it != animations_.end()) {
|
||||||
@@ -300,7 +300,7 @@ void Cache::loadAnimations() {
|
|||||||
|
|
||||||
for (const auto& l : list) {
|
for (const auto& l : list) {
|
||||||
auto name = getFileName(l);
|
auto name = getFileName(l);
|
||||||
animations_.emplace_back(name, loadAnimationsFromFile(l));
|
animations_.emplace_back(name, SurfaceAnimatedSprite::loadAnimationsFromFile(l));
|
||||||
updateLoadingProgress();
|
updateLoadingProgress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,11 +80,11 @@ struct ResourceText {
|
|||||||
|
|
||||||
// Estructura para almacenar ficheros animaciones y su nombre
|
// Estructura para almacenar ficheros animaciones y su nombre
|
||||||
struct ResourceAnimation {
|
struct ResourceAnimation {
|
||||||
std::string name; // Nombre del fichero
|
std::string name; // Nombre del fichero
|
||||||
Animations animation; // Objeto con las animaciones
|
SurfaceAnimatedSprite::Animations animation; // Objeto con las animaciones
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
ResourceAnimation(std::string name, Animations animation)
|
ResourceAnimation(std::string name, SurfaceAnimatedSprite::Animations animation)
|
||||||
: name(std::move(name)),
|
: name(std::move(name)),
|
||||||
animation(std::move(animation)) {}
|
animation(std::move(animation)) {}
|
||||||
};
|
};
|
||||||
@@ -245,7 +245,7 @@ class Cache {
|
|||||||
auto getText(const std::string& name) -> std::shared_ptr<Text>;
|
auto getText(const std::string& name) -> std::shared_ptr<Text>;
|
||||||
|
|
||||||
// Obtiene la animación a partir de un nombre
|
// Obtiene la animación a partir de un nombre
|
||||||
auto getAnimations(const std::string& name) -> Animations&;
|
auto getAnimations(const std::string& name) -> SurfaceAnimatedSprite::Animations&;
|
||||||
|
|
||||||
// Obtiene el mapa de tiles a partir de un nombre
|
// Obtiene el mapa de tiles a partir de un nombre
|
||||||
auto getTileMap(const std::string& name) -> std::vector<int>&;
|
auto getTileMap(const std::string& name) -> std::vector<int>&;
|
||||||
|
|||||||
Reference in New Issue
Block a user