Nox Engine
 
Loading...
Searching...
No Matches
Animator.h
Go to the documentation of this file.
1
6#include "Sprites.h"
7#include <memory>
8#include <unordered_map>
9
15struct Animator : public Component {
16public:
20 struct Animation {
21 const char* id;
22 int frames;
23 SDL_Texture* texture;
24 int speed = 100;
25 bool loop;
26 bool reversible = false;
27
28 Animation();
33 Animation(const char* id);
44 const char* id, SDL_Point frameSize, const char* texPath, int speed = 100,
45 bool isLooping = true, bool isReversible = false
46 );
47 ~Animation();
48 };
49
53 struct Edge {
54 std::vector<std::shared_ptr<bool>> conditions;
56
57 Edge();
62 Edge(std::vector<std::shared_ptr<bool>> conditions);
63 ~Edge();
64
69 const bool canTraverse() const;
70 };
71
72 Animator();
73 ~Animator();
74
78 void init() override;
82 void update() override;
86 void draw() override;
87
96 void addAnimation(
97 const char* id, const char* texPath, int speed = 100, const bool isLooping = true,
98 const bool isReversible = false
99 );
106 void
107 addEdge(const char* idFrom, const char* idTo, std::vector<std::shared_ptr<bool>> conditions);
112 std::string getCurrAnimID();
113
114private:
115 std::shared_ptr<Animation> currentAnimation;
116
117 Sprites* sprite;
118 SDL_Point frameSize;
119
120 std::unordered_map<const char*, std::shared_ptr<Animation>> animations;
121 std::unordered_map<const char*, std::unordered_map<const char*, Edge>> adjMatrix;
122};
Defines the component for rendering a static or animated sprite.
Represents a single, self-contained animation sequence.
Definition Animator.h:20
bool loop
Whether the animation should loop upon completion.
Definition Animator.h:25
bool reversible
If true, the animation plays forwards then backwards.
Definition Animator.h:26
int frames
The number of frames in the sprite sheet.
Definition Animator.h:22
SDL_Texture * texture
The texture containing the animation frames.
Definition Animator.h:23
int speed
The delay in milliseconds between frames.
Definition Animator.h:24
const char * id
Unique identifier for the animation.
Definition Animator.h:21
Defines a transition edge between two animations in the state machine.
Definition Animator.h:53
const bool canTraverse() const
Checks if all conditions for traversing this edge are met.
Definition Animator.cpp:49
std::vector< std::shared_ptr< bool > > conditions
Definition Animator.h:54
Manages sprite sheet animations and transitions between them using a state machine.
Definition Animator.h:15
std::string getCurrAnimID()
Gets the ID of the currently playing animation.
Definition Animator.cpp:111
void addEdge(const char *idFrom, const char *idTo, std::vector< std::shared_ptr< bool > > conditions)
Defines a conditional transition between two animations.
Definition Animator.cpp:102
void draw() override
Draws the current frame of the active animation.
Definition Animator.cpp:93
void init() override
Initializes the Animator component, acquiring the entity's Sprite component.
Definition Animator.cpp:62
void addAnimation(const char *id, const char *texPath, int speed=100, const bool isLooping=true, const bool isReversible=false)
Adds a new animation state to the animator's state machine.
Definition Animator.cpp:95
void update() override
Updates the current animation state, checking for transitions and advancing the frame.
Definition Animator.cpp:69
The base class for all components in the ECS.
Definition ECS.h:53
A component that gives an entity a visual representation using a texture.
Definition Sprites.h:18