1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /**注册一个继承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;
- }
- /**全局状态机,游戏流程 (私有有行为机更灵活)*/
- class FMS{
- static IFMS: Map<string,IFMS> = new Map();
- private _current:IFMS;
- /**当前状态*/
- get Current():IFMS{return this._current};
- get CurrentTaype():string{return this._current ? null:this._current.type};
- /**初始1个状态*/
- public Init(type:string):void{
- this.Change(type);
- }
- /**切换状态*/
- public Change(type:string):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,"---------------------");
- }
- }
- */
- export default function get_new_fms():FMS { return new FMS;}
|