ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
AnimationComponent.h
浏览该文件的文档.
1#pragma once
2
3#include "Behavior.h"
4#include "../Core/Core.h"
6
7#include <string>
8#include <unordered_map>
9#include <memory>
10#include <vector>
11
12namespace Shit
13{
14 class Animation;
15
33 public:
36
37 // --- 生命周期 ---
38 void onAttach() override;
39 void onStart() override;
40 void onUpdate() override;
41 void onDestroy() override;
42
43 // --- 动画管理 ---
44 void addAnimation(const std::string& name, std::unique_ptr<Animation> animation);
45
46 void play(const std::string& name);
47 void stop();
48 void pause();
49 void resume();
50
63 void play(const std::string& name, const SpriteSheet& sheet,
64 const std::vector<int>& frames, float duration = 0.1f, bool loop = true);
65
66 // --- 查询 ---
67 bool isPlaying() const { return m_isPlaying; }
68 bool isPaused() const { return m_isPaused; }
69 const std::string& getCurrentAnimationName() const { return m_currentAnimationName; }
70
71 private:
72 // 把当前帧源矩形回写到 SpriteRenderer(若存在且纹理一致)
73 void applyCurrentFrame();
74
75 SHIT_META(Disable)
76 std::unordered_map<std::string, std::unique_ptr<Animation>> m_animations;
77 SHIT_META(Disable)
78 Animation* m_currentAnimation = nullptr;
79 SHIT_META(({.displayName = "Current Animation", .readOnly = true}))
80 std::string m_currentAnimationName;
81 SHIT_META(({.displayName = "Current Time", .readOnly = true}))
82 float m_currentTime = 0.0f;
83
84 SHIT_META(({.displayName = "Playing", .readOnly = true}))
85 bool m_isPlaying = false;
86 SHIT_META(({.displayName = "Paused", .readOnly = true}))
87 bool m_isPaused = false;
88 };
89}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:62
#define SHIT_REFLECT(Mode)
定义 Macros.h:59
#define SHIT_META(...)
定义 Macros.h:68
void onStart() override
首次 update 前执行一次
const std::string & getCurrentAnimationName() const
定义 AnimationComponent.h:69
void play(const std::string &name)
bool isPlaying() const
定义 AnimationComponent.h:67
bool isPaused() const
定义 AnimationComponent.h:68
~AnimationComponent() override
void onAttach() override
void play(const std::string &name, const SpriteSheet &sheet, const std::vector< int > &frames, float duration=0.1f, bool loop=true)
用数字帧索引直接设置并播放一段动画
void onUpdate() override
每帧执行
void onDestroy() override
void addAnimation(const std::string &name, std::unique_ptr< Animation > animation)
逐帧动画数据
定义 Animation.h:16
Behavior()=default
精灵图集(sprite-sheet)网格切割器
定义 SpriteSheet.h:23
定义 AudioPlayer.h:10