FMS.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**注册一个继承IFMS的状态类型 */
  2. export function FMSRegister(name: string) {
  3. return function (target:any) {
  4. const instance = new target() as IFMS;
  5. instance.type=name;
  6. FMS.IFMS.set(name, instance);
  7. }
  8. }
  9. /**状态接口*/
  10. export interface IFMS{
  11. type: string;
  12. onEntry: () => void;
  13. onExit: () => void;
  14. }
  15. /**全局状态机,游戏流程 (私有有行为机更灵活)*/
  16. export class FMS<T extends string>{
  17. static IFMS: Map<string,IFMS> = new Map();
  18. private _current:IFMS;
  19. /**当前状态*/
  20. get Current():IFMS{return this._current};
  21. get CurrentType():string{
  22. return this._current ? this._current.type:null
  23. };
  24. /**初始1个状态*/
  25. public Init(type:string):void{
  26. this.Change(type as T);
  27. }
  28. /**切换状态*/
  29. public Change(type:T):void{
  30. if(this._current)this._current.onExit();
  31. this._current = FMS.IFMS.get(type );
  32. if(this._current)this._current.onEntry();
  33. }
  34. }
  35. /**例子:
  36. @FMSRegister("Init")
  37. export class FMSInit implements IFMS{
  38. type: string;
  39. onExit: () => void;
  40. onEntry():void{
  41. console.log(this.type,"---------------------");
  42. }
  43. }
  44. */