Example #1
0
 protected void InitFSM()
 {
     if (m_StateMachine == null)
     {
         m_StateMachine = new FStateMachine(name, m_States.Length, m_Transitions.Length);
         m_StateMachine.MaxStackSize = m_StackSize;
         MonoBehaviour impl = m_OtherImplement ?? this;
         for (int i = 0; i < m_States.Length; i++)
         {
             FiniteState state = m_States[i];
             if (state.Init(impl))
             {
                 m_StateMachine.AddState(state, state.m_IsDefaultState);
             }
         }
         for (int i = 0; i < m_Transitions.Length; i++)
         {
             FiniteStateTransition trans = m_Transitions[i];
             if (trans.Init(impl))
             {
                 m_StateMachine.AddTransition(trans.m_FromState, trans.m_ToState, trans);
             }
         }
     }
 }
        private void GenerateTransitions(Type type)
        {
            object[] methods = Utility.Ref.GetMethodsWithAttribute <FStateTransitionAttribute>(type, true);
            if (methods == null)
            {
                m_Transitions = new FiniteStateTransition[0];
                return;
            }
            List <FiniteStateTransition> transitions = new List <FiniteStateTransition>();

            for (int i = 0; i < methods.Length; i++)
            {
                System.Reflection.MethodInfo mtd = methods[i] as System.Reflection.MethodInfo;
                if (!Utility.Ref.MatchMethodRetAndParams(mtd, typeof(bool), null))
                {
                    Debug.LogError(string.Format("{0} 不能作为状态方法,因为类型不能和 \" bool Method() \" 匹配.", mtd.Name));
                    continue;
                }
                object[] trans = mtd.GetCustomAttributes(typeof(FStateTransitionAttribute), true);
                for (int j = 0; j < trans.Length; j++)
                {
                    FStateTransitionAttribute t  = trans[j] as FStateTransitionAttribute;
                    FiniteStateTransition     ft = new FiniteStateTransition();
                    ft.m_FromState = t.From ?? "";
                    if (!string.IsNullOrEmpty(ft.m_FromState) && !HasState(ft.m_FromState))
                    {
                        continue;
                    }
                    ft.m_ToState = t.To ?? "";
                    if (!string.IsNullOrEmpty(ft.m_ToState) && !HasState(ft.m_ToState))
                    {
                        continue;
                    }
                    ft.m_ConditionMethod = mtd.Name ?? "";
                    transitions.Add(ft);
                }
            }
            transitions.Sort((x, y) => string.IsNullOrEmpty(x.m_FromState) || string.IsNullOrEmpty(x.m_ToState) ? 1 : -1);
            m_Transitions = transitions.ToArray();
        }