3#include <unordered_map>
26 GameObject(
const std::string& name);
50 const std::vector<GameObject*>&
getChildren()
const {
return m_children; }
56 const std::string&
getName()
const {
return m_name; }
57 const std::string&
getTag()
const {
return m_tag; }
61 void setName(
const std::string& name) { m_name = name; }
62 void setTag(
const std::string& tag) { m_tag = tag; }
65 std::unordered_map<std::type_index, std::unique_ptr<Component>>&
getComponents() {
return m_components; }
68 template <
typename Fn>
70 for (
auto& [type, comp] : m_components) {
71 if (comp) fn(comp.get());
96 template <
typename T,
typename... Args>
98 static_assert(std::is_base_of_v<Component, T>,
"添加的组件必须继承自 Component!");
100 auto type_index = std::type_index(
typeid(
T));
107 auto new_component = std::make_unique<T>(std::forward<Args>(args)...);
108 T* new_component_ptr = new_component.get();
109 new_component->setOwner(
this);
114 m_components[type_index] = std::unique_ptr<Component>(new_component.release());
117 Scene* scene = m_scene;
118 std::weak_ptr<void> lifetime = m_lifetime;
119 const std::string goName = m_name;
121 new_component_ptr->onCreate();
122 if (lifetime.expired())
return nullptr;
127 if (scene && !new_component_ptr->isRegistered()) {
128 new_component_ptr->onAttach();
130 if (lifetime.expired())
return nullptr;
132 ST_CORE_TRACE(
"GameObject : {} 已添加 组件 {}", goName,
typeid(
T).name());
134 return new_component_ptr;
142 template <
typename T>
144 static_assert(std::is_base_of_v<Component, T>,
"获取的组件必须继承自 Component!");
145 auto type_index = std::type_index(
typeid(
T));
147 if (
auto it = m_components.find(type_index); it != m_components.end()) {
148 return static_cast<T*
>(it->second.get());
158 template <
typename T>
160 static_assert(std::is_base_of_v<Component, T>,
"检查的组件必须继承自 Component!");
162 return m_components.contains(std::type_index(
typeid(
T)));
171 template <
typename T>
173 static_assert(std::is_base_of_v<Component, T>,
"移除的组件必须继承自 Component!");
174 auto type_index = std::type_index(
typeid(
T));
176 if (
auto it = m_components.find(type_index); it != m_components.end()) {
178 auto comp = std::move(it->second);
179 m_components.erase(it);
187 void removeFromParentChildren();
191 Scene* m_scene =
nullptr;
192 std::unordered_map<std::type_index, std::unique_ptr<Component>> m_components;
193 bool m_needDestroy =
false;
196 std::vector<GameObject*> m_children;
200 std::shared_ptr<void> m_lifetime = std::make_shared<int>(0);
219 template <
typename T>
224 : m_owner(owner), m_lifetime(lifetime) {}
228 if (!m_owner || m_lifetime.expired())
return nullptr;
229 return m_owner->template getComponent<T>();
237 explicit operator bool()
const {
return valid(); }
244 std::weak_ptr<void> m_lifetime;
#define SHIT_API
定义 Core.h:14
#define ST_CORE_TRACE(...)
定义 Log.h:49
friend class Scene
定义 GameObject.h:24
void setNeedDestroy(bool needDestroy)
定义 GameObject.h:64
void setParent(GameObject *parent)
设置父物体
void addChild(GameObject *child)
添加子物体(等价于 child->setParent(this))
定义 GameObject.h:53
Scene * getScene() const
定义 GameObject.h:58
const std::vector< GameObject * > & getChildren() const
获取子物体列表
定义 GameObject.h:50
void setScene(Scene *scene)
设置所属场景(同时触发未注册组件的 onAttach)
void destroy()
标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体
const std::string & getName() const
定义 GameObject.h:56
GameObject & operator=(const GameObject &)=delete
Component * addComponentInstance(Component *component)
添加组件
GameObject * getParent() const
获取父物体
定义 GameObject.h:49
GameObject & operator=(GameObject &&)=delete
void removeComponent()
移除某个组件
定义 GameObject.h:172
void setTag(const std::string &tag)
设置标签(用于分类,如 "enemy"、"player")
定义 GameObject.h:62
void forEachComponent(Fn &&fn)
只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map)
定义 GameObject.h:69
std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents()
获取全部组件(按 type_index 索引)
定义 GameObject.h:65
void setName(const std::string &name)
定义 GameObject.h:61
bool hasComponent()
检查是否存在某个组件
定义 GameObject.h:159
T * addComponent(Args &&... args)
定义 GameObject.h:97
GameObject(const GameObject &)=delete
WeakComponentRef< T > getWeakRef()
创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)
定义 GameObject.h:77
T * getComponent()
获取组件
定义 GameObject.h:143
GameObject(GameObject &&)=delete
const std::string & getTag() const
定义 GameObject.h:57
bool isNeedDestroy() const
定义 GameObject.h:59
bool valid() const
组件当前是否有效(onAttach 后、removeComponent 前,且 owner 未被销毁)
定义 GameObject.h:233
T * operator->() const
便捷解引用(调用前建议先 valid() 检查或直接解引用判空)
定义 GameObject.h:236
WeakComponentRef(GameObject *owner, const std::weak_ptr< void > &lifetime)
定义 GameObject.h:223
GameObject * getOwner() const
获取所属 GameObject(可能已被销毁,调用方需自行保证安全)
定义 GameObject.h:240
WeakComponentRef()=default
T * get() const
获取组件(组件已移除或对象已销毁则返回 nullptr)
定义 GameObject.h:227