![]() |
ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
|
场景类 更多...
#include <Scene.h>
Public 成员函数 | |
| Scene (const std::string &name) | |
| ~Scene () | |
| Scene (const Scene &)=delete | |
| Scene & | operator= (const Scene &)=delete |
| Scene (Scene &&)=delete | |
| Scene & | operator= (Scene &&)=delete |
| void | init () |
| 注册默认系统(BehaviorSystem + RenderSystem + UIRenderSystem),幂等 | |
| void | update () |
| 更新所有 System(固定步循环 + 变步长遍历)+ 处理延迟操作 | |
| void | destroy () |
| 销毁所有对象与系统 | |
| bool | hasSystems () const |
| 是否已注册任何 System(SceneManager 用于自动初始化,防止漏调 init() 导致空场景) | |
| bool | hasEnabledCamera () |
| 场景中是否存在已启用的相机(含延迟添加的 pending 对象)。 运行态加载场景时对象先进 m_pendingAdditions、m_gameObjects 为空, 只扫正式容器会误判"没有相机"而重复注入兜底相机。 | |
| GameObject * | findGameObjectByName (const std::string &name) |
| 遍历全部对象(正式 + pending),返回第一个名为 name 的对象。 与 hasEnabledCamera 同理,覆盖 pending 防止运行态漏判。 | |
| void | addGameObject (std::unique_ptr< GameObject > &&gameObject) |
| 延迟添加 GameObject(帧末生效) | |
| GameObject * | createGameObject (const std::string &name) |
| 创建并添加 GameObject | |
| GameObject * | instantiate (const Prefab &prefab, const std::string &name="") |
| 从预制体实例化 | |
| void | removeGameObject (GameObject *gameObject) |
| 按指针标记销毁 | |
| void | removeGameObjectByName (const std::string &name) |
| 按名称标记销毁 | |
| bool | registerComponent (Component *component) |
| 组件挂载广播(组件 onAttach 调用)。分发给可处理的系统,返回是否有系统认领。 | |
| void | unregisterComponent (Component *component) |
| 组件卸下广播(组件 onDetach 调用)。 | |
| template<typename T> | |
| T * | registerSystem () |
| template<typename T> | |
| void | unregisterSystem () |
| template<typename T> | |
| T * | getSystem () |
| template<typename T> | |
| bool | hasSystem () |
| System * | registerSystem (const std::string &typeName) |
| 按类型名注册系统(通过反射 Factory 创建实例)。幂等(已有则返回现有)。 失败(类型未注册/无工厂/非 System 派生)返回 nullptr 并 WARN。 | |
| System * | getSystem (const std::string &typeName) const |
| 按类型名查询系统(返回 nullptr 表示未注册) | |
| bool | hasSystem (const std::string &typeName) const |
| 按类型名判断系统是否已注册 | |
| void | unregisterSystem (const std::string &typeName) |
| 按类型名延迟移除系统 | |
| std::vector< std::string > | getRegisteredSystemTypeNames () const |
| 返回当前已注册系统的类型名列表(按优先级排序) | |
| void | setSystemPriority (const std::string &typeName, int priority) |
| 调整系统优先级(即时生效,下一帧 update 前重排序) | |
| void | unregisterSystemsBySource (const std::string &source) |
| 卸载指定来源注册的所有系统(插件卸载前调用,防止卸载 DLL 后 vtable 悬垂) | |
| void | flushPendingSystemRemovals () |
| 立即处理待移除系统队列(热重载卸载 DLL 前调用,确保 vtable 悬垂前销毁系统) | |
| const std::string & | getName () const |
| std::vector< std::unique_ptr< GameObject > > & | getGameObjects () |
| void | setName (const std::string &name) |
| uint64_t | getGeneration () const |
| 场景结构代数:任何对象增删/组件增删/改名/改父都会递增。 编辑器据此判断"场景内容是否变化",及时重建场景树、校验选中态, 避免播放中对象被游戏逻辑销毁后仍持有悬垂指针。 | |
| bool | containsGameObject (const GameObject *gameObject) const |
| 对象是否仍在当前场景容器中(地址比较,供编辑器校验旧选中指针) | |
| Component * | componentByUuid (uint64_t uuid) const |
| 按持久 UUID 查找组件(ComponentRef<T> 懒解析 / 编辑器拖拽引用共用) 组件已移除或对象已销毁后返回 nullptr(UUID 索引随组件生命周期维护)。 | |
静态 Public 属性 | |
| static constexpr float | FIXED_TIME_STEP = 1.0f / 60.0f |
| 固定步长(秒):onFixedUpdate 与物理模拟的共同节拍 | |
| static constexpr int | MAX_FIXED_STEPS_PER_FRAME = 3 |
| 单渲染帧最大固定步数(超出丢弃,防死亡螺旋) | |
友元 | |
| class | GameObject |
| GameObject 结构变更(组件增删/改名/改父)时通知 bumpGeneration | |
场景类
场景是游戏世界的容器,管理所有 GameObject、System 的生命周期。 通过 SceneManager 的栈机制实现场景切换(主菜单→游戏→暂停)。
使用方式(从 .scene 文件加载——唯一公开入口): SceneManager::LoadSceneFromFile("Scenes/level1.scene");
注:Scene 对象通常由 SceneManager 内部创建,用户代码无需手动构造。 手动构造 Scene 仅用于编辑器预览/单元测试等高级场景。
|
explicit |
| ~Scene | ( | ) |
|
delete |
|
delete |
| void addGameObject | ( | std::unique_ptr< GameObject > && | gameObject | ) |
延迟添加 GameObject(帧末生效)
|
inline |
按持久 UUID 查找组件(ComponentRef<T> 懒解析 / 编辑器拖拽引用共用) 组件已移除或对象已销毁后返回 nullptr(UUID 索引随组件生命周期维护)。
| bool containsGameObject | ( | const GameObject * | gameObject | ) | const |
对象是否仍在当前场景容器中(地址比较,供编辑器校验旧选中指针)
| GameObject * createGameObject | ( | const std::string & | name | ) |
创建并添加 GameObject
| void destroy | ( | ) |
销毁所有对象与系统
| GameObject * findGameObjectByName | ( | const std::string & | name | ) |
遍历全部对象(正式 + pending),返回第一个名为 name 的对象。 与 hasEnabledCamera 同理,覆盖 pending 防止运行态漏判。
| void flushPendingSystemRemovals | ( | ) |
立即处理待移除系统队列(热重载卸载 DLL 前调用,确保 vtable 悬垂前销毁系统)
|
inline |
|
inline |
场景结构代数:任何对象增删/组件增删/改名/改父都会递增。 编辑器据此判断"场景内容是否变化",及时重建场景树、校验选中态, 避免播放中对象被游戏逻辑销毁后仍持有悬垂指针。
|
inline |
| std::vector< std::string > getRegisteredSystemTypeNames | ( | ) | const |
返回当前已注册系统的类型名列表(按优先级排序)
|
inline |
| System * getSystem | ( | const std::string & | typeName | ) | const |
按类型名查询系统(返回 nullptr 表示未注册)
| bool hasEnabledCamera | ( | ) |
场景中是否存在已启用的相机(含延迟添加的 pending 对象)。 运行态加载场景时对象先进 m_pendingAdditions、m_gameObjects 为空, 只扫正式容器会误判"没有相机"而重复注入兜底相机。
|
inline |
| bool hasSystem | ( | const std::string & | typeName | ) | const |
按类型名判断系统是否已注册
|
inline |
是否已注册任何 System(SceneManager 用于自动初始化,防止漏调 init() 导致空场景)
| void init | ( | ) |
注册默认系统(BehaviorSystem + RenderSystem + UIRenderSystem),幂等
| GameObject * instantiate | ( | const Prefab & | prefab, |
| const std::string & | name = "" ) |
从预制体实例化
| bool registerComponent | ( | Component * | component | ) |
组件挂载广播(组件 onAttach 调用)。分发给可处理的系统,返回是否有系统认领。
|
inline |
| System * registerSystem | ( | const std::string & | typeName | ) |
按类型名注册系统(通过反射 Factory 创建实例)。幂等(已有则返回现有)。 失败(类型未注册/无工厂/非 System 派生)返回 nullptr 并 WARN。
| void removeGameObject | ( | GameObject * | gameObject | ) |
按指针标记销毁
| void removeGameObjectByName | ( | const std::string & | name | ) |
按名称标记销毁
|
inline |
| void setSystemPriority | ( | const std::string & | typeName, |
| int | priority ) |
调整系统优先级(即时生效,下一帧 update 前重排序)
| void unregisterComponent | ( | Component * | component | ) |
组件卸下广播(组件 onDetach 调用)。
|
inline |
| void unregisterSystem | ( | const std::string & | typeName | ) |
按类型名延迟移除系统
| void unregisterSystemsBySource | ( | const std::string & | source | ) |
卸载指定来源注册的所有系统(插件卸载前调用,防止卸载 DLL 后 vtable 悬垂)
| void update | ( | ) |
更新所有 System(固定步循环 + 变步长遍历)+ 处理延迟操作
|
friend |
GameObject 结构变更(组件增删/改名/改父)时通知 bumpGeneration
|
staticconstexpr |
固定步长(秒):onFixedUpdate 与物理模拟的共同节拍
|
staticconstexpr |
单渲染帧最大固定步数(超出丢弃,防死亡螺旋)