ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
GameObject类 参考final

游戏物体类 更多...

#include <GameObject.h>

Public 成员函数

 ~GameObject ()=default
 GameObject (const GameObject &)=delete
GameObject & operator= (const GameObject &)=delete
 GameObject (GameObject &&)=delete
GameObject & operator= (GameObject &&)=delete
void destroy ()
 标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体
void setParent (GameObject *parent)
 设置父物体
GameObject * getParent () const
 获取父物体
const std::vector< GameObject * > & getChildren () const
 获取子物体列表
void addChild (GameObject *child)
 添加子物体(等价于 child->setParent(this))
const std::string & getName () const
const std::string & getTag () const
ScenegetScene () const
bool isNeedDestroy () const
void setName (const std::string &name)
void setTag (const std::string &tag)
 设置标签(用于分类,如 "enemy"、"player")
void setScene (Scene *scene)
 设置所属场景(同时触发未注册组件的 onAttach)
void setNeedDestroy (bool needDestroy)
std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents ()
 获取全部组件(按 type_index 索引)
template<typename Fn>
void forEachComponent (Fn &&fn)
 只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map)
template<typename T>
WeakComponentRef< T > getWeakRef ()
 创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)
ComponentaddComponentInstance (Component *component)
 添加组件
template<typename T, typename... Args>
T * addComponent (Args &&... args)
template<typename T>
T * getComponent ()
 获取组件
template<typename T>
bool hasComponent ()
 检查是否存在某个组件
template<typename T>
void removeComponent ()
 移除某个组件

友元

class Scene

详细描述

游戏物体类

构造函数私有,只能通过 Scene::createGameObjectScene::instantiate 创建。

构造及析构函数说明

◆ ~GameObject()

~GameObject ( )
default

◆ GameObject() [1/2]

GameObject ( const GameObject & )
delete

◆ GameObject() [2/2]

GameObject ( GameObject && )
delete

成员函数说明

◆ addChild()

void addChild ( GameObject * child)
inline

添加子物体(等价于 child->setParent(this))

◆ addComponent()

template<typename T, typename... Args>
T * addComponent ( Args &&... args)
inline

◆ addComponentInstance()

Component * addComponentInstance ( Component * component)

添加组件

模板参数
T组件类型(须继承 Component)
Args构造参数类型
参数
args传递给组件构造函数
返回
组件指针(若已存在则返回已有的)

动态添加组件实例(Prefab 实例化等反射场景用,所有权转移给 GameObject)

参数
component已通过反射工厂创建的组件实例
返回
添加后的组件指针(若已存在同类型,则丢弃传入实例并返回已有的)

◆ destroy()

void destroy ( )

标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体

◆ forEachComponent()

template<typename Fn>
void forEachComponent ( Fn && fn)
inline

只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map)

◆ getChildren()

const std::vector< GameObject * > & getChildren ( ) const
inline

获取子物体列表

◆ getComponent()

template<typename T>
T * getComponent ( )
inline

获取组件

模板参数
T组件类型
返回
组件裸指针

◆ getComponents()

std::unordered_map< std::type_index, std::unique_ptr< Component > > & getComponents ( )
inline

获取全部组件(按 type_index 索引)

◆ getName()

const std::string & getName ( ) const
inline

◆ getParent()

GameObject * getParent ( ) const
inline

获取父物体

◆ getScene()

Scene * getScene ( ) const
inline

◆ getTag()

const std::string & getTag ( ) const
inline

◆ getWeakRef()

template<typename T>
WeakComponentRef< T > getWeakRef ( )
inline

创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)

◆ hasComponent()

template<typename T>
bool hasComponent ( )
inline

检查是否存在某个组件

模板参数
T组件类型
返回
是否存在组件

◆ isNeedDestroy()

bool isNeedDestroy ( ) const
inline

◆ operator=() [1/2]

GameObject & operator= ( const GameObject & )
delete

◆ operator=() [2/2]

GameObject & operator= ( GameObject && )
delete

◆ removeComponent()

template<typename T>
void removeComponent ( )
inline

移除某个组件

模板参数
T组件类型

生命周期调用顺序:onDetach → onDestroy

◆ setName()

void setName ( const std::string & name)
inline

◆ setNeedDestroy()

void setNeedDestroy ( bool needDestroy)
inline

◆ setParent()

void setParent ( GameObject * parent)

设置父物体

自动从旧父物体的子列表中移除自己,并加入新父物体的子列表。 传入 nullptr 即解除当前父子关系。跨 Scene 的父子关系将被拒绝并记 WARN。

参数
parent新父物体(可为 nullptr)

◆ setScene()

void setScene ( Scene * scene)

设置所属场景(同时触发未注册组件的 onAttach)

◆ setTag()

void setTag ( const std::string & tag)
inline

设置标签(用于分类,如 "enemy"、"player")

◆ Scene

friend class Scene
friend

该类的文档由以下文件生成: