Beispiel #1
0
 // Make sure you init the Machine
 public bool ChangeState(T state)
 {
     if (state.Equals(_CurrentState))
     {
         return(false);
     }
     else
     {
         _CurrentMapping.Exit();
         _CurrentState   = state;
         _CurrentMapping = _StateLookup[state];
         _CurrentMapping.Enter();
         return(true);
     }
 }
Beispiel #2
0
 public void Init(T initState)
 {
     _CurrentState   = initState;
     _CurrentMapping = _StateLookup[initState];
     _CurrentMapping.Enter();
 }
Beispiel #3
0
        void Awake()
        {
            List <MonoBehaviour> scripts = new List <MonoBehaviour>();

            GetComponentsInChildren(_AutoManageChildren, scripts);
            foreach (MonoBehaviour script in scripts)
            {
                MethodInfo[] methods = script.GetType().GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (MethodInfo method in methods)
                {
                    foreach (StateListenerAttribute sa in method.GetCustomAttributes(typeof(StateListenerAttribute), true))
                    {
                        System.Object state     = sa.state;
                        Type          stateType = state.GetType();
                        System.Object stateMachine;
                        Type          stateMachineType = Type.GetType("Toolkit.StateMachine`1[" + stateType + "]");
                        if (!_StateMachineLookup.TryGetValue(stateMachineType, out stateMachine))
                        {
                            stateMachine = Activator.CreateInstance(stateMachineType);
                            _StateMachineLookup.Add(stateMachineType, stateMachine);
                        }
                        System.Object stateLookup  = stateMachine.GetType().GetField("_StateLookup", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(stateMachine);
                        StateMapping  stateMapping = (StateMapping)stateLookup.GetType().GetMethod("get_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Invoke(stateLookup, new object[] { state });
                        switch (sa.on)
                        {
                        case StateEvent.Enter:
                            if (method.ReturnType == typeof(IEnumerator))
                            {
                                Func <IEnumerator> func = CreateDelegate <Func <IEnumerator> >(method, script);
                                stateMapping.Enter = () => { script.StartCoroutine(func()); };
                            }
                            else
                            {
                                stateMapping.Enter = CreateDelegate <Action>(method, script);
                            }
                            break;

                        case StateEvent.Exit:
                            stateMapping.Exit = CreateDelegate <Action>(method, script);
                            break;

                        case StateEvent.FixedUpdate:
                            stateMapping.FixedUpdate = CreateDelegate <Action>(method, script);
                            break;

                        case StateEvent.LateUpdate:
                            stateMapping.LateUpdate = CreateDelegate <Action>(method, script);
                            break;

                        case StateEvent.Update:
                            stateMapping.Update = CreateDelegate <Action>(method, script);
                            break;
                        }
                    }
                }
                FieldInfo[] fields = script.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                foreach (FieldInfo field in fields)
                {
                    if (field.GetCustomAttributes(typeof(StateMachineInjectAttribute), true).Length > 0)
                    {
                        if (!field.FieldType.ToString().Contains("StateMachine"))
                        {
                            throw new Exception("The filed type not a state machine");
                        }

                        System.Object stateMachine;
                        Type          stateMachineType = field.FieldType;
                        if (!_StateMachineLookup.TryGetValue(stateMachineType, out stateMachine))
                        {
                            stateMachine = Activator.CreateInstance(stateMachineType);;
                            _StateMachineLookup.Add(stateMachineType, stateMachine);
                        }
                        field.SetValue(script, stateMachine);
                    }
                }
            }
        }