Example #1
0
        public void Start(Type stateType)
        {
            if (IsRunning)
            {
                throw new GameFrameworkException("FSM is running, can not start again.");
            }

            if (stateType == null)
            {
                throw new GameFrameworkException("State type is invalid.");
            }

            if (!typeof(AFsmState<T>).IsAssignableFrom(stateType))
            {
                throw new GameFrameworkException(string.Format("State type '{0}' is invalid.", stateType.FullName));
            }

            AFsmState<T> state = GetState(stateType);
            if (state == null)
            {
                throw new GameFrameworkException(string.Format("FSM '{0}' can not start state '{1}' which is not exist.", state, stateType.FullName));
            }

            m_CurrentStateTime = 0f;
            m_CurrentState = state;
            m_CurrentState.OnEnter(this);
        }
Example #2
0
 public TState GetState<TState>() where TState : AFsmState<T>
 {
     AFsmState<T> state = null;
     if (m_States.TryGetValue(typeof(TState), out state))
     {
         return (TState)state;
     }
     return null;
 }
Example #3
0
        public AFsm(T owner)
        {
            if (owner == null)
            {
                throw new GameFrameworkException("FSM owner is invalid.");
            }

            m_Owner = owner;
            m_States = new Dictionary<Type, AFsmState<T>>();
            m_Datas = new Dictionary<string, Variable>();

            m_CurrentStateTime = 0f;
            m_CurrentState = null;
            m_IsDestroyed = false;
        }
Example #4
0
        /// <summary>
        /// 开始有限状态机。
        /// </summary>
        /// <typeparam name="TState">要开始的有限状态机状态类型。</typeparam>
        public void Start<TState>() where TState : AFsmState<T>
        {
            if (IsRunning)
            {
                throw new GameFrameworkException("FSM is running, can not start again.");
            }

            AFsmState<T> state = GetState<TState>();
            if (state == null)
            {
                throw new GameFrameworkException(string.Format($"FSM can not start state {typeof(TState).FullName} which is not exist."));
            }

            m_CurrentStateTime = 0f;
            m_CurrentState = state;
            m_CurrentState.OnEnter(this);
        }
Example #5
0
        public void ChangeState(Type stateType)
        {
            if (m_CurrentState == null)
            {
                throw new GameFrameworkException("Current state is invalid.");
            }

            AFsmState<T> state = GetState(stateType);
            if (state == null)
            {
                throw new GameFrameworkException(string.Format("FSM '{0}' can not change state to '{1}' which is not exist.", state, stateType.FullName));
            }

            m_CurrentState.OnLeave(this);
            m_CurrentStateTime = 0f;
            m_CurrentState = state;
            m_CurrentState.OnEnter(this);
        }
Example #6
0
        /// <summary>
        /// 关闭并清理有限状态机。
        /// </summary>
        public virtual void ShutDown()
        {
            if (m_CurrentState != null)
            {
                m_CurrentState.OnLeave(this);
                m_CurrentState = null;
                m_CurrentStateTime = 0f;
            }

            foreach (var state in m_States.Values)
            {
                state.OnDestroy(this);
            }

            m_States.Clear();
            m_Datas.Clear();

            m_IsDestroyed = true;
        }
Example #7
0
        public AFsmState<T> GetState(Type stateType)
        {
            if (stateType == null)
            {
                throw new GameFrameworkException("State type is invalid.");
            }

            if (!typeof(AFsmState<T>).IsAssignableFrom(stateType))
            {
                throw new GameFrameworkException(string.Format("State type '{0}' is invalid.", stateType.FullName));
            }

            AFsmState<T> state = null;
            if (m_States.TryGetValue(stateType, out state))
            {
                return state;
            }

            return null;
        }