|
ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
|
动画组件 更多...
#include <AnimationComponent.h>
Public 成员函数 | |
| AnimationComponent () | |
| ~AnimationComponent () override | |
| void | onAttach () override |
| void | onStart () override |
| 首次 update 前执行一次 | |
| void | onUpdate () override |
| 每帧执行 | |
| void | onDestroy () override |
| 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) |
| 用数字帧索引直接设置并播放一段动画 | |
| bool | isPlaying () const |
| bool | isPaused () const |
| const std::string & | getCurrentAnimationName () const |
| Public 成员函数 继承自 Behavior | |
| Behavior ()=default | |
| ~Behavior () override=default | |
| void | onCreate () override |
| void | onAttach () override |
| 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 |
额外继承的成员函数 | |
| Protected 属性 继承自 Behavior | |
| bool | m_isStarted = false |
| Protected 属性 继承自 Component | |
| GameObject * | m_owner = nullptr |
| bool | m_isRegistered = false |
动画组件
继承 Behavior,由 BehaviorSystem 每帧调用 onUpdate 推进当前动画时间, 并把当前帧的源矩形 (SDL_FRect) 回写到同 GameObject 上的 SpriteRenderer。
典型用法(三点式 sprite-sheet): Shit::SpriteSheet sheet(4, 8, 32, 32); // 4行8列,每帧32×32 auto* anim = go->addComponent<AnimationComponent>(); anim->play("walk", sheet, {0,1,2,3,4,5}, 0.1f, true); // 全局帧索引数组
或手工添加已构造好的 Animation: anim->addAnimation("walk", std::move(walkAnim)); anim->play("walk");
| AnimationComponent | ( | ) |
|
override |
| void addAnimation | ( | const std::string & | name, |
| std::unique_ptr< Animation > | animation ) |
|
inline |
|
inline |
|
inline |
|
overridevirtual |
重载 Component .
|
overridevirtual |
重载 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 ) |
用数字帧索引直接设置并播放一段动画
按 frames 中的索引从 sheet 切出条帧,构造一个 Animation 并以 name 登记/覆盖后播放。 若 name 已存在则替换。
| name | 动画名(用作登记与 play 索引) |
| sheet | 精灵图集,提供行列网格切割 |
| frames | 想要播放的全局帧索引序列(如 {0,1,2,3,5,8},可不连续) |
| duration | 每帧持续时间(秒) |
| loop | 是否循环 |
| void resume | ( | ) |
| void stop | ( | ) |