Beispiel #1
0
        public void ChangeState(E key)
        {
            IQState <T> state = Get(key);

            if (state == null)
            {
                Debug.LogError("该状态不存在: " + key);
                return;
            }

            if (currentState == state)
            {
                Debug.LogError("该状态已存在: " + key);
                return;
            }

            //退出之前状态
            if (currentState != null)
            {
                currentState.Exit();
            }

            //设置当前状态
            currentState = state;

            //进入当前状态
            if (currentState != null)
            {
                currentState.Enter();
            }
        }
Beispiel #2
0
        public void SetCurrentState(E key)
        {
            IQState <T> state = Get(key);

            currentState = state;
            currentState.Enter();
        }
Beispiel #3
0
 public void Add(E key, IQState <T> node)
 {
     if (!states.ContainsKey(key))
     {
         states.Add(key, node);
     }
 }
Beispiel #4
0
        public IQStateMachine(T _root)
        {
            root         = _root;
            currentState = null;
            globalStates = new List <IQState <T> >();
            states       = new Dictionary <E, IQState <T> >();

            int num = 0;

            foreach (E e in System.Enum.GetValues(typeof(E)))
            {
                string classname = typeof(T).ToString() + "+State" + e.ToString();
                Type   t         = Type.GetType(classname);
                if (t == null)
                {
                    Debug.LogError("状态[" + e.ToString() + "]没有找到匹配的状态类[" + classname + "]!");
                }
                else
                {
                    IQState <T> state = (IQState <T>)Activator.CreateInstance(t);
                    state.Root = root;
                    states.Add(e, state);

                    if (num == 0)
                    {
                        SetCurrentState(e);
                    }
                    num++;
                }
            }
        }
Beispiel #5
0
 //
 public void RemoveGlobalState(IQState <T> state)
 {
     if (globalStates.Contains(state))
     {
         state.Exit();
         globalStates.Remove(state);
     }
 }
Beispiel #6
0
        public void SetGlobalState(E key)
        {
            IQState <T> state = Get(key);

            if (!globalStates.Contains(state))
            {
                state.Enter();
                globalStates.Add(state);
            }
        }