Ejemplo n.º 1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private StateMachineFeedbackEventArgs <TState, TTrigger> PerformTrigger(TTrigger trigger, object context)
        {
            var currentState = _currentState;
            var transition   = currentState.ValidateTransition(trigger);

            if (transition == null)
            {
                throw _logger.TransitionNotDefined(_codeBehind.GetType(), currentState.Value, trigger);
            }

            var eventArgs = new StateMachineFeedbackEventArgs <TState, TTrigger>(_currentState.Value, transition.DestinationStateValue, trigger, context);

            _currentState.Leave(eventArgs);

            try
            {
                transition.RaiseTransitioning(eventArgs);
            }
            catch
            {
                _currentState.Enter(eventArgs);
                throw;
            }

            _currentState = _states[transition.DestinationStateValue];
            _currentState.Enter(eventArgs);

            return(eventArgs);
        }
Ejemplo n.º 2
0
 public StateMachine(T context, MachineState <T> initialState)
 {
     _context = context;
     AddState(initialState);
     _currentState = initialState;
     _currentState.Enter();
 }
Ejemplo n.º 3
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.º 4
0
 public void ChangeState(MachineState newState)
 {
     currentState.Exit(this);
     currentState = newState;
     currentState.Enter(this);
 }