ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
GameObject.h
浏览该文件的文档.
1
#pragma once
2
#include <string>
3
#include <unordered_map>
4
#include <typeindex>
5
#include <memory>
6
#include <utility>
7
8
#include "
../Core/Config.h
"
9
#include "
../Component/Component.h
"
10
#include "
../Component/Behavior.h
"
11
#include "
../Component/RendererComponent.h
"
12
#include "
../Scene/Scene.h
"
13
14
namespace
Shit
{
15
class
Scene
;
// 前向声明
16
22
class
SHIT_API
GameObject final {
23
friend
class
Scene
;
24
private
:
25
GameObject(
const
std::string& name);
26
27
public
:
28
~GameObject
() =
default
;
29
30
// 禁止拷贝和移动
31
GameObject
(
const
GameObject&) =
delete
;
32
GameObject&
operator=
(
const
GameObject&) =
delete
;
33
GameObject
(GameObject&&) =
delete
;
34
GameObject&
operator=
(GameObject&&) =
delete
;
35
36
void
destroy
();
37
38
// --- getter & setter ---
39
const
std::string&
getName
()
const
{
return
m_name; }
40
const
std::string&
getTag
()
const
{
return
m_tag; }
41
Scene
*
getScene
()
const
{
return
m_scene; }
42
bool
isNeedDestroy
()
const
{
return
m_needDestroy; }
43
44
void
setName
(
const
std::string& name) { m_name = name; }
45
void
setTag
(
const
std::string& tag) { m_tag = tag; }
46
void
setScene
(
Scene
* scene);
47
void
setNeedDestroy
(
bool
needDestroy) { m_needDestroy = needDestroy; }
48
std::unordered_map<std::type_index, std::unique_ptr<Component>>&
getComponents
() {
return
m_components; }
49
57
58
template
<
typename
T
,
typename
... Args>
59
T
*
addComponent
(Args&&... args) {
60
static_assert
(std::is_base_of_v<Component, T>,
"添加的组件必须继承自 Component!"
);
61
62
auto
type_index = std::type_index(
typeid
(
T
));
63
64
if
(
hasComponent<T>
()) {
// 是否已经存在
65
return
getComponent<T>
();
66
}
67
68
// 创建组件
69
auto
new_component = std::make_unique<T>(std::forward<Args>(args)...);
70
T
* new_component_ptr = new_component.get();
71
new_component->setOwner(
this
);
72
new_component->onCreate();
// onCreate:轻量初始化
73
74
// 若已挂载场景则立即执行 onAttach(注册到 System)
75
if
(m_scene) {
76
new_component->onAttach();
77
}
78
79
m_components[type_index] = std::unique_ptr<Component>(new_component.release());
80
ST_CORE_TRACE
(
"GameObject : {} 已添加 组件 {}"
, m_name,
typeid
(
T
).name());
81
82
return
new_component_ptr;
83
}
84
90
template
<
typename
T>
91
T
*
getComponent
() {
92
static_assert
(std::is_base_of_v<Component, T>,
"获取的组件必须继承自 Component!"
);
93
auto
type_index = std::type_index(
typeid
(
T
));
94
95
if
(
auto
it = m_components.find(type_index); it != m_components.end()) {
96
return
static_cast<
T
*
>
(it->second.get());
97
}
98
return
nullptr
;
99
}
100
106
template
<
typename
T>
107
bool
hasComponent
() {
108
static_assert
(std::is_base_of_v<Component, T>,
"检查的组件必须继承自 Component!"
);
109
110
return
m_components.contains(std::type_index(
typeid
(
T
)));
111
}
112
119
template
<
typename
T>
120
void
removeComponent
() {
121
static_assert
(std::is_base_of_v<Component, T>,
"移除的组件必须继承自 Component!"
);
122
auto
type_index = std::type_index(
typeid
(
T
));
123
124
if
(
auto
it = m_components.find(type_index); it != m_components.end()) {
125
it->second->onDetach();
126
it->second->onDestroy();
127
m_components.erase(it);
128
}
129
}
130
131
private
:
132
void
clean();
// 清理(只能 Scene 调用)
133
134
std::string m_name;
// 游戏物体名称
135
std::string m_tag;
// 标签
136
Scene
* m_scene =
nullptr
;
// 所在 Scene 指针
137
std::unordered_map<std::type_index, std::unique_ptr<Component>> m_components;
// 挂载的组件
138
bool
m_needDestroy =
false
;
// 是否需要销毁(由 Scene 负责销毁)
139
};
140
}
Behavior.h
Component.h
Config.h
SHIT_API
#define SHIT_API
定义
Core.h:12
ST_CORE_TRACE
#define ST_CORE_TRACE(...)
定义
Log.h:48
RendererComponent.h
Scene.h
Shit::GameObject::~GameObject
~GameObject()=default
Shit::GameObject::Scene
friend class Scene
定义
GameObject.h:23
Shit::GameObject::setNeedDestroy
void setNeedDestroy(bool needDestroy)
定义
GameObject.h:47
Shit::GameObject::getScene
Scene * getScene() const
定义
GameObject.h:41
Shit::GameObject::setScene
void setScene(Scene *scene)
设置所属场景(同时触发未注册组件的 onAttach)
Shit::GameObject::destroy
void destroy()
标记为待销毁(帧末由 Scene 统一清理)
Shit::GameObject::getName
const std::string & getName() const
定义
GameObject.h:39
Shit::GameObject::operator=
GameObject & operator=(const GameObject &)=delete
Shit::GameObject::operator=
GameObject & operator=(GameObject &&)=delete
Shit::GameObject::removeComponent
void removeComponent()
移除某个组件
定义
GameObject.h:120
Shit::GameObject::setTag
void setTag(const std::string &tag)
设置标签(用于分类,如 "enemy"、"player")
定义
GameObject.h:45
Shit::GameObject::getComponents
std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents()
获取全部组件(按 type_index 索引)
定义
GameObject.h:48
Shit::GameObject::setName
void setName(const std::string &name)
定义
GameObject.h:44
Shit::GameObject::hasComponent
bool hasComponent()
检查是否存在某个组件
定义
GameObject.h:107
Shit::GameObject::addComponent
T * addComponent(Args &&... args)
添加组件
定义
GameObject.h:59
Shit::GameObject::GameObject
GameObject(const GameObject &)=delete
Shit::GameObject::getComponent
T * getComponent()
获取组件
定义
GameObject.h:91
Shit::GameObject::GameObject
GameObject(GameObject &&)=delete
Shit::GameObject::getTag
const std::string & getTag() const
定义
GameObject.h:40
Shit::GameObject::isNeedDestroy
bool isNeedDestroy() const
定义
GameObject.h:42
Shit::Scene
场景类
定义
Scene.h:34
Shit
定义
AudioPlayer.h:9
Shit::KeyCode::T
@ T
定义
KeyCode.h:31
ShitEngine
GameObject
GameObject.h
制作者
1.17.0