ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
Component.h
浏览该文件的文档.
1
#pragma once
2
#include "
../Core/Config.h
"
3
#include "
../Reflection/Macros.h
"
4
5
namespace
Shit
{
6
class
GameObject
;
// 前向声明
7
class
System
;
// 前向声明(friend:系统销毁时重置认领组件的注册状态)
8
20
class
SHIT_API
SHIT_REFLECT
(BlackList)
Component
{
21
friend
class
GameObject
;
// 只能通过GameObject初始化组件
22
friend
class
System
;
// 系统销毁时通过 System::resetComponent 重置组件注册状态
23
SHIT_REFLECT_BODY
(
Component
)
24
25
public
:
26
Component
();
27
virtual
~Component
() =
default
;
28
29
virtual
void
onCreate
() {}
// 组件创建时(有 owner,但可能尚未挂到场景)
30
virtual
void
onAttach
() {
m_isRegistered
=
true
; }
// 组件挂载到活动场景时(基类默认标记已注册;依赖系统的组件在找不到系统时自行置回 false 以便补挂)
31
virtual
void
onDetach
() {}
// 组件从场景卸下时
32
virtual
void
onDestroy
() {}
// 组件销毁时
33
36
virtual
void
onAfterDeserialize
() {}
37
38
// 禁止拷贝和移动
39
Component
(
const
Component
&) =
delete
;
40
Component
&
operator=
(
const
Component
&) =
delete
;
41
Component
(
Component
&&) =
delete
;
42
Component
&
operator=
(
Component
&&) =
delete
;
43
44
GameObject
*
getOwner
()
const
{
return
m_owner
; }
45
bool
isRegistered
()
const
{
return
m_isRegistered
; }
46
47
private
:
48
// 只允许 GameObject 设置
49
inline
void
setOwner(
GameObject
* owner) { m_owner = owner; }
50
51
protected
:
52
// 内部:仅 System 通过 resetComponent 调用,用于系统注销后重置注册状态,
53
// 使已存在组件在同类系统重新注册时能被 System::init 补扫认领。
54
inline
void
setRegistered
(
bool
registered) {
m_isRegistered
= registered; }
55
56
protected
:
57
SHIT_META
(({.displayName =
"Owner"
, .readOnly =
true
}))
58
GameObject
*
m_owner
=
nullptr
;
// 组件的拥有者
59
SHIT_META
(({.displayName =
"Registered"
, .readOnly =
true
}))
60
bool
m_isRegistered
=
false
;
61
};
62
}
Config.h
SHIT_API
#define SHIT_API
定义
Core.h:14
Macros.h
SHIT_REFLECT_BODY
#define SHIT_REFLECT_BODY(Type)
定义
Macros.h:62
SHIT_REFLECT
#define SHIT_REFLECT(Mode)
定义
Macros.h:59
SHIT_META
#define SHIT_META(...)
定义
Macros.h:68
Shit::Component::GameObject
friend class GameObject
定义
Component.h:21
Shit::Component::Component
Component()
Shit::Component::setRegistered
void setRegistered(bool registered)
定义
Component.h:54
Shit::Component::operator=
Component & operator=(Component &&)=delete
Shit::Component::onDestroy
virtual void onDestroy()
定义
Component.h:32
Shit::Component::onCreate
virtual void onCreate()
定义
Component.h:29
Shit::Component::onAttach
virtual void onAttach()
定义
Component.h:30
Shit::Component::Component
Component(const Component &)=delete
Shit::Component::getOwner
GameObject * getOwner() const
定义
Component.h:44
Shit::Component::m_owner
GameObject * m_owner
定义
Component.h:58
Shit::Component::Component
Component(Component &&)=delete
Shit::Component::onDetach
virtual void onDetach()
定义
Component.h:31
Shit::Component::isRegistered
bool isRegistered() const
定义
Component.h:45
Shit::Component::~Component
virtual ~Component()=default
Shit::Component::operator=
Component & operator=(const Component &)=delete
Shit::Component::onAfterDeserialize
virtual void onAfterDeserialize()
反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
定义
Component.h:36
Shit::Component::m_isRegistered
bool m_isRegistered
定义
Component.h:60
Shit::Component::System
friend class System
定义
Component.h:22
Shit::GameObject
游戏物体类
定义
GameObject.h:23
Shit::System
System 基类
定义
System.h:15
Shit
定义
AudioPlayer.h:10
include
ShitEngine
Component
Component.h
制作者
1.17.0