Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AnimationGraph.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Animation System - Animation Graph (State Machine)
4
5Purpose:
6- Define state machine structure for animations
7- Handle state transitions based on parameters
8- Evaluate conditions for automatic state changes
9*/
10
11#pragma once
12
13#include "AnimationTypes.h"
14#include <string>
15#include <vector>
16#include <unordered_map>
17#include <memory>
18
19namespace OlympeAnimation
20{
21 // Forward declaration
22 class AnimationBank;
23
24 // ========================================================================
25 // Condition - Evaluates a parameter against a value
26 // ========================================================================
27 struct Condition
28 {
29 std::string parameter; // Parameter name to check
30 ComparisonOperator op; // Comparison operator
31 ParameterValue value; // Value to compare against
32
34 };
35
36 // ========================================================================
37 // Transition - Defines a transition between two states
38 // ========================================================================
40 {
41 std::string fromState; // Source state (or "ANY" for any state)
42 std::string toState; // Target state
43 float transitionTime = 0.1f; // Blend duration in seconds
44 std::vector<Condition> conditions; // Conditions that must be met
45
46 Transition() = default;
47 };
48
49 // ========================================================================
50 // AnimationState - Single state in the animation graph
51 // ========================================================================
53 {
54 std::string name; // State name (e.g., "Idle", "Walk")
55 std::string animationName; // Animation to play in this state
57 int priority = 0; // Higher priority = more important
58
59 AnimationState() = default;
60 };
61
62 // ========================================================================
63 // AnimationGraph - State machine for animation control
64 // ========================================================================
66 {
67 public:
68 AnimationGraph() = default;
69 ~AnimationGraph() = default;
70
71 // Load animation graph from JSON file
72 bool LoadFromFile(const std::string& filePath);
73
74 // Parameter management
75 void SetParameter(const std::string& name, bool value);
76 void SetParameter(const std::string& name, float value);
77 void SetParameter(const std::string& name, int value);
78 void SetParameter(const std::string& name, const std::string& value);
79
80 bool GetParameterBool(const std::string& name, bool defaultValue = false) const;
81 float GetParameterFloat(const std::string& name, float defaultValue = 0.0f) const;
82 int GetParameterInt(const std::string& name, int defaultValue = 0) const;
83 std::string GetParameterString(const std::string& name, const std::string& defaultValue = "") const;
84
85 // State management
86 void SetCurrentState(const std::string& stateName);
87 const std::string& GetCurrentState() const { return m_currentState; }
88
89 // Get animation name for current state
90 std::string GetCurrentAnimationName() const;
91
92 // Update state machine (checks for transitions)
93 // Returns true if state changed
94 bool Update(float deltaTime);
95
96 // Get graph name
97 const std::string& GetGraphName() const { return m_graphName; }
98
99 // Get animation bank path
100 const std::string& GetAnimationBankPath() const { return m_animationBankPath; }
101
102 // Check if graph is valid
103 bool IsValid() const { return m_isValid; }
104
105 private:
106 std::string m_graphName;
107 std::string m_description;
109 std::string m_currentState;
110 std::string m_defaultState = "Idle";
111 bool m_isValid = false;
112
113 std::unordered_map<std::string, ParameterValue> m_parameters;
114 std::unordered_map<std::string, AnimationState> m_states;
115 std::vector<Transition> m_transitions;
116
117 // Helper: Parse JSON
118 bool ParseJSON(const std::string& jsonContent);
119
120 // Helper: Evaluate a single condition
121 bool EvaluateCondition(const Condition& condition) const;
122
123 // Helper: Evaluate all conditions for a transition
124 bool EvaluateTransition(const Transition& transition) const;
125
126 // Helper: Find valid transition from current state
127 const Transition* FindValidTransition() const;
128 };
129
130} // namespace OlympeAnimation
Core animation data structures for 2D sprite animation system.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::unordered_map< std::string, ParameterValue > m_parameters
float GetParameterFloat(const std::string &name, float defaultValue=0.0f) const
const std::string & GetCurrentState() const
bool ParseJSON(const std::string &jsonContent)
bool LoadFromFile(const std::string &filePath)
std::string GetParameterString(const std::string &name, const std::string &defaultValue="") const
bool EvaluateCondition(const Condition &condition) const
int GetParameterInt(const std::string &name, int defaultValue=0) const
bool GetParameterBool(const std::string &name, bool defaultValue=false) const
std::vector< Transition > m_transitions
void SetParameter(const std::string &name, bool value)
void SetCurrentState(const std::string &stateName)
std::string GetCurrentAnimationName() const
bool EvaluateTransition(const Transition &transition) const
const Transition * FindValidTransition() const
std::unordered_map< std::string, AnimationState > m_states
const std::string & GetAnimationBankPath() const
const std::string & GetGraphName() const
std::vector< Condition > conditions