ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Component.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Config.h"
3
4namespace Shit {
5 class GameObject; // 前向声明
6
19 friend class GameObject; // 只能通过GameObject初始化组件
20
21 public:
23 virtual ~Component() = default;
24
25 virtual void onCreate() {} // 组件创建时(有 owner,但可能尚未挂到场景)
26 virtual void onAttach() {} // 组件挂载到活动场景时
27 virtual void onDetach() {} // 组件从场景卸下时
28 virtual void onDestroy() {} // 组件销毁时
29
30 // 禁止拷贝和移动
31 Component(const Component&) = delete;
32 Component& operator=(const Component&) = delete;
33 Component(Component&&) = delete;
35
36 GameObject* getOwner() const { return m_owner; }
37 bool isRegistered() const { return m_isRegistered; }
38
39 private:
40 // 只允许 GameObject 设置
41 inline void setOwner(GameObject* owner) { m_owner = owner; }
42
43 protected:
44 GameObject* m_owner = nullptr; // 组件的拥有者
45 bool m_isRegistered = false;
46 };
47}
#define SHIT_API
定义 Core.h:12
friend class GameObject
定义 Component.h:19
Component & operator=(Component &&)=delete
virtual void onDestroy()
定义 Component.h:28
virtual void onCreate()
定义 Component.h:25
virtual void onAttach()
定义 Component.h:26
Component(const Component &)=delete
GameObject * getOwner() const
定义 Component.h:36
GameObject * m_owner
定义 Component.h:44
Component(Component &&)=delete
virtual void onDetach()
定义 Component.h:27
bool isRegistered() const
定义 Component.h:37
virtual ~Component()=default
Component & operator=(const Component &)=delete
bool m_isRegistered
定义 Component.h:45
游戏物体类
定义 GameObject.h:22
定义 AudioPlayer.h:9