public TransitionArgs(State From, State To, StateMachine StateMachine) { this.From = From; this.To = To; this.StateMachine = StateMachine; }
/// <summary> /// No package private. Called by machine. /// </summary> /// <param name="name">Name.</param> /// <param name="state">State.</param> public Transition(string name, State state) { this.Name = name; this.state = state; }
/// <summary> /// Convenience constructor to give a name. /// </summary> /// <param name="state">State.</param> public Transition(State state) { this.Name = string.Format ("To {0}", state.Name); this.state = state; }
/// <summary> /// Remove the state and all transitions from upstream states. /// </summary> /// <returns>The state.</returns> /// <param name="state">State.</param> public StateMachine RemoveState(State state) { if (state == StartState) { throw new ArgumentException ("Cannot remove start state"); } else { states.Remove (state); foreach (Transition t in state.Transitions) { state.RemoveTransition (t); } return this; } }
/// <summary> /// Ask the machine to transition from the current state to the requested state. /// /// Raises a TransitionException if the requested state is not reachable. /// </summary> /// <param name="state">State.</param> public void Transition(State state) { Debug.Log (string.Format("Transitioning from {0} to {1}", CurrentState.Name, state.Name)); if (CurrentState != state) { foreach (Transition t in CurrentState.Transitions) { if (t.State == state) { currentState = state; // return OnTransition (currentState, state); OnTransition (currentState, state); // Throw the event if(Transitioned != null) Transitioned(this, new TransitionArgs(currentState,state,this)); return; } } throw new TransitionException (string.Format ("State {0} is not accessible from the current state {1}", state.Name, CurrentState.Name)); } }
/// <summary> /// Raises the transition event. Override this method in inheritors to write code /// that occurs on Transition. /// /// For non-inheritors, use the event instead. /// </summary> /// <param name="from">From.</param> /// <param name="to">To.</param> public abstract void OnTransition(State from, State to);
/// <summary> /// Add a Transition from one state to another. /// /// Used in constructing the graph. /// </summary> /// <returns>The transition.</returns> /// <param name="from">From.</param> /// <param name="to">To.</param> public Transition AddTransition(State from, State to) { if (!States.Contains (from) || !States.Contains (to)) throw new TransitionException ("One or both of the states does not exist"); Transition transition = new Transition (to); from.AddTransition (transition); transitions.Add (transition); return transition; }
/// <summary> /// Add a state with no transitions in or out. /// </summary> /// <returns>The state.</returns> /// <param name="name">Name.</param> public State AddState(string name) { State state = new State (name,this); states.Add (state); return state; }
public StateMachine() { states = new HashSet<State> (); transitions = new HashSet<Transition> (); start = AddState ("Start"); currentState = start; }
/// <summary> /// Factory method to add a transition from the current state to the given state. /// </summary> /// <returns>The transition.</returns> /// <param name="state">State.</param> public State AddTransition(State state) { machine.AddTransition (this, state); return state; }