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 统一清理)
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 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

成员函数说明

◆ addComponent()

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

添加组件

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

◆ destroy()

void destroy ( )

标记为待销毁(帧末由 Scene 统一清理)

◆ 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

◆ getScene()

Scene * getScene ( ) const
inline

◆ getTag()

const std::string & getTag ( ) const
inline

◆ 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

◆ setScene()

void setScene ( Scene * scene)

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

◆ setTag()

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

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

◆ Scene

friend class Scene
friend

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