ShitEngine
1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
AnimationComponent.h
浏览该文件的文档.
1
#pragma once
2
3
#include "
Behavior.h
"
4
#include "
../Core/Core.h
"
5
#include "
../Animation/AnimationClip.h
"
6
#include "
../Render/SpriteSheet.h
"
7
8
#include <nlohmann/json.hpp>
9
10
#include <string>
11
#include <unordered_map>
12
#include <memory>
13
#include <vector>
14
15
namespace
Shit
16
{
17
class
Animation
;
18
35
class
SHIT_API
SHIT_REFLECT
(BlackList)
AnimationComponent
:
public
Behavior
{
36
SHIT_REFLECT_BODY
(
AnimationComponent
)
37
public
:
38
AnimationComponent
();
39
~AnimationComponent
()
override
;
40
41
// --- 生命周期 ---
42
void
onAttach
()
override
;
43
void
onStart
()
override
;
44
void
onUpdate
()
override
;
45
void
onDestroy
()
override
;
46
void
onAfterDeserialize
()
override
;
47
void
onFieldChanged
(
const
std::string& fieldName)
override
;
48
49
// --- 动画管理(运行时动态 API,保持兼容) ---
50
void
addAnimation
(
const
std::string& name, std::unique_ptr<Animation> animation);
51
52
void
play
(
const
std::string& name);
53
void
stop
();
54
void
pause
();
55
void
resume
();
56
60
void
play
(
const
std::string& name,
const
SpriteSheet
& sheet,
61
const
std::vector<int>& frames,
float
duration = 0.1f,
bool
loop =
true
);
62
63
// --- P28 剪辑管理(编辑器 + 序列化) ---
64
66
int
clipCount
()
const
{
return
static_cast<
int
>
(m_clips.size()); }
68
const
AnimationClip
*
clipAt
(
int
index)
const
;
70
const
std::vector<AnimationClip>&
clips
()
const
{
return
m_clips; }
71
73
int
addClip
(
const
std::string& name);
75
bool
setClip
(
int
index,
const
AnimationClip
& clip);
77
bool
removeClip
(
int
index);
79
bool
setDefaultClip
(
int
index);
81
int
defaultClipIndex
()
const
;
82
84
void
syncClipsData
();
85
86
// --- 查询 ---
87
bool
isPlaying
()
const
{
return
m_isPlaying; }
88
bool
isPaused
()
const
{
return
m_isPaused; }
89
const
std::string&
getCurrentAnimationName
()
const
{
return
m_currentAnimationName; }
90
91
private
:
92
// 把当前帧源矩形回写到 SpriteRenderer(若存在且纹理一致)
93
void
applyCurrentFrame();
94
// 解析 m_clipsData 到 m_clips 并重建运行时 Animation 列表
95
void
parseClipsData();
96
// 把 m_clips 重建为 m_animations(覆盖同名);供 onStart/onAfterDeserialize/剪辑变更后调用
97
void
rebuildFromClips();
98
// 剪辑数据变化后的统一入口:重建 + 同步载体
99
void
notifyClipsChanged();
100
101
SHIT_META
(Disable)
102
std::unordered_map<std::string, std::unique_ptr<Animation>> m_animations;
103
SHIT_META
(Disable)
104
Animation
* m_currentAnimation =
nullptr
;
105
// P28:运行时剪辑列表(从 m_clipsData 解析而来)
106
SHIT_META
(Disable)
107
std::vector<AnimationClip> m_clips;
108
// P28:序列化载体——所有剪辑的 JSON 数组字符串
109
// 注意:不能标 readOnly——readOnly 字段会被 Prefab 序列化跳过,导致剪辑数据丢失
110
SHIT_META
(({.displayName =
"Animation Clips"
, .tooltip =
"序列化载体,由编辑器维护(JSON)"
}))
111
std::string m_clipsData;
112
113
SHIT_META
(({.displayName =
"Current Animation"
, .readOnly =
true
}))
114
std::string m_currentAnimationName;
115
SHIT_META
(({.displayName =
"Current Time"
, .readOnly =
true
}))
116
float
m_currentTime = 0.0f;
117
118
SHIT_META
(({.displayName =
"Playing"
, .readOnly =
true
}))
119
bool
m_isPlaying =
false
;
120
SHIT_META
(({.displayName =
"Paused"
, .readOnly =
true
}))
121
bool
m_isPaused =
false
;
122
};
123
}
AnimationClip.h
Behavior.h
Core.h
SHIT_API
#define SHIT_API
定义
Core.h:14
SHIT_REFLECT_BODY
#define SHIT_REFLECT_BODY(Type)
定义
Macros.h:89
SHIT_REFLECT
#define SHIT_REFLECT(Mode)
定义
Macros.h:86
SHIT_META
#define SHIT_META(...)
定义
Macros.h:95
SpriteSheet.h
Shit::AnimationComponent::onStart
void onStart() override
首次 update 前执行一次
Shit::AnimationComponent::getCurrentAnimationName
const std::string & getCurrentAnimationName() const
定义
AnimationComponent.h:89
Shit::AnimationComponent::clipCount
int clipCount() const
剪辑数量
定义
AnimationComponent.h:66
Shit::AnimationComponent::clipAt
const AnimationClip * clipAt(int index) const
取第 i 个剪辑(越界返回 nullptr)
Shit::AnimationComponent::removeClip
bool removeClip(int index)
移除第 index 个剪辑(若正播放则停止);返回是否成功
Shit::AnimationComponent::play
void play(const std::string &name)
Shit::AnimationComponent::isPlaying
bool isPlaying() const
定义
AnimationComponent.h:87
Shit::AnimationComponent::defaultClipIndex
int defaultClipIndex() const
取默认剪辑索引;无则 -1
Shit::AnimationComponent::resume
void resume()
Shit::AnimationComponent::isPaused
bool isPaused() const
定义
AnimationComponent.h:88
Shit::AnimationComponent::setDefaultClip
bool setDefaultClip(int index)
设为/取消默认剪辑(isDefault 唯一化);返回是否成功
Shit::AnimationComponent::syncClipsData
void syncClipsData()
把当前 m_clips 同步到 m_clipsData(JSON 载体)——编辑器/脚本修改后调用
Shit::AnimationComponent::pause
void pause()
Shit::AnimationComponent::stop
void stop()
Shit::AnimationComponent::onAfterDeserialize
void onAfterDeserialize() override
反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
Shit::AnimationComponent::onFieldChanged
void onFieldChanged(const std::string &fieldName) override
检查器直接写反射字段后回调(字段名)。用于绕过 setter 的直写完成后 重新同步引擎状态(如 Box2D 刚体/形状)。默认无操作。
Shit::AnimationComponent::~AnimationComponent
~AnimationComponent() override
Shit::AnimationComponent::onAttach
void onAttach() override
Shit::AnimationComponent::AnimationComponent
AnimationComponent()
Shit::AnimationComponent::setClip
bool setClip(int index, const AnimationClip &clip)
覆盖第 index 个剪辑为 clip(并重建运行时动画);返回是否成功
Shit::AnimationComponent::play
void play(const std::string &name, const SpriteSheet &sheet, const std::vector< int > &frames, float duration=0.1f, bool loop=true)
用数字帧索引直接设置并播放一段动画(运行时动态,不落盘)
Shit::AnimationComponent::onUpdate
void onUpdate() override
每帧执行
Shit::AnimationComponent::addClip
int addClip(const std::string &name)
新增一个剪辑(重名自动追加 " (N)"),返回其索引;-1 失败
Shit::AnimationComponent::clips
const std::vector< AnimationClip > & clips() const
全部剪辑(只读)
定义
AnimationComponent.h:70
Shit::AnimationComponent::onDestroy
void onDestroy() override
Shit::AnimationComponent::addAnimation
void addAnimation(const std::string &name, std::unique_ptr< Animation > animation)
Shit::Animation
逐帧动画数据
定义
Animation.h:17
Shit::Behavior::Behavior
Behavior()=default
Shit::SpriteSheet
Sprite sheet grid cutter
定义
SpriteSheet.h:23
Shit
定义
AnimationClip.h:12
Shit::AnimationClip
动画剪辑(可序列化数据,P28)
定义
AnimationClip.h:31
include
ShitEngine
Component
AnimationComponent.h
制作者
1.17.0