PlayAreaStateMachine.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  12. public class PlayAreaStateMachine
  13. {
  14. public Dictionary<PlayAreaStateEnum, AbstractPlayAreaState<SafetyAreaMono>> playAreaStateDic;
  15. private IState currentState;
  16. public void InitStateMachine(SafetyAreaMono safetyAreaMono)
  17. {
  18. if (playAreaStateDic == null)
  19. {
  20. playAreaStateDic = new Dictionary<PlayAreaStateEnum, AbstractPlayAreaState<SafetyAreaMono>>();
  21. playAreaStateDic.Add(PlayAreaStateEnum.WaitingDraw, new PrepareDrawPlayAreaState());
  22. playAreaStateDic.Add(PlayAreaStateEnum.OK, new PlayAreaOKState());
  23. playAreaStateDic.Add(PlayAreaStateEnum.NotEnough, new PlayAreaNotEnoughState());
  24. foreach(AbstractPlayAreaState<SafetyAreaMono> valueItem in playAreaStateDic.Values)
  25. {
  26. valueItem.Init(safetyAreaMono);
  27. }
  28. }
  29. }
  30. public void ChangeState(PlayAreaStateEnum playAreaStateEnum, object data = null)
  31. {
  32. if (currentState != null)
  33. {
  34. currentState.OnStateExit(data);
  35. }
  36. IState newState = playAreaStateDic[playAreaStateEnum];
  37. newState.OnStateEnter(data);
  38. currentState = newState;
  39. }
  40. public void ExitCurrentState(object data = null)
  41. {
  42. if (currentState != null)
  43. {
  44. currentState.OnStateExit(data);
  45. }
  46. currentState = null;
  47. }
  48. }