예제 #1
0
 /// <summary>
 /// This will call the IStateSwitcher.DestroyStateSwitching(); and will remove the current state.
 /// Also this will clean the history of all activated states.
 /// </summary>
 public void CleanStateHandler()
 {
     RemoveCurrentState();
     switcher.DestroyStateSwitching(User);
     previousActivatedStates.Clear();
     currentState = null;
 }
예제 #2
0
 private void RemoveCurrentState()
 {
     if (currentState != null)
     {
         currentState.OnStateEnded();
         currentState = null;
     }
 }
예제 #3
0
    void Awake()
    {
        this.notActive      = new NotActiveState();
        this.activePlayable = new ActivePlayableState(this.Facade);
        this.activeAI       = new ActiveAIState();

        this.SetStateNotActive();
    }
예제 #4
0
 public void StartState(IBehaviourState newState)
 {
     if (!CanStartEvent(newState))
     {
         return;
     }
     newState.Start();
     _currentStates.Add(newState);
 }
예제 #5
0
 private void SetState(IBehaviourState state)
 {
     if (this.currentState != null)
     {
         this.currentState.Out();
     }
     this.currentState = state;
     this.currentState.In();
 }
예제 #6
0
    private bool CanStartEvent(IBehaviourState newState)
    {
        var response = _currentStates.All(state => state.GetType() != newState.GetType());

        if (!response)
        {
            Debug.LogError($"Trying to Start {newState.GetType()} : This event already exist! Use FSM.Restart() method instead.");
        }
        return(response);
    }
예제 #7
0
    public void ChangeStateToFrom(IBehaviourState newState, IBehaviourState currentlyState)
    {
        if (!CanStartEvent(newState))
        {
            return;
        }

        var current = GetState(currentlyState.GetType());

        if (current != null)
        {
            current.Exit();
            _currentStates.Remove(current);
        }
        StartState(newState);
    }
예제 #8
0
        /// <summary>
        /// This method sets a new state for the BehaviourStateUser.
        /// The parameters given are info components which will be catched by the given state. This state can use them if it so desires.
        /// If the method returns 'true', the state has been set to the current state.
        /// It will always keep a reference to the state and reuse it when called again until the CleanStateHandler() is called.
        /// </summary>
        public bool SetState(Type stateType, params IStateInfoPart[] stateInfoParts)
        {
            ISelfEndingBehaviourState selfEndingState;
            IBehaviourState           previousState = currentState;

            stateType = switcher.GetConvertedState(stateType);

            if (!switcher.GetPermissionForState(stateType))
            {
                return(false);
            }

            if (!previousActivatedStates.ContainsKey(stateType))
            {
                previousActivatedStates.Add(stateType, (IBehaviourState)Activator.CreateInstance(stateType));
            }

            if (!previousActivatedStates[stateType].IsAbleToActivate(User))
            {
                return(false);
            }

            selfEndingState = previousActivatedStates[stateType] as ISelfEndingBehaviourState;

            if (selfEndingState != null)
            {
                selfEndingState.SetSelfEndingState(switcher.GetReturnState(stateType));
            }

            RemoveCurrentState();
            currentState = previousActivatedStates[stateType];
            currentState.OnStateStart(User, new BehaviourStateInfo(stateInfoParts));

            if (previousState != null && StateSwitchedEvent != null)
            {
                StateSwitchedEvent(User, previousState.GetType(), currentState.GetType());
            }

            return(true);
        }