FMS.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. class FMS{
  17. static IFMS: Map<string,IFMS> = new Map();
  18. private _current:IFMS;
  19. /**当前状态*/
  20. get Current():IFMS{return this._current};
  21. get CurrentTaype():string{return this._current ? null:this._current.type};
  22. /**初始1个状态*/
  23. public Init(type:string):void{
  24. this.Change(type);
  25. }
  26. /**切换状态*/
  27. public Change(type:string):void{
  28. if(this._current)this._current.onExit();
  29. this._current = FMS.IFMS.get(type);
  30. if(this._current)this._current.onEntry();
  31. }
  32. }
  33. /**例子:
  34. @FMSRegister("Init")
  35. export class FMSInit implements IFMS{
  36. type: string;
  37. onExit: () => void;
  38. onEntry():void{
  39. console.log(this.type,"---------------------");
  40. }
  41. }
  42. */
  43. export default function get_new_fms():FMS { return new FMS;}