123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using CIS;
- using UnityEngine;
- namespace MonsterLove.StateMachine
- {
- public class StateMachineEngine : MonoBehaviour
- {
- public void Initialize<T>(StateMachineBehaviour entity)
- {
- Array values = Enum.GetValues(typeof(T));
- this.stateLookup = new Dictionary<Enum, StateMapping>();
- for (int i = 0; i < values.Length; i++)
- {
- StateMapping stateMapping = new StateMapping((Enum)values.GetValue(i));
- this.stateLookup.Add(stateMapping.state, stateMapping);
- }
- MethodInfo[] methods = entity.GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- char[] separator = "_".ToCharArray();
- for (int j = 0; j < methods.Length; j++)
- {
- string[] array = methods[j].Name.Split(separator);
- if (array.Length > 1)
- {
- Enum key;
- try
- {
- key = (Enum)Enum.Parse(typeof(T), array[0]);
- }
- catch (ArgumentException)
- {
- for (int k = 0; k < this.ignoredNames.Length; k++)
- {
- if (array[0] == this.ignoredNames[k])
- {
- break;
- }
- }
- goto IL_279;
- }
- StateMapping stateMapping2 = this.stateLookup[key];
- string text = array[1];
- if (text != null)
- {
- if (!(text == "Enter"))
- {
- if (!(text == "Exit"))
- {
- if (!(text == "Update"))
- {
- if (!(text == "LateUpdate"))
- {
- if (text == "FixedUpdate")
- {
- stateMapping2.FixedUpdate = this.CreateDelegate<Action>(methods[j], entity);
- }
- }
- else
- {
- stateMapping2.LateUpdate = this.CreateDelegate<Action>(methods[j], entity);
- }
- }
- else
- {
- stateMapping2.Update = this.CreateDelegate<Action>(methods[j], entity);
- }
- }
- else if (methods[j].ReturnType == typeof(IEnumerator))
- {
- stateMapping2.Exit = this.CreateDelegate<StateMapping.DoNothingCoroutine>(methods[j], entity);
- }
- else
- {
- StateMapping.DoNothing action = this.CreateDelegate<StateMapping.DoNothing>(methods[j], entity);
- stateMapping2.Exit = delegate(object userData)
- {
- action(userData);
- return null;
- };
- }
- }
- else if (methods[j].ReturnType == typeof(IEnumerator))
- {
- stateMapping2.Enter = this.CreateDelegate<StateMapping.DoNothingCoroutine>(methods[j], entity);
- }
- else
- {
- StateMapping.DoNothing action = this.CreateDelegate<StateMapping.DoNothing>(methods[j], entity);
- stateMapping2.Enter = delegate(object userData)
- {
- action(userData);
- return null;
- };
- }
- }
- }
- IL_279:;
- }
- }
- private V CreateDelegate<V>(MethodInfo method, object target) where V : class
- {
- V v = Delegate.CreateDelegate(typeof(V), target, method) as V;
- if (v == null)
- {
- throw new ArgumentException("Unabled to create delegate for method called " + method.Name);
- }
- return v;
- }
- public void ChangeState(Enum newState, object userData = null)
- {
- if (this.stateLookup == null)
- {
- throw new Exception("States have not been configured, please call initialized before trying to set state");
- }
- if (!this.stateLookup.ContainsKey(newState))
- {
- 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");
- }
- if (this.debug && this.currentState != null)
- {
- UnityEngine.Debug.Log(string.Concat(new object[]
- {
- "currState: ",
- this.currentState.state,
- " nextState: ",
- newState
- }));
- }
- StateMapping stateMapping = this.stateLookup[newState];
- if (this.currentState == stateMapping)
- {
- return;
- }
- this._ChangeToNewStateRoutine(stateMapping, userData);
- }
- private void _ChangeToNewStateRoutine(StateMapping newState, object userData = null)
- {
- if (this.currentState != null)
- {
- IEnumerator enumerator = this.currentState.Exit(null);
- if (enumerator != null)
- {
- base.StartCoroutine(enumerator);
- }
- }
- this.currentState = newState;
- if (this.currentState != null)
- {
- IEnumerator enumerator2 = this.currentState.Enter(userData);
- if (enumerator2 != null)
- {
- base.StartCoroutine(enumerator2);
- }
- }
- }
- private IEnumerator ChangeToNewStateRoutine(StateMapping newState)
- {
- if (this.currentState != null)
- {
- IEnumerator exitRoutine = this.currentState.Exit(null);
- if (exitRoutine != null)
- {
- yield return base.StartCoroutine(exitRoutine);
- }
- }
- this.currentState = newState;
- if (this.currentState != null)
- {
- IEnumerator enterRoutine = this.currentState.Enter(null);
- if (enterRoutine != null)
- {
- yield return base.StartCoroutine(enterRoutine);
- }
- }
- yield break;
- }
- private void FixedUpdate()
- {
- if (!this.isStart)
- {
- return;
- }
- if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
- {
- return;
- }
- if (this.currentState != null)
- {
- this.currentState.FixedUpdate();
- }
- }
- private void Update()
- {
- if (!this.isStart)
- {
- return;
- }
- if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
- {
- return;
- }
- if (this.currentState != null)
- {
- this.currentState.Update();
- }
- }
- private void LateUpdate()
- {
- if (!this.isStart)
- {
- return;
- }
- if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
- {
- return;
- }
- if (this.currentState != null)
- {
- this.currentState.LateUpdate();
- }
- }
- public void PauseStateMachine(bool isPause)
- {
- this.isStart = !isPause;
- }
- public static void DoNothing()
- {
- }
- public static void DoNothingCollider(Collider other)
- {
- }
- public static void DoNothingCollision(Collision other)
- {
- }
- public static IEnumerator DoNothingCoroutine(object userData = null)
- {
- yield break;
- }
- public Enum GetState()
- {
- if (this.currentState != null)
- {
- return this.currentState.state;
- }
- return null;
- }
- public bool debug;
- private StateMapping currentState;
- private Dictionary<Enum, StateMapping> stateLookup;
- private Dictionary<string, Delegate> methodLookup;
- private readonly string[] ignoredNames = new string[]
- {
- "add",
- "remove",
- "get",
- "set"
- };
- private bool isStart = true;
- }
- }
|