Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / StateController.cs
  1. using System;
  2. using Godot;
  3. using System.Collections.Generic;
  4.  
  5. /// <summary>
  6. /// 对象状态机控制器
  7. /// </summary>
  8. public class StateController<T, S> : Component where T : ActivityObject where S : Enum
  9. {
  10. /// <summary>
  11. /// 获取当前状态
  12. /// </summary>
  13. public S CurrState => CurrStateBase != null ? CurrStateBase.State : default;
  14. /// <summary>
  15. /// 当前活跃的状态对象实例
  16. /// </summary>
  17. public StateBase<T, S> CurrStateBase { get; private set; }
  18.  
  19. /// <summary>
  20. /// 负责存放状态实例对象
  21. /// </summary>
  22. private readonly Dictionary<S, StateBase<T, S>> _states = new Dictionary<S, StateBase<T, S>>();
  23.  
  24. /// <summary>
  25. /// 记录下当前帧是否有改变的状态
  26. /// </summary>
  27. private bool _isChangeState;
  28.  
  29. public override void PhysicsProcess(float delta)
  30. {
  31. _isChangeState = false;
  32. if (CurrStateBase != null)
  33. {
  34. CurrStateBase.PhysicsProcess(delta);
  35. //判断当前帧是否有改变的状态, 如果有, 则重新调用 PhysicsProcess() 方法
  36. if (_isChangeState)
  37. {
  38. PhysicsProcess(delta);
  39. }
  40. }
  41. }
  42.  
  43. public override void DebugDraw()
  44. {
  45. if (CurrStateBase != null)
  46. {
  47. CurrStateBase.DebugDraw();
  48. }
  49. }
  50.  
  51. /// <summary>
  52. /// 往状态机里注册一个新的状态实例
  53. /// </summary>
  54. public void Register(StateBase<T, S> stateBase)
  55. {
  56. if (GetStateInstance(stateBase.State) != null)
  57. {
  58. GD.PrintErr("当前状态已经在状态机中注册:", stateBase);
  59. return;
  60. }
  61.  
  62. stateBase.Master = ActivityObject as T;
  63. stateBase.StateController = this;
  64. _states.Add(stateBase.State, stateBase);
  65. }
  66.  
  67. /// <summary>
  68. /// 获取指定状态对应的实例对象
  69. /// </summary>
  70. public StateBase<T, S> GetState(S state)
  71. {
  72. if (_states.ContainsKey(state))
  73. {
  74. return _states[state];
  75. }
  76.  
  77. return null;
  78. }
  79.  
  80. /// <summary>
  81. /// 立即切换到下一个指定状态, 并且这一帧会被调用 PhysicsProcess
  82. /// </summary>
  83. public void ChangeState(S next, params object[] arg)
  84. {
  85. _changeState(false, next, arg);
  86. }
  87.  
  88. /// <summary>
  89. /// 切换到下一个指定状态, 下一帧才会调用 PhysicsProcess
  90. /// </summary>
  91. public void ChangeStateLate(S next, params object[] arg)
  92. {
  93. _changeState(true, next, arg);
  94. }
  95.  
  96. /// <summary>
  97. /// 根据状态类型获取相应的状态对象
  98. /// </summary>
  99. private StateBase<T, S> GetStateInstance(S stateType)
  100. {
  101. _states.TryGetValue(stateType, out var v);
  102. return v;
  103. }
  104.  
  105. /// <summary>
  106. /// 切换状态
  107. /// </summary>
  108. private void _changeState(bool late, S next, params object[] arg)
  109. {
  110. if (CurrStateBase != null && CurrStateBase.State.Equals(next))
  111. {
  112. return;
  113. }
  114.  
  115. var newState = GetStateInstance(next);
  116. if (newState == null)
  117. {
  118. GD.PrintErr("当前状态机未找到相应状态:" + next);
  119. return;
  120. }
  121.  
  122. if (CurrStateBase == null)
  123. {
  124. CurrStateBase = newState;
  125. newState.Enter(default, arg);
  126. }
  127. else if (CurrStateBase.CanChangeState(next))
  128. {
  129. _isChangeState = !late;
  130. var prev = CurrStateBase.State;
  131. CurrStateBase.Exit(next);
  132. GD.Print("nextState => " + next);
  133. CurrStateBase = newState;
  134. CurrStateBase.Enter(prev, arg);
  135. }
  136. }
  137. }