SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
AnimationStateMachine.hpp
Go to the documentation of this file.
1#ifndef _ANIMATIONSTATEMACHINE_HPP_
2#define _ANIMATIONSTATEMACHINE_HPP_
3
4#include <Core/OSDef.hpp>
5#include <string>
6#include <vector>
7#include <unordered_map>
8#include <variant>
9
10namespace Sleak {
11
12 class AnimationClip;
13
14 /// What the state machine returns each frame for the animator to sample.
15 /// @ingroup animation
17 AnimationClip* clipA = nullptr;
18 float timeA = 0.0f;
19 AnimationClip* clipB = nullptr; // null if not blending
20 float timeB = 0.0f;
21 float blendWeight = 0.0f; // 0 = pure A, 1 = pure B
22 };
23
24 /// One named clip binding within the graph, with its own loop/speed settings.
25 /// @ingroup animation
27 std::string name;
28 AnimationClip* clip = nullptr;
29 bool loop = true;
30 float speed = 1.0f;
31 };
32
33 /// Comparison used to evaluate a TransitionCondition against a live parameter.
34 /// @ingroup animation
43
44 using ParamValue = std::variant<bool, float, int>;
45
46 /// One guard on a transition: param op threshold must hold for the transition to fire.
47 /// @ingroup animation
53
54 /// An edge in the state graph, with its blend timing and guard conditions.
55 /// @ingroup animation
57 int fromState = -1;
58 int toState = -1;
59 float blendDuration = 0.3f;
60 bool waitForClipEnd = false;
61 std::vector<TransitionCondition> conditions;
62 };
63
64 /// Graph of animation states and blended transitions, driven by named
65 /// bool/float/int parameters set from gameplay code.
66 /// @ingroup animation
67 class ENGINE_API AnimationStateMachine {
68 public:
71
72 // Build the state graph
73 /// Adds a state bound to clip, returning its index for use in AddTransition.
74 int AddState(const std::string& name, AnimationClip* clip,
75 bool loop = true, float speed = 1.0f);
76 /// Adds a transition edge from -> to, returning its index.
77 int AddTransition(int from, int to, float blendDuration = 0.3f,
78 bool waitForClipEnd = false);
79 /// Appends a guard condition to the transition at transIndex.
80 void AddTransitionCondition(int transIndex, const std::string& paramName,
81 CompareOp op, ParamValue threshold);
82 /// Sets the state the machine starts in, with no blend.
83 void SetDefaultState(int stateIndex);
84
85 // Parameters (set by gameplay code)
86 void SetBool(const std::string& name, bool value);
87 void SetFloat(const std::string& name, float value);
88 void SetInt(const std::string& name, int value);
89
90 // Called each frame by AnimatorComponent
91 /// Advances playback/blend time, evaluates transitions, and returns what to sample this frame.
92 SampleRequest Update(float deltaTime);
93
94 // Query
95 const std::string& GetCurrentStateName() const;
96 bool IsBlending() const { return m_blending; }
97
98 private:
99 /// True if every condition on trans currently holds (AND logic; vacuously true if empty).
100 bool EvaluateConditions(const AnimationTransition& trans) const;
101 /// Compares param against threshold using op, coercing both to float.
102 bool CompareParam(const ParamValue& param, CompareOp op,
103 const ParamValue& threshold) const;
104 /// Begins blending from the current state into the transition's target state.
105 void StartTransition(int transIndex);
106
107 std::vector<AnimationState> m_states;
108 std::vector<AnimationTransition> m_transitions;
109 std::unordered_map<std::string, ParamValue> m_params;
110
111 int m_currentState = -1;
112 float m_currentTime = 0.0f; // in ticks
113
114 // Blend state
115 bool m_blending = false;
116 int m_prevState = -1;
117 float m_prevTime = 0.0f;
118 float m_blendElapsed = 0.0f;
119 float m_blendDuration = 0.0f;
120 };
121
122} // namespace Sleak
123
124#endif // _ANIMATIONSTATEMACHINE_HPP_
void SetDefaultState(int stateIndex)
Sets the state the machine starts in, with no blend.
int AddTransition(int from, int to, float blendDuration=0.3f, bool waitForClipEnd=false)
Adds a transition edge from -> to, returning its index.
SampleRequest Update(float deltaTime)
Advances playback/blend time, evaluates transitions, and returns what to sample this frame.
void SetFloat(const std::string &name, float value)
void SetBool(const std::string &name, bool value)
void AddTransitionCondition(int transIndex, const std::string &paramName, CompareOp op, ParamValue threshold)
Appends a guard condition to the transition at transIndex.
int AddState(const std::string &name, AnimationClip *clip, bool loop=true, float speed=1.0f)
Adds a state bound to clip, returning its index for use in AddTransition.
void SetInt(const std::string &name, int value)
const std::string & GetCurrentStateName() const
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
std::variant< bool, float, int > ParamValue
std::vector< TransitionCondition > conditions