|
ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
|
游戏物体类 更多...
#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 |
| Scene * | getScene () 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 组件的弱引用(组件被移除/对象销毁后自动失效) | |
| Component * | addComponentInstance (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::createGameObject 或 Scene::instantiate 创建。
|
default |
|
delete |
|
delete |
|
inline |
添加子物体(等价于 child->setParent(this))
|
inline |
添加组件
| T | 组件类型(须继承 Component) |
| Args | 构造参数类型 |
| args | 传递给组件构造函数 |
动态添加组件实例(Prefab 实例化等反射场景用,所有权转移给 GameObject)
| component | 已通过反射工厂创建的组件实例 |
| void destroy | ( | ) |
标记为待销毁(帧末由 Scene 统一清理);级联标记所有子物体
|
inline |
只读遍历全部组件(供系统补扫等使用,避免暴露内部可变 map)
|
inline |
获取子物体列表
|
inline |
获取组件
| T | 组件类型 |
|
inline |
获取全部组件(按 type_index 索引)
|
inline |
|
inline |
获取父物体
|
inline |
|
inline |
|
inline |
创建指向本对象上 T 组件的弱引用(组件被移除/对象销毁后自动失效)
|
inline |
检查是否存在某个组件
| T | 组件类型 |
|
inline |
|
delete |
|
delete |
|
inline |
移除某个组件
| T | 组件类型 |
生命周期调用顺序:onDetach → onDestroy
|
inline |
|
inline |
| void setParent | ( | GameObject * | parent | ) |
设置父物体
自动从旧父物体的子列表中移除自己,并加入新父物体的子列表。 传入 nullptr 即解除当前父子关系。跨 Scene 的父子关系将被拒绝并记 WARN。
| parent | 新父物体(可为 nullptr) |
| void setScene | ( | Scene * | scene | ) |
设置所属场景(同时触发未注册组件的 onAttach)
|
inline |
设置标签(用于分类,如 "enemy"、"player")
|
friend |