/// <summary> /// Sets new state based off input transition /// </summary> /// <param name="trans">Transition to be set to given state</param> public void SetTransition(FSMTransitions trans) { if (trans == FSMTransitions.none) { Debug.LogError("Null transition is not allowed"); return; } FSMStateID newID = currentState.CheckTransition(trans); if (newID == FSMStateID.none) { Debug.LogError("There is no state bound to " + trans.ToString()); return; } foreach (var state in FSMStates) { if (newID == state.StateID) { Debug.Log("State changed to " + newID.ToString()); currentState.OnStateExit(playerTransform, gameObject); currentState = state; currentState.OnStateEnter(playerTransform, gameObject); return; } } Debug.LogError("Unidentified error with state transition"); }
/// <summary> /// Checks if transition is avalable for use /// </summary> /// <param name="transition">Transition to be set</param> /// <returns>Returns transition to be transitioned to if legal, otherwise returns current state</returns> public FSMStateID CheckTransition(FSMTransitions transition) { if (transitions.ContainsKey(transition)) { return(transitions[transition]); } else { Debug.LogError("Transition " + transition.ToString() + " is not present within allowed transitions for this state"); return(stateID); } }
/// <summary> /// Modifies a transition of the state to transition to a different state /// </summary> /// <param name="transitionState">New state that is to be transitioned to when calling this transition</param> /// <param name="trans">previously added transition that is to be modified</param> /// <returns></returns> public bool EditTransitionState(FSMStateID transitionState, FSMTransitions trans) { if (transitions.ContainsKey(trans)) { transitions[trans] = transitionState; Debug.Log("Transition " + trans.ToString() + " of state " + stateID.ToString() + " has been modified to transition to stateID " + transitionState.ToString()); return(true); } else { Debug.LogError("Current state " + stateID.ToString() + " does not contain transition " + trans.ToString() + ". Did you mean to use AddTransitionState()"); return(false); } }
/// <summary> /// Removes transition state from legal transition states /// </summary> /// <param name="trans">Transition indented to be removed</param> public void RemoveTransitionState(FSMTransitions trans) { if (trans == FSMTransitions.none) { Debug.LogError("Null Transition not allowed"); return; } if (transitions.ContainsKey(trans)) { transitions.Remove(trans); Debug.Log("Transition " + trans.ToString() + " has been removed as a transitionable state of " + stateID.ToString()); return; } Debug.LogError(trans.ToString() + " transition is not present within the transitionable states of " + stateID.ToString()); }
/// <summary> /// Adds transition to given state after checking /// </summary> /// <param name="transitionState">State that is to be transitioned to with transition</param> /// <param name="trans">transition selected to transition to above state</param> public void AddTransitionState(FSMStateID transitionState, FSMTransitions trans) { if (transitionState == FSMStateID.none) { Debug.LogError("Null Transition not allowed"); return; } if (transitions.ContainsKey(trans)) { Debug.LogError(trans.ToString() + " transition already listed as Transitionable"); return; } transitions.Add(trans, transitionState); Debug.Log("Transition state " + transitionState.ToString() + " has been added as a transitionable state of " + stateID.ToString() + " using transition " + trans.ToString()); }