Ejemplo n.º 1
0
    public R ChangeState <R>() where R : MachineState <T>
    {
        // avoid changing to the same state
        var newType = typeof(R);

        if (_currentState.GetType() == newType)
        {
            return(_currentState as R);
        }

        // only call end if we have a currentState
        if (_currentState != null)
        {
            _currentState.Exit();
        }
#if UNITY_EDITOR
        // do a sanity check while in the editor to ensure we have the given state in our state list
        if (!_states.ContainsKey(newType))
        {
            var error = GetType() + ": state " + newType +
                        " does not exist. Did you forget to add it by calling addState?";
            Debug.LogError(error);
            throw new Exception(error);
        }
#endif

        // swap states and call begin
        PreviousState = _currentState;
        _currentState = _states[newType];
        _currentState.Enter();
        ElapsedTimeInState = 0f;

        // fire the changed event if we have a listener
        if (OnStateChanged != null)
        {
            OnStateChanged();
        }
        return(_currentState as R);
    }
Ejemplo n.º 2
0
 public void AddState(MachineState <T> state)
 {
     state.setMachineAndContext(this, _context);
     _states[state.GetType()] = state;
 }