简单应用状态机
This commit is contained in:
107
client/assets/scripts/App/AppStatus/AppStatusManager.ts
Normal file
107
client/assets/scripts/App/AppStatus/AppStatusManager.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { FSM } from "../../Framework/FSM/FSM";
|
||||
import { AppStatusBoot } from "./AppStatusBoot";
|
||||
import { AppStatusLogin } from "./AppStatusLogin";
|
||||
import { AppStatusGame } from "./AppStatusGame";
|
||||
|
||||
/**
|
||||
* 应用状态管理器
|
||||
* 职责:
|
||||
* - 管理应用的整体状态流转
|
||||
* - 提供单例访问
|
||||
* - 初始化所有应用状态
|
||||
*
|
||||
* 状态流程: Boot -> Login -> Game
|
||||
*/
|
||||
export class AppStatusManager {
|
||||
private static _instance: AppStatusManager | null = null;
|
||||
private _fsm: FSM;
|
||||
|
||||
/**
|
||||
* 构造函数(私有)
|
||||
*/
|
||||
private constructor() {
|
||||
this._fsm = new FSM();
|
||||
this.initStates();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单例
|
||||
*/
|
||||
static getInstance(): AppStatusManager {
|
||||
if (!this._instance) {
|
||||
this._instance = new AppStatusManager();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化所有状态
|
||||
*/
|
||||
private initStates(): void {
|
||||
console.log("[AppStatusManager] 初始化应用状态...");
|
||||
|
||||
// 添加应用状态: Boot -> Login -> Game
|
||||
this._fsm.addState(new AppStatusBoot(this._fsm));
|
||||
this._fsm.addState(new AppStatusLogin(this._fsm));
|
||||
this._fsm.addState(new AppStatusGame(this._fsm));
|
||||
|
||||
console.log("[AppStatusManager] 应用状态初始化完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动应用
|
||||
* 从Boot状态开始
|
||||
*/
|
||||
start(): void {
|
||||
console.log("[AppStatusManager] 启动应用...");
|
||||
this._fsm.changeState("Boot");
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换状态
|
||||
* @param stateName 状态名称
|
||||
* @param params 可选参数
|
||||
*/
|
||||
changeState(stateName: string, params?: any): void {
|
||||
this._fsm.changeState(stateName, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前状态名称
|
||||
*/
|
||||
getCurrentStateName(): string | null {
|
||||
return this._fsm.getCurrentStateName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前状态
|
||||
*/
|
||||
getCurrentState(): any {
|
||||
return this._fsm.getCurrentState();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新状态机(在主循环中调用)
|
||||
* @param dt 距离上一帧的时间增量(秒)
|
||||
*/
|
||||
update(dt: number): void {
|
||||
this._fsm.update(dt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态机实例
|
||||
* 用于高级操作
|
||||
*/
|
||||
getFSM(): FSM {
|
||||
return this._fsm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁管理器
|
||||
*/
|
||||
destroy(): void {
|
||||
console.log("[AppStatusManager] 销毁应用状态管理器");
|
||||
this._fsm.clear();
|
||||
AppStatusManager._instance = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user