![]() |
ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
|
动画组件 / Animator(可序列化多剪辑版本) 更多...
#include <AnimationComponent.h>
Public 成员函数 | |
| AnimationComponent () | |
| ~AnimationComponent () override | |
| void | onAttach () override |
| void | onStart () override |
| 首次 update 前执行一次 | |
| void | onUpdate () override |
| 每帧执行 | |
| void | onDestroy () override |
| void | onAfterDeserialize () override |
| 反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。 | |
| void | onFieldChanged (const std::string &fieldName) override |
| 检查器直接写反射字段后回调(字段名)。用于绕过 setter 的直写完成后 重新同步引擎状态(如 Box2D 刚体/形状)。默认无操作。 | |
| void | addAnimation (const std::string &name, std::unique_ptr< Animation > animation) |
| void | play (const std::string &name) |
| void | stop () |
| void | pause () |
| void | resume () |
| void | play (const std::string &name, const SpriteSheet &sheet, const std::vector< int > &frames, float duration=0.1f, bool loop=true) |
| 用数字帧索引直接设置并播放一段动画(运行时动态,不落盘) | |
| int | clipCount () const |
| 剪辑数量 | |
| const AnimationClip * | clipAt (int index) const |
| 取第 i 个剪辑(越界返回 nullptr) | |
| const std::vector< AnimationClip > & | clips () const |
| 全部剪辑(只读) | |
| int | addClip (const std::string &name) |
| 新增一个剪辑(重名自动追加 " (N)"),返回其索引;-1 失败 | |
| bool | setClip (int index, const AnimationClip &clip) |
| 覆盖第 index 个剪辑为 clip(并重建运行时动画);返回是否成功 | |
| bool | removeClip (int index) |
| 移除第 index 个剪辑(若正播放则停止);返回是否成功 | |
| bool | setDefaultClip (int index) |
| 设为/取消默认剪辑(isDefault 唯一化);返回是否成功 | |
| int | defaultClipIndex () const |
| 取默认剪辑索引;无则 -1 | |
| void | syncClipsData () |
| 把当前 m_clips 同步到 m_clipsData(JSON 载体)——编辑器/脚本修改后调用 | |
| bool | isPlaying () const |
| bool | isPaused () const |
| const std::string & | getCurrentAnimationName () const |
| Public 成员函数 继承自 Behavior | |
| Behavior ()=default | |
| ~Behavior () override=default | |
| void | onCreate () override |
| void | onAttach () override |
| virtual void | onFixedUpdate (float fixedDt) |
| 固定步长执行(与物理同拍;Scene 固定步循环驱动) | |
| virtual void | onCollisionEnter (GameObject *other) |
| 与 other 开始接触(每对象对一次) | |
| virtual void | onCollisionStay (GameObject *other) |
| 与 other 持续接触(每帧一次) | |
| virtual void | onCollisionExit (GameObject *other) |
| 与 other 结束接触 | |
| void | onDetach () override |
| void | onDestroy () override |
| bool | isStarted () const |
| onStart 是否已执行过 | |
| void | setStarted (bool isStarted) |
| Public 成员函数 继承自 Component | |
| Component () | |
| virtual | ~Component ()=default |
| Component (const Component &)=delete | |
| Component & | operator= (const Component &)=delete |
| Component (Component &&)=delete | |
| Component & | operator= (Component &&)=delete |
| GameObject * | getOwner () const |
| bool | isRegistered () const |
| uint64_t | getUuid () const |
| 组件持久 ID(随 .scene 序列化,是 ComponentRef<T> 引用字段的寻址依据) | |
| void | setUuid (uint64_t uuid) |
| 直接设置组件 ID(仅引擎反序列化/索引维护使用;正常流程由构造函数随机分配) | |
额外继承的成员函数 | |
| Protected 成员函数 继承自 Component | |
| void | setRegistered (bool registered) |
| Protected 属性 继承自 Behavior | |
| bool | m_isStarted = false |
| Protected 属性 继承自 Component | |
| GameObject * | m_owner = nullptr |
| bool | m_isRegistered = false |
| uint64_t | m_uuid = 0 |
| 持久 ID(构造时随机分配;0 仅在序列化前尚未赋值时短暂存在) | |
动画组件 / Animator(可序列化多剪辑版本)
继承 Behavior,由 BehaviorSystem 每帧调用 onUpdate 推进当前动画时间, 并把当前帧的源矩形 (SDL_FRect) 回写到同 GameObject 上的 SpriteRenderer。
P28:可序列化的动画剪辑管理。动画剪辑(AnimationClip,独立于 Animation/AnimationClip.h) 以反射字符串载体 m_clipsData(JSON)持久化,onAfterDeserialize / onFieldChanged 把其解析 到内部 Animation 列表;onStart 自动播放默认剪辑。编辑器通过 addClip / setClip / removeClip / setDefaultClip 等 API 修改并反向同步 m_clipsData。
运行时也可用原有 API 动态播放: anim->play("walk", sheet, {0,1,2,3,4,5}, 0.1f, true); // 全局帧索引数组
注:如需参数驱动的状态机(状态/转换/参数),请用 Animation/Animator.h。
| AnimationComponent | ( | ) |
|
override |
| void addAnimation | ( | const std::string & | name, |
| std::unique_ptr< Animation > | animation ) |
| int addClip | ( | const std::string & | name | ) |
新增一个剪辑(重名自动追加 " (N)"),返回其索引;-1 失败
| const AnimationClip * clipAt | ( | int | index | ) | const |
取第 i 个剪辑(越界返回 nullptr)
|
inline |
剪辑数量
|
inline |
全部剪辑(只读)
| int defaultClipIndex | ( | ) | const |
取默认剪辑索引;无则 -1
|
inline |
|
inline |
|
inline |
|
overridevirtual |
反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
重载 Component .
|
overridevirtual |
重载 Component .
|
overridevirtual |
重载 Component .
|
overridevirtual |
检查器直接写反射字段后回调(字段名)。用于绕过 setter 的直写完成后 重新同步引擎状态(如 Box2D 刚体/形状)。默认无操作。
重载 Component .
|
overridevirtual |
首次 update 前执行一次
重载 Behavior .
|
overridevirtual |
每帧执行
重载 Behavior .
| void pause | ( | ) |
| void play | ( | const std::string & | name | ) |
| void play | ( | const std::string & | name, |
| const SpriteSheet & | sheet, | ||
| const std::vector< int > & | frames, | ||
| float | duration = 0.1f, | ||
| bool | loop = true ) |
用数字帧索引直接设置并播放一段动画(运行时动态,不落盘)
| bool removeClip | ( | int | index | ) |
移除第 index 个剪辑(若正播放则停止);返回是否成功
| void resume | ( | ) |
| bool setClip | ( | int | index, |
| const AnimationClip & | clip ) |
覆盖第 index 个剪辑为 clip(并重建运行时动画);返回是否成功
| bool setDefaultClip | ( | int | index | ) |
设为/取消默认剪辑(isDefault 唯一化);返回是否成功
| void stop | ( | ) |
| void syncClipsData | ( | ) |
把当前 m_clips 同步到 m_clipsData(JSON 载体)——编辑器/脚本修改后调用