PlayAreaStateMachine.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public enum PlayAreaStateEnum
  7. {
  8. WaitingDraw,
  9. NotEnough,
  10. OK,
  11. ConfirmPlayArea,
  12. }
  13. public class PlayAreaStateMachine
  14. {
  15. public Dictionary<PlayAreaStateEnum, AbstractPlayAreaState<SafetyAreaMono>> playAreaStateDic;
  16. private IState currentState;
  17. public void InitStateMachine(SafetyAreaMono safetyAreaMono)
  18. {
  19. if (playAreaStateDic == null)
  20. {
  21. playAreaStateDic = new Dictionary<PlayAreaStateEnum, AbstractPlayAreaState<SafetyAreaMono>>();
  22. playAreaStateDic.Add(PlayAreaStateEnum.WaitingDraw, new PrepareDrawPlayAreaState());
  23. playAreaStateDic.Add(PlayAreaStateEnum.OK, new PlayAreaOKState());
  24. playAreaStateDic.Add(PlayAreaStateEnum.NotEnough, new PlayAreaNotEnoughState());
  25. playAreaStateDic.Add(PlayAreaStateEnum.ConfirmPlayArea, new ConfirmPlayAreaState());
  26. foreach (AbstractPlayAreaState<SafetyAreaMono> valueItem in playAreaStateDic.Values)
  27. {
  28. valueItem.Init(safetyAreaMono);
  29. }
  30. }
  31. }
  32. public void ChangeState(PlayAreaStateEnum playAreaStateEnum, object data = null)
  33. {
  34. if (currentState != null)
  35. {
  36. currentState.OnStateExit(data);
  37. }
  38. IState newState = playAreaStateDic[playAreaStateEnum];
  39. newState.OnStateEnter(data);
  40. currentState = newState;
  41. }
  42. public void ExitCurrentState(object data = null)
  43. {
  44. if (currentState != null)
  45. {
  46. currentState.OnStateExit(data);
  47. }
  48. currentState = null;
  49. }
  50. }