StateMachineEngine.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using CIS;
  6. using UnityEngine;
  7. namespace MonsterLove.StateMachine
  8. {
  9. public class StateMachineEngine : MonoBehaviour
  10. {
  11. public void Initialize<T>(StateMachineBehaviour entity)
  12. {
  13. Array values = Enum.GetValues(typeof(T));
  14. this.stateLookup = new Dictionary<Enum, StateMapping>();
  15. for (int i = 0; i < values.Length; i++)
  16. {
  17. StateMapping stateMapping = new StateMapping((Enum)values.GetValue(i));
  18. this.stateLookup.Add(stateMapping.state, stateMapping);
  19. }
  20. MethodInfo[] methods = entity.GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  21. char[] separator = "_".ToCharArray();
  22. for (int j = 0; j < methods.Length; j++)
  23. {
  24. string[] array = methods[j].Name.Split(separator);
  25. if (array.Length > 1)
  26. {
  27. Enum key;
  28. try
  29. {
  30. key = (Enum)Enum.Parse(typeof(T), array[0]);
  31. }
  32. catch (ArgumentException)
  33. {
  34. for (int k = 0; k < this.ignoredNames.Length; k++)
  35. {
  36. if (array[0] == this.ignoredNames[k])
  37. {
  38. break;
  39. }
  40. }
  41. goto IL_279;
  42. }
  43. StateMapping stateMapping2 = this.stateLookup[key];
  44. string text = array[1];
  45. if (text != null)
  46. {
  47. if (!(text == "Enter"))
  48. {
  49. if (!(text == "Exit"))
  50. {
  51. if (!(text == "Update"))
  52. {
  53. if (!(text == "LateUpdate"))
  54. {
  55. if (text == "FixedUpdate")
  56. {
  57. stateMapping2.FixedUpdate = this.CreateDelegate<Action>(methods[j], entity);
  58. }
  59. }
  60. else
  61. {
  62. stateMapping2.LateUpdate = this.CreateDelegate<Action>(methods[j], entity);
  63. }
  64. }
  65. else
  66. {
  67. stateMapping2.Update = this.CreateDelegate<Action>(methods[j], entity);
  68. }
  69. }
  70. else if (methods[j].ReturnType == typeof(IEnumerator))
  71. {
  72. stateMapping2.Exit = this.CreateDelegate<StateMapping.DoNothingCoroutine>(methods[j], entity);
  73. }
  74. else
  75. {
  76. StateMapping.DoNothing action = this.CreateDelegate<StateMapping.DoNothing>(methods[j], entity);
  77. stateMapping2.Exit = delegate(object userData)
  78. {
  79. action(userData);
  80. return null;
  81. };
  82. }
  83. }
  84. else if (methods[j].ReturnType == typeof(IEnumerator))
  85. {
  86. stateMapping2.Enter = this.CreateDelegate<StateMapping.DoNothingCoroutine>(methods[j], entity);
  87. }
  88. else
  89. {
  90. StateMapping.DoNothing action = this.CreateDelegate<StateMapping.DoNothing>(methods[j], entity);
  91. stateMapping2.Enter = delegate(object userData)
  92. {
  93. action(userData);
  94. return null;
  95. };
  96. }
  97. }
  98. }
  99. IL_279:;
  100. }
  101. }
  102. private V CreateDelegate<V>(MethodInfo method, object target) where V : class
  103. {
  104. V v = Delegate.CreateDelegate(typeof(V), target, method) as V;
  105. if (v == null)
  106. {
  107. throw new ArgumentException("Unabled to create delegate for method called " + method.Name);
  108. }
  109. return v;
  110. }
  111. public void ChangeState(Enum newState, object userData = null)
  112. {
  113. if (this.stateLookup == null)
  114. {
  115. throw new Exception("States have not been configured, please call initialized before trying to set state");
  116. }
  117. if (!this.stateLookup.ContainsKey(newState))
  118. {
  119. throw new Exception("No state with the name " + newState.ToString() + " can be found. Please make sure you are called the correct type the statemachine was initialized with");
  120. }
  121. if (this.debug && this.currentState != null)
  122. {
  123. UnityEngine.Debug.Log(string.Concat(new object[]
  124. {
  125. "currState: ",
  126. this.currentState.state,
  127. " nextState: ",
  128. newState
  129. }));
  130. }
  131. StateMapping stateMapping = this.stateLookup[newState];
  132. if (this.currentState == stateMapping)
  133. {
  134. return;
  135. }
  136. this._ChangeToNewStateRoutine(stateMapping, userData);
  137. }
  138. private void _ChangeToNewStateRoutine(StateMapping newState, object userData = null)
  139. {
  140. if (this.currentState != null)
  141. {
  142. IEnumerator enumerator = this.currentState.Exit(null);
  143. if (enumerator != null)
  144. {
  145. base.StartCoroutine(enumerator);
  146. }
  147. }
  148. this.currentState = newState;
  149. if (this.currentState != null)
  150. {
  151. IEnumerator enumerator2 = this.currentState.Enter(userData);
  152. if (enumerator2 != null)
  153. {
  154. base.StartCoroutine(enumerator2);
  155. }
  156. }
  157. }
  158. private IEnumerator ChangeToNewStateRoutine(StateMapping newState)
  159. {
  160. if (this.currentState != null)
  161. {
  162. IEnumerator exitRoutine = this.currentState.Exit(null);
  163. if (exitRoutine != null)
  164. {
  165. yield return base.StartCoroutine(exitRoutine);
  166. }
  167. }
  168. this.currentState = newState;
  169. if (this.currentState != null)
  170. {
  171. IEnumerator enterRoutine = this.currentState.Enter(null);
  172. if (enterRoutine != null)
  173. {
  174. yield return base.StartCoroutine(enterRoutine);
  175. }
  176. }
  177. yield break;
  178. }
  179. private void FixedUpdate()
  180. {
  181. if (!this.isStart)
  182. {
  183. return;
  184. }
  185. if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
  186. {
  187. return;
  188. }
  189. if (this.currentState != null)
  190. {
  191. this.currentState.FixedUpdate();
  192. }
  193. }
  194. private void Update()
  195. {
  196. if (!this.isStart)
  197. {
  198. return;
  199. }
  200. if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
  201. {
  202. return;
  203. }
  204. if (this.currentState != null)
  205. {
  206. this.currentState.Update();
  207. }
  208. }
  209. private void LateUpdate()
  210. {
  211. if (!this.isStart)
  212. {
  213. return;
  214. }
  215. if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
  216. {
  217. return;
  218. }
  219. if (this.currentState != null)
  220. {
  221. this.currentState.LateUpdate();
  222. }
  223. }
  224. public void PauseStateMachine(bool isPause)
  225. {
  226. this.isStart = !isPause;
  227. }
  228. public static void DoNothing()
  229. {
  230. }
  231. public static void DoNothingCollider(Collider other)
  232. {
  233. }
  234. public static void DoNothingCollision(Collision other)
  235. {
  236. }
  237. public static IEnumerator DoNothingCoroutine(object userData = null)
  238. {
  239. yield break;
  240. }
  241. public Enum GetState()
  242. {
  243. if (this.currentState != null)
  244. {
  245. return this.currentState.state;
  246. }
  247. return null;
  248. }
  249. public bool debug;
  250. private StateMapping currentState;
  251. private Dictionary<Enum, StateMapping> stateLookup;
  252. private Dictionary<string, Delegate> methodLookup;
  253. private readonly string[] ignoredNames = new string[]
  254. {
  255. "add",
  256. "remove",
  257. "get",
  258. "set"
  259. };
  260. private bool isStart = true;
  261. }
  262. }