6#include <unordered_map>
39 explicit Scene(
const std::string& name);
81 static_assert(std::is_base_of_v<System, T>,
"必须继承自 System 基类");
83 auto type_index = std::type_index(
typeid(
T));
89 auto new_system = std::make_unique<T>();
90 T* new_system_ptr = new_system.get();
92 new_system_ptr->setScene(
this);
97 m_systemsMap[type_index] = std::unique_ptr<System>(new_system.release());
98 m_systems.push_back(new_system_ptr);
99 m_isSystemsNeedSort =
true;
101 new_system_ptr->init();
105 return new_system_ptr;
108 template <
typename T>
110 static_assert(std::is_base_of_v<System, T>,
"必须继承自 System 基类");
112 m_pendingRemoveSystems.push_back(std::type_index(
typeid(
T)));
115 template <
typename T>
117 static_assert(std::is_base_of_v<System, T>,
"必须继承自 System 基类");
118 auto type_index = std::type_index(
typeid(
T));
120 if (
auto it = m_systemsMap.find(type_index); it != m_systemsMap.end()) {
121 return static_cast<T*
>(it->second.get());
128 static_assert(std::is_base_of_v<System, T>,
"必须继承自 System 基类");
130 return m_systemsMap.contains(std::type_index(
typeid(
T)));
161 const std::string&
getName()
const {
return m_name; }
162 std::vector<std::unique_ptr<GameObject>>&
getGameObjects() {
return m_gameObjects; }
164 void setName(
const std::string& name) { m_name = name; }
179 void processPendingAdditions();
180 void processPendingRemoveSystems();
181 void bumpGeneration() { ++m_generation; }
185 void indexComponentUuid(Component* component);
187 void unindexComponentUuid(Component* component);
190 std::vector<std::unique_ptr<GameObject>> m_gameObjects;
191 std::vector<std::unique_ptr<GameObject>> m_pendingAdditions;
192 uint64_t m_generation = 0;
193 std::unordered_map<uint64_t, Component*> m_uuidMap;
195 std::unordered_map<std::type_index, std::unique_ptr<System>> m_systemsMap;
196 std::vector<System*> m_systems;
197 std::vector<std::type_index> m_pendingRemoveSystems;
198 bool m_isSystemsNeedSort =
false;
199 bool m_isInited =
false;
200 float m_fixedAccumulator = 0.0f;
#define SHIT_API
定义 Core.h:14
#define ST_CORE_TRACE(...)
定义 Log.h:67
行为基类 —— 用于编写自定义游戏逻辑
定义 Behavior.h:22
相机组件,定义"从哪个角度观察世界"
定义 CameraComponent.h:16
预制体,可重复生成相同配置的 GameObject
定义 Prefab.h:23
渲染组件基类
定义 RendererComponent.h:18
friend class GameObject
GameObject 结构变更(组件增删/改名/改父)时通知 bumpGeneration
定义 Scene.h:37
void addGameObject(std::unique_ptr< GameObject > &&gameObject)
延迟添加 GameObject(帧末生效)
Scene & operator=(const Scene &)=delete
void init()
注册默认系统(BehaviorSystem + RenderSystem + UIRenderSystem),幂等
uint64_t getGeneration() const
场景结构代数:任何对象增删/组件增删/改名/改父都会递增。 编辑器据此判断"场景内容是否变化",及时重建场景树、校验选中态, 避免播放中对象被游戏逻辑销毁后仍持有悬垂指针。
定义 Scene.h:169
static constexpr float FIXED_TIME_STEP
固定步长(秒):onFixedUpdate 与物理模拟的共同节拍
定义 Scene.h:52
void unregisterSystemsBySource(const std::string &source)
卸载指定来源注册的所有系统(插件卸载前调用,防止卸载 DLL 后 vtable 悬垂)
Component * componentByUuid(uint64_t uuid) const
按持久 UUID 查找组件(ComponentRef<T> 懒解析 / 编辑器拖拽引用共用) 组件已移除或对象已销毁后返回 nullptr(UUID 索引随组件生命周期维护)。
定义 Scene.h:176
void removeGameObjectByName(const std::string &name)
按名称标记销毁
const std::string & getName() const
定义 Scene.h:161
void removeGameObject(GameObject *gameObject)
按指针标记销毁
System * registerSystem(const std::string &typeName)
按类型名注册系统(通过反射 Factory 创建实例)。幂等(已有则返回现有)。 失败(类型未注册/无工厂/非 System 派生)返回 nullptr 并 WARN。
bool containsGameObject(const GameObject *gameObject) const
对象是否仍在当前场景容器中(地址比较,供编辑器校验旧选中指针)
void unregisterComponent(Component *component)
组件卸下广播(组件 onDetach 调用)。
bool hasSystems() const
是否已注册任何 System(SceneManager 用于自动初始化,防止漏调 init() 导致空场景)
定义 Scene.h:57
System * getSystem(const std::string &typeName) const
按类型名查询系统(返回 nullptr 表示未注册)
std::vector< std::string > getRegisteredSystemTypeNames() const
返回当前已注册系统的类型名列表(按优先级排序)
void flushPendingSystemRemovals()
立即处理待移除系统队列(热重载卸载 DLL 前调用,确保 vtable 悬垂前销毁系统)
bool hasEnabledCamera()
场景中是否存在已启用的相机(含延迟添加的 pending 对象)。 运行态加载场景时对象先进 m_pendingAdditions、m_gameObjects 为空, 只扫正式容器会误判"没有相机"而重...
void setName(const std::string &name)
定义 Scene.h:164
GameObject * createGameObject(const std::string &name)
创建并添加 GameObject
void setSystemPriority(const std::string &typeName, int priority)
调整系统优先级(即时生效,下一帧 update 前重排序)
Scene(const Scene &)=delete
GameObject * instantiate(const Prefab &prefab, const std::string &name="")
从预制体实例化
T * getSystem()
定义 Scene.h:116
void unregisterSystem()
定义 Scene.h:109
void update()
更新所有 System(固定步循环 + 变步长遍历)+ 处理延迟操作
T * registerSystem()
定义 Scene.h:80
void unregisterSystem(const std::string &typeName)
按类型名延迟移除系统
bool hasSystem(const std::string &typeName) const
按类型名判断系统是否已注册
static constexpr int MAX_FIXED_STEPS_PER_FRAME
单渲染帧最大固定步数(超出丢弃,防死亡螺旋)
定义 Scene.h:53
GameObject * findGameObjectByName(const std::string &name)
遍历全部对象(正式 + pending),返回第一个名为 name 的对象。 与 hasEnabledCamera 同理,覆盖 pending 防止运行态漏判。
std::vector< std::unique_ptr< GameObject > > & getGameObjects()
定义 Scene.h:162
bool hasSystem()
定义 Scene.h:127
Scene(const std::string &name)
Scene & operator=(Scene &&)=delete
bool registerComponent(Component *component)
组件挂载广播(组件 onAttach 调用)。分发给可处理的系统,返回是否有系统认领。
场景管理器(单例)
定义 SceneManager.h:19