Beispiel #1
0
 public void AddState(State state)
 {
     if (null == m_statesDict)
         m_statesDict = new Dictionary<Type, State>();
     if(!m_statesDict.ContainsKey(state.GetType()))
         m_statesDict.Add(state.GetType(), state);
 }
Beispiel #2
0
        /// <summary>
        /// changes the current state
        /// </summary>
        public R ChangeState <R>() where R : State <T>
        {
            var newType = typeof(R);

            //			Don't change if we're in the new state already
            if (currentState != null && currentState.GetType() == newType)
            {
                return(currentState as R);
            }

            if (!ContainsState <R>())
            {
                UnityEngine.Debug.LogError("State " + newType.Name + " does not exist on " + context + ". Did you forget to add it by calling addState? Current state will remain active.");
                return(null);
            }

            // Exit the old state, if it exists.
            if (currentState != null)
            {
                ExitState();
            }


            // swap states and call begin
            previousState = currentState;
            currentState  = states[newType];
            EnterState();
            //		Debug.Log (DebugX.LogString(this, "Transitioned from "+previousState.ToString()+" to "+_currentState.ToString()));
            if (OnStateChanged != null)
            {
                OnStateChanged(previousState == null ? null : previousState.GetType(), currentState.GetType());
            }

            // Run the new state.
            currentState.UpdateTransitions();

            return(currentState as R);
        }
Beispiel #3
0
 /// <summary>
 /// Adds a state to the machine
 /// </summary>
 public void AddState(State <T> state)
 {
     UnityEngine.Debug.Assert(state != null, "New state is null");
     states[state.GetType()] = state;
     state.SetMachine(this);
 }