/// <summary> /// Add a new transition from 1 state to another /// </summary> /// <returns> public ITransitionCommand AddTransition(FiniteState fromState, ITransitionCommand component, FiniteState toState) { component.enabled = false; component.FSM = this; StateTransition st = new StateTransition(fromState,component); //Debug.Log("Adding new transition type " + component.GetType() + " from state " + fromState.StateName + " to state " + toState.StateName); if(!MapTransition(fromState,component,toState)){ Destroy(component); //always delete duplicate transition components //Debug.Log("fromState " + fromState.StateName + " already contains a transition type " + component.GetType().ToString()); return null; } return component; }
/// <summary> /// Map the transitiom from 'fromState' to 'toState'. /// </summary> /// <returns> /// false if there is a transition from 'fromState' matching 'transition', else true; /// </returns> bool MapTransition(FiniteState fromState, ITransitionCommand transition,FiniteState toState) { return MapTransition(fromState,transition,toState,true); }
/// <summary> /// Called by ITransitionCommand's when the transitional condition is met, triggers state change /// </summary> public void OnTransition(ITransitionCommand transition) { StateTransition st = new StateTransition(CurrentState,transition); ChangeState(StateTransitions[st]); }
/// <summary> /// Map the transitiom from 'fromState' to 'toState'. /// </summary> /// <returns> /// false if there is a transition from 'fromState' matching 'transition', else true; /// </returns> bool MapTransition(FiniteState fromState, ITransitionCommand transition,FiniteState toState,bool cache) { StateTransition st = new StateTransition(fromState,transition); if(StateTransitions.ContainsKey(st)){ return false; } AddState(fromState,cache); AddState(toState,cache); StateTransitions.Add(st,toState); TransitionMap[fromState].Add(transition); if(cache){ StateTransitionState sts = new StateTransitionState();//ScriptableObject.CreateInstance<StateTransitionState>() as StateTransitionState; sts.StartState = fromState; sts.EndState = toState; sts.Transition = transition; StateCache.Add(sts); } return true; }
public StateTransition(FiniteState currentState, ITransitionCommand component) { CurrentState = currentState; TransitionComponent = component; }