/// <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; }
private void RemoveCurrentState() { if (currentState != null) { currentState.OnStateEnded(); currentState = null; } }
void Awake() { this.notActive = new NotActiveState(); this.activePlayable = new ActivePlayableState(this.Facade); this.activeAI = new ActiveAIState(); this.SetStateNotActive(); }
public void StartState(IBehaviourState newState) { if (!CanStartEvent(newState)) { return; } newState.Start(); _currentStates.Add(newState); }
private void SetState(IBehaviourState state) { if (this.currentState != null) { this.currentState.Out(); } this.currentState = state; this.currentState.In(); }
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); }
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); }
/// <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); }