ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Animator.h
浏览该文件的文档.
1#pragma once
2
4#include "../Core/Core.h"
6#include "AnimationClip.h"
7
8#include <nlohmann/json.hpp>
9
10#include <memory>
11#include <string>
12#include <vector>
13
14namespace Shit
15{
16 class Animation;
17
19 enum class AnimatorParamType : int {
20 Float = 0,
21 Bool = 1,
22 Trigger = 2,
23 };
24
32
34 enum class AnimatorConditionType : int {
35 FloatGt = 0,
36 FloatLt = 1,
37 FloatEq = 2,
38 Bool = 3,
39 Trigger = 4,
40 };
41
49
52 std::string name;
54 std::string assetPath;
55 bool isEntry = false;
56 float graphX = 0.0f;
57 float graphY = 0.0f;
58 };
59
62 int fromState = -1;
63 int toState = -1;
64 std::vector<AnimatorTransitionCondition> conditions;
65 float exitTime = 0.0f;
66 };
67
81 class SHIT_API SHIT_REFLECT(BlackList) Animator : public Behavior {
83 public:
85 ~Animator() override;
86
87 // ── 生命周期 ──
88 void onAttach() override;
89 void onStart() override;
90 void onUpdate() override;
91 void onDestroy() override;
92 void onAfterDeserialize() override;
93 void onFieldChanged(const std::string& fieldName) override;
94
95 // ── 参数驱动 ──
96 void setFloat(const std::string& name, float value);
97 void setBool(const std::string& name, bool value);
98 void setTrigger(const std::string& name);
99
100 // ── 状态查询 ──
101 int stateCount() const { return static_cast<int>(m_states.size()); }
102 const AnimatorState* stateAt(int index) const;
103 int currentStateIndex() const { return m_currentState; }
104 const std::string& currentStateName() const;
105 void setCurrentState(int index);
108
109 // ── 参数管理 ──
110 int paramCount() const { return static_cast<int>(m_params.size()); }
111 const AnimatorParameter* paramAt(int index) const;
112 int addParam(const std::string& name, AnimatorParamType type);
113 bool setParam(int index, const AnimatorParameter& param);
114 bool removeParam(int index);
115 int findParam(const std::string& name) const;
116
117 // ── 状态管理 ──
118 int addState(const std::string& name);
119 bool setState(int index, const AnimatorState& state);
120 bool removeState(int index);
121
122 // ── 转换管理 ──
123 int transitionCount() const { return static_cast<int>(m_transitions.size()); }
124 const AnimatorTransition* transitionAt(int index) const;
125 int addTransition(int fromState, int toState);
126 bool setTransition(int index, const AnimatorTransition& transition);
127 bool removeTransition(int index);
128
129 // ── 序列化载体 ──
131 void syncData();
132
133 // ── 运行时查询 ──
134 bool isPlaying() const { return m_isPlaying; }
136 uint64_t getDataGeneration() const { return m_dataGeneration; }
137
138 private:
140 void enterState(int index);
142 void parseData();
144 void notifyDataChanged();
146 void applyCurrentFrame();
148 bool checkTransition(const AnimatorTransition& t) const;
149
150 // 反射字符串载体(JSON:states / params / transitions)
151 // 注意:不能标 readOnly——readOnly 字段会被 Prefab 序列化跳过,导致状态机数据丢失
152 SHIT_META(({.displayName = "Animator Data", .tooltip = "状态机序列化载体(JSON),由编辑器维护"}))
153 std::string m_animatorData;
154
155 // 运行时(Disable 不入反射)
156 SHIT_META(Disable)
157 std::vector<AnimatorState> m_states;
158 SHIT_META(Disable)
159 std::vector<AnimatorParameter> m_params;
160 SHIT_META(Disable)
161 std::vector<AnimatorTransition> m_transitions;
162
163 SHIT_META(Disable)
164 int m_currentState = -1;
165 SHIT_META(Disable)
166 std::unique_ptr<Animation> m_currentAnimation;
167 SHIT_META(Disable)
168 float m_animTime = 0.0f;
169 SHIT_META(Disable)
170 bool m_isPlaying = false;
171
172 SHIT_META(Disable)
173 uint64_t m_dataGeneration = 0;
174
175 // 只读展示字段
176 SHIT_META(({.displayName = "Current State", .readOnly = true}))
177 std::string m_currentStateDisplay;
178 SHIT_META(({.displayName = "Playing", .readOnly = true}))
179 bool m_playingDisplay = false;
180 };
181}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:89
#define SHIT_REFLECT(Mode)
定义 Macros.h:86
#define SHIT_META(...)
定义 Macros.h:95
逐帧动画数据
定义 Animation.h:17
void onStart() override
首次 update 前执行一次
const AnimatorState * stateAt(int index) const
uint64_t getDataGeneration() const
数据代数,每次状态/参数/转换变更后递增(编辑器用于判断是否需要重建状态机图)
定义 Animator.h:136
bool isPlaying() const
定义 Animator.h:134
const AnimatorParameter * paramAt(int index) const
int currentStateIndex() const
定义 Animator.h:103
bool removeState(int index)
连带清理引用该状态的转换
int stateCount() const
定义 Animator.h:101
int addState(const std::string &name)
~Animator() override
bool setTransition(int index, const AnimatorTransition &transition)
bool evaluateTransitions()
立即求值一次转换(编辑器/运行共用;onUpdate 自动调用)
const std::string & currentStateName() const
int paramCount() const
定义 Animator.h:110
void setCurrentState(int index)
强制切到指定状态(编辑器预览/调试用)
void onAfterDeserialize() override
反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
void setTrigger(const std::string &name)
void syncData()
把当前 states/params/transitions 同步到 m_animatorData(编辑器修改后调用)
int addParam(const std::string &name, AnimatorParamType type)
void setFloat(const std::string &name, float value)
bool removeParam(int index)
void setBool(const std::string &name, bool value)
void onFieldChanged(const std::string &fieldName) override
检查器直接写反射字段后回调(字段名)。用于绕过 setter 的直写完成后 重新同步引擎状态(如 Box2D 刚体/形状)。默认无操作。
int transitionCount() const
定义 Animator.h:123
void onAttach() override
bool setState(int index, const AnimatorState &state)
const AnimatorTransition * transitionAt(int index) const
void onUpdate() override
每帧执行
bool setParam(int index, const AnimatorParameter &param)
void onDestroy() override
int findParam(const std::string &name) const
bool removeTransition(int index)
int addTransition(int fromState, int toState)
Behavior()=default
定义 AnimationClip.h:12
AnimatorParamType
动画参数类型(状态机条件变量);以 int 存 JSON 载体,无需反射
定义 Animator.h:19
@ Float
浮点(比较 threshold)
定义 Animator.h:20
@ Bool
布尔(比较 boolValue)
定义 Animator.h:21
@ Trigger
触发器(一次性触发,转换求值后消耗)
定义 Animator.h:22
AnimatorConditionType
条件类型(转换求值);以 int 存 JSON 载体,无需反射
定义 Animator.h:34
@ FloatLt
float < threshold
定义 Animator.h:36
@ FloatEq
|float - threshold| < 0.001
定义 Animator.h:37
@ FloatGt
float > threshold
定义 Animator.h:35
动画剪辑(可序列化数据,P28)
定义 AnimationClip.h:31
动画参数(可序列化)
定义 Animator.h:26
std::string name
定义 Animator.h:27
AnimatorParamType type
定义 Animator.h:28
bool boolValue
Bool 当前值 / Trigger 触发标志
定义 Animator.h:30
float floatValue
Float 当前值
定义 Animator.h:29
状态机节点(可序列化)
定义 Animator.h:51
float graphY
状态机图节点 Y(编辑器布局用,随 .scene 保存)
定义 Animator.h:57
float graphX
状态机图节点 X(编辑器布局用,随 .scene 保存)
定义 Animator.h:56
bool isEntry
是否入口状态(onStart 优先进入)
定义 Animator.h:55
std::string assetPath
引用的 .anim 资产路径(序列化字段;空 = 旧场景内嵌 clip,向后兼容)
定义 Animator.h:54
std::string name
状态名
定义 Animator.h:52
AnimationClip clip
运行时剪辑缓存(从 .anim 文件加载,不入序列化)
定义 Animator.h:53
转换的单个条件(引用参数名)
定义 Animator.h:43
float threshold
Float 阈值
定义 Animator.h:46
AnimatorConditionType type
定义 Animator.h:45
std::string parameter
参数名
定义 Animator.h:44
bool boolValue
Bool 期望值
定义 Animator.h:47
状态间转换(可序列化;同一状态可有多条,按序求值)
定义 Animator.h:61
int fromState
源状态索引(-1 = 任意)
定义 Animator.h:62
int toState
目标状态索引
定义 Animator.h:63
std::vector< AnimatorTransitionCondition > conditions
全部满足才切换
定义 Animator.h:64
float exitTime
保留:切换延迟(秒),当前实现立即切换
定义 Animator.h:65