Skip to content

File Animation.hpp

File List > Components > Animation.hpp

Go to the documentation of this file

#ifndef ENGINE_COMPONENTS_ANIMATION_HPP_
#define ENGINE_COMPONENTS_ANIMATION_HPP_

#include "Assets/Sprites.hpp"
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

namespace Engine {

struct FrameData {
    raylib::Rectangle rect; 
    float pivotX = 0.0f;    
    float pivotY = 0.0f;    

    FrameData() = default;

    FrameData(const raylib::Rectangle& r, float px = 0.0f, float py = 0.0f);

    FrameData(float x, float y, float w, float h, float px = 0.0f, float py = 0.0f);
};

struct AnimationClip {
    std::string name = "Idle";           
    std::vector<std::string> assetPaths; 
    float frameWidth = 0.0f;     
    float frameHeight = 0.0f;    
    std::size_t totalFrames = 0; 
    std::vector<FrameData> frameData; 
    float frameDuration = 0.1f;       
    bool loop = true;                 

    AnimationClip() = default;

    AnimationClip(std::string clipName, std::vector<std::string> paths, float duration = 0.1f,
                  bool isLooping = true);

    AnimationClip(std::string clipName, const std::string& sheetPath, float width, float height,
                  std::size_t total = 0, float duration = 0.1f, bool isLooping = true);

    AnimationClip(std::string clipName, const std::string& sheetPath, std::vector<FrameData> frames,
                  float duration = 0.1f, bool isLooping = true);
};

struct Animation {
    std::unordered_map<std::string, AnimationClip>
        clips;                         
    std::string currentAnimation = ""; 
    std::vector<std::shared_ptr<Sprite>>
        sprites; 

    float timer = 0.0f;           
    std::size_t currentFrame = 0; 
    bool isPlaying = true;        

    float offsetX = 0.0f; 
    float offsetY = 0.0f; 
    float scaleX = 1.0f;  
    float scaleY = 1.0f;  
    bool flipX = false;   
    bool flipY = false;   

    Animation() = default;

    const AnimationClip* GetCurrentClip() const;

    AnimationClip* GetCurrentClip();

    bool Play(const std::string& clipName, bool restartIfSame = false);

    Animation(std::vector<std::string> paths, float frameDuration = 0.1f, bool loop = true,
              float offsetX = 0.0f, float offsetY = 0.0f, float scaleX = 1.0f, float scaleY = 1.0f,
              bool flipX = false, bool flipY = false, const std::string& clipName = "Idle");

    Animation(const std::string& sheetPath, float frameWidth, float frameHeight,
              std::size_t totalFrames = 0, float frameDuration = 0.1f, bool loop = true,
              float offsetX = 0.0f, float offsetY = 0.0f, float scaleX = 1.0f, float scaleY = 1.0f,
              bool flipX = false, bool flipY = false, const std::string& clipName = "Idle");

    Animation(const std::string& sheetPath, std::vector<FrameData> frames,
              float frameDuration = 0.1f, bool loop = true, float offsetX = 0.0f,
              float offsetY = 0.0f, float scaleX = 1.0f, float scaleY = 1.0f, bool flipX = false,
              bool flipY = false, const std::string& clipName = "Idle");
};

} // namespace Engine

#endif // ENGINE_COMPONENTS_ANIMATION_HPP_