新增 GameFlow 运行时状态机:PIE 与独立运行统一生命周期

Stopped→Loading→Running↔Paused 单一状态源,编辑器 Play/Stop
与 main.cpp 独立运行共用同一套加载/恢复/暂停语义
This commit is contained in:
2026-10-08 17:02:09 +08:00
parent f73e52926f
commit f327f44269
7 changed files with 329 additions and 62 deletions
+5
View File
@@ -24,6 +24,7 @@
#include <Engine/Core/Types.h>
#include <Engine/Core/Event.h>
#include <Engine/Core/GameFlow.h>
#include <Engine/Core/Time.h>
#include <chrono>
#include <string>
@@ -75,6 +76,7 @@ namespace Engine {
Assets::AssetManager& GetAssets() const { return *m_Assets; }
PhysicsWorld& GetPhysics() const { return *m_Physics; }
Scripting::ScriptRuntime& GetScripts() const { return *m_Scripts; }
GameFlow& GetGameFlow() { return m_GameFlow; }
u64 GetFrameCount() const { return m_FrameCount; }
// ImGui相关
@@ -127,6 +129,9 @@ namespace Engine {
// 其后端销毁需要 Device 与 GLFWwindow 存活)
Scope<ImGuiLayer> m_ImGuiLayer;
// 游戏流状态机(构造即冻结:Stopped → timeScale=0)
GameFlow m_GameFlow;
bool m_Running = false;
u64 m_FrameCount = 0;
+71
View File
@@ -0,0 +1,71 @@
//
// GameFlow.h:游戏流状态机(引擎层唯一的"游戏在干什么"权威)。
//
// 状态:Stopped(编辑态/未启动)→ Loading(加载中)→ Running ⇄ Paused,
// 任意活动态可 RequestStop 回 Stopped。转换矩阵外的请求拒绝 + warn。
//
// 职责边界:
// - timeScale 单一权威:每次转换统一设 Time::SetTimeScale
// (Running/Loading→1,Paused/Stopped→0)。上层(编辑器/游戏)
// 不再自行碰 timeScale——冻结语义由状态推导,不再散落
// - 状态转换广播:AddListener 注册监听(组合根/编辑器 Init 期注册,
// 非每帧路径)。编辑器在 →Loading 挂场景快照、→Stopped 挂恢复;
// 脚本/系统将来同样可订阅(跨系统解耦)
// - 不知场景不知资产:加载本身(读文件/反序列化)是调用方的事,
// 本类只管状态与转换纪律(CompleteLoading 由加载方显式调)
//
// 与主循环的契约(Application::Run):
// - Input::Poll 仅 Running 激活(Paused/Stopped 动作静默)
// - 物理步进仅 Loading 跳过(编辑态对账照跑,停摆靠 dt=0)
// - 脚本仅 Running 执行(Unity Play Mode / UE PIE 语义——脚本副
// 作用不依赖 dt,编辑态不得运行);→Stopped 由监听 EndPlay 终
// 局脚本实例(组合根注册,先于编辑器回滚监听)
//
#ifndef HELLOVULKAN_GAMEFLOW_H
#define HELLOVULKAN_GAMEFLOW_H
#include "Engine/Core/Types.h"
#include <functional>
#include <vector>
namespace Engine {
enum class GameFlowState : u8 {
Stopped, // 编辑态 / 未启动(timeScale=0)
Loading, // 加载中(物理/脚本步进跳过;timeScale=1 无消费方)
Running, // 游戏运行(timeScale=1)
Paused, // 暂停(timeScale=0;编辑器 Pause / 游戏菜单挂起)
};
class GameFlow {
public:
// (from, to);注册期 = 组合根/编辑器 Init,非每帧路径
using Listener = std::function<void(GameFlowState, GameFlowState)>;
GameFlow(); // 构造即冻结(Stopped 语义:timeScale=0)
void AddListener(Listener l) { m_Listeners.push_back(std::move(l)); }
GameFlowState State() const { return m_State; }
bool IsIn(GameFlowState s) const { return m_State == s; }
// ---- 转换请求(矩阵外拒绝 + warn,返回是否生效)----
bool RequestStart(); // Stopped → Loading
void CompleteLoading(); // Loading → Running(加载方显式调)
bool RequestPause(); // Running → Paused
bool RequestResume(); // Paused → Running
bool RequestStop(); // Loading/Running/Paused → Stopped
private:
// 立即生效转换:设 timeScale + 广播(同既有 DoPlay 同步语义)
void Transition(GameFlowState to);
GameFlowState m_State = GameFlowState::Stopped;
std::vector<Listener> m_Listeners;
};
} // namespace Engine
#endif //HELLOVULKAN_GAMEFLOW_H