/**注册一个继承IFMS的状态类型 */ export function FMSRegister(name: string) { return function (target:any) { const instance = new target() as IFMS; instance.type=name; FMS.IFMS.set(name, instance); } } /**状态接口*/ export interface IFMS{ type: string; onEntry: () => void; onExit: () => void; } /**全局状态机,游戏流程 (私有有行为机更灵活)*/ export class FMS{ static IFMS: Map = new Map(); private _current:IFMS; /**当前状态*/ get Current():IFMS{return this._current}; get CurrentType():string{ return this._current ? this._current.type:null }; /**初始1个状态*/ public Init(type:string):void{ this.Change(type as T); } /**切换状态*/ public Change(type:T):void{ if(this._current)this._current.onExit(); this._current = FMS.IFMS.get(type ); if(this._current)this._current.onEntry(); } } /**例子: @FMSRegister("Init") export class FMSInit implements IFMS{ type: string; onExit: () => void; onEntry():void{ console.log(this.type,"---------------------"); } } */