ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
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"
12#include "../Scene/Scene.h"
13
14namespace 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>
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>
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>
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}
#define SHIT_API
定义 Core.h:12
#define ST_CORE_TRACE(...)
定义 Log.h:48
~GameObject()=default
friend class Scene
定义 GameObject.h:23
void setNeedDestroy(bool needDestroy)
定义 GameObject.h:47
Scene * getScene() const
定义 GameObject.h:41
void setScene(Scene *scene)
设置所属场景(同时触发未注册组件的 onAttach)
void destroy()
标记为待销毁(帧末由 Scene 统一清理)
const std::string & getName() const
定义 GameObject.h:39
GameObject & operator=(const GameObject &)=delete
GameObject & operator=(GameObject &&)=delete
void removeComponent()
移除某个组件
定义 GameObject.h:120
void setTag(const std::string &tag)
设置标签(用于分类,如 "enemy"、"player")
定义 GameObject.h:45
std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents()
获取全部组件(按 type_index 索引)
定义 GameObject.h:48
void setName(const std::string &name)
定义 GameObject.h:44
bool hasComponent()
检查是否存在某个组件
定义 GameObject.h:107
T * addComponent(Args &&... args)
添加组件
定义 GameObject.h:59
GameObject(const GameObject &)=delete
T * getComponent()
获取组件
定义 GameObject.h:91
GameObject(GameObject &&)=delete
const std::string & getTag() const
定义 GameObject.h:40
bool isNeedDestroy() const
定义 GameObject.h:42
场景类
定义 Scene.h:34
定义 AudioPlayer.h:9
@ T
定义 KeyCode.h:31