/// <summary> /// This method tries to change the state the FSM is in based on /// the current state and the transition passed. If current state /// doesn't have a target state for the transition passed, /// an ERROR message is printed. /// </summary> public void PerformTransition(FsmTransitionId trans) { // Check for NullTransition before changing the current state if (trans == FsmTransitionId.NullTransition) { throw new Exception("FSM ERROR: NullTransition is not allowed for a real transition"); } // Check if the currentState has the transition passed as argument FsmStateId id = currentState.GetOutputState(trans); if (id == FsmStateId.NullStateID) { throw new Exception("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); } // Update the currentStateID and currentState currentStateID = id; foreach (FsmState state in states) { if (state.ID == currentStateID) { // Do the post processing of the state before setting the new one currentState.DoBeforeLeaving(); currentState = state; // Reset the state to its desired condition before it can reason or act currentState.DoBeforeEntering(); break; } } } // PerformTransition()
/// <summary> /// This method returns the new state the FSM should be if /// this state receives a transition and /// </summary> public FsmStateId GetOutputState(FsmTransitionId trans) { // Check if the map has this transition if (transitionMap.ContainsKey(trans)) { return(transitionMap[trans]); } return(FsmStateId.NullStateID); }
/// <summary> /// This method deletes a pair transition-state from this state's map. /// If the transition was not inside the state's map, an ERROR message is printed. /// </summary> public void DeleteTransition(FsmTransitionId trans) { // Check for NullTransition if (trans == FsmTransitionId.NullTransition) { throw new Exception("FSMState ERROR: NullTransition is not allowed"); } // Check if the pair is inside the map before deleting if (transitionMap.ContainsKey(trans)) { transitionMap.Remove(trans); return; } throw new Exception("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() + " was not on the state's transition list"); }
// regular add method public void AddTransition(FsmTransitionId trans, FsmStateId id) { // Check if anyone of the args is invalid if (trans == FsmTransitionId.NullTransition) { throw new Exception("FSMState ERROR: NullTransition is not allowed for a real transition"); } if (id == FsmStateId.NullStateID) { throw new Exception("FSMState ERROR: NullStateID is not allowed for a real ID"); } // Since this is a Deterministic FSM, // check if the current transition was already inside the map if (transitionMap.ContainsKey(trans)) { throw new Exception("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() + "Impossible to assign to another state"); } transitionMap.Add(trans, id); }
// helper for the "Reason" method public void PerformTransition(FsmTransitionId trans) { Debug.Log("going to" + trans.ToString()); machine.PerformTransition(trans); }
/// <summary> /// This method tries to change the state the FSM is in based on /// the current state and the transition passed. If current state /// doesn't have a target state for the transition passed, /// an ERROR message is printed. /// </summary> public void PerformTransition(FsmTransitionId trans) { // Check for NullTransition before changing the current state if (trans == FsmTransitionId.NullTransition) { throw new Exception("FSM ERROR: NullTransition is not allowed for a real transition"); } // Check if the currentState has the transition passed as argument FsmStateId id = currentState.GetOutputState(trans); if (id == FsmStateId.NullStateID) { throw new Exception("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); } // Update the currentStateID and currentState currentStateID = id; foreach (FsmState state in states) { if (state.ID == currentStateID) { // Do the post processing of the state before setting the new one currentState.DoBeforeLeaving(); currentState = state; // Reset the state to its desired condition before it can reason or act currentState.DoBeforeEntering(); break; } } }
// fluid interface public FsmState WithTransition(FsmTransitionId trans, FsmStateId id) { AddTransition(trans, id); return(this); }
// fluid interface public FsmState WithTransition(FsmTransitionId trans, FsmStateId id) { AddTransition(trans, id); return this; }
// helper for the "Reason" method public void PerformTransition(FsmTransitionId trans) { Debug.Log ("going to" + trans.ToString()); machine.PerformTransition(trans); }
/// <summary> /// This method returns the new state the FSM should be if /// this state receives a transition and /// </summary> public FsmStateId GetOutputState(FsmTransitionId trans) { // Check if the map has this transition if (transitionMap.ContainsKey(trans)) { return transitionMap[trans]; } return FsmStateId.NullStateID; }