private XState GetState(txStateID state_id) { XState state; bool found = mStates.TryGetValue(state_id, out state); XUtils.Assert(found); return(state); }
public void RemoveTransition(txStateID from, Trigger trigger) { XUtils.Assert(!mLocked, "Modifying state machine while in use not allowed."); XState from_state; bool found_from = mStates.TryGetValue(from, out from_state); XUtils.Assert(found_from); from_state.Remove(trigger); }
// TODO: templatize this to improve what is passed in for trigger, cast it to int public void CreateTransition(txStateID from, txStateID to, Trigger trigger, TransitionCallback callback) { XUtils.Assert(!mLocked, "Modifying state machine while in use not allowed."); // make sure 'from' already exists // make sure 'from' state doesn't already have that trigger (Add() will do that) XState from_state, to_state; bool found_from = mStates.TryGetValue(from, out from_state); bool found_to = mStates.TryGetValue(to, out to_state); XUtils.Assert(found_from && found_to); from_state.Add(trigger, to_state, callback); }
public void RemoveTransitionsTo(txStateID state_id) { List <Trigger> removals = new List <Trigger>(); for (int i = 0; i < mTransitions.Count; ++i) { var transition_i = mTransitions.ElementAt(i); if (transition_i.Value.GetToStateID().Equals(state_id)) { removals.Add(transition_i.Key); } } foreach (Trigger removal in removals) { mTransitions.Remove(removal); } }
public void RemoveState(txStateID state_id, txStateID new_state_if_current) { XUtils.Assert(!mLocked, "Modifying state machine while in use not allowed."); // if that was the current state, prepare to switch states as indicated. // remove all transitions to that state from other states. // remove state. XState next_state = null; if (mCurrentState.mStateID.Equals(state_id)) { XUtils.Assert(!new_state_if_current.Equals(state_id)); if (new_state_if_current.Equals(txStateID.kNone)) { XUtils.Assert(mStates.Count == 1, "can only switch to no current state if removing the last state"); mCurrentState = null; } else { bool found_next = mStates.TryGetValue(new_state_if_current, out next_state); XUtils.Assert(found_next); } } for (int i = 0; i < mStates.Count; ++i) { var state_i = mStates.ElementAt(i); state_i.Value.RemoveTransitionsTo(state_id); } if (next_state != null) { mCurrentState = next_state; } XState state; bool found = mStates.TryGetValue(state_id, out state); XUtils.Assert(found); mStates.Remove(state_id); }
public void Init() { mCurrentGameTime = new GameTime(); mContactCount = eContactCount.Unknown; mMultiPoke_StartMaxSeparation = 0d; mMultiPoke_StartMaxSeparation = 0d; mStateMachine = new XStateMachine <eContactChange>(eContactChange.NoChange); txStateID start = mStateMachine.CreateState(State_Start); txStateID no_contacts = mStateMachine.CreateState(State_NoContacts); txStateID tracking_single_poke = mStateMachine.CreateState(State_SinglePoke); txStateID tracking_single_drag = mStateMachine.CreateState(State_SingleDrag); txStateID tracking_multi_poke = mStateMachine.CreateState(State_MultiPoke); txStateID tracking_multi_drag = mStateMachine.CreateState(State_MultiDrag); txStateID ignoring_contacts = mStateMachine.CreateState(State_IgnoringContacts); txStateID tracking_4_contacts = mStateMachine.CreateState(State_4Contacts); txStateID tracking_5_contacts = mStateMachine.CreateState(State_5Contacts); mStateMachine.CreateTransition(no_contacts, tracking_single_poke, eContactChange.ZeroToOne, Transition_NoContacts_SinglePoke); mStateMachine.CreateTransition(no_contacts, tracking_multi_poke, eContactChange.ZeroToTwo, Transition_NoContacts_MultiPoke); mStateMachine.CreateTransition(no_contacts, ignoring_contacts, eContactChange.ZeroToMany, Transition_Trivial); mStateMachine.CreateTransition(no_contacts, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(no_contacts, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_poke, tracking_single_drag, eContactChange.StillToMoving, Transition_SinglePoke_SingleDrag); mStateMachine.CreateTransition(tracking_single_poke, no_contacts, eContactChange.OneToZero, Transition_SinglePoke_NoContacts); mStateMachine.CreateTransition(tracking_single_poke, tracking_multi_poke, eContactChange.OneToTwo, Transition_SinglePoke_MultiPoke); mStateMachine.CreateTransition(tracking_single_poke, ignoring_contacts, eContactChange.OneToMany, Transition_SinglePoke_IgnoringContacts); mStateMachine.CreateTransition(tracking_single_poke, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_poke, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_drag, tracking_multi_drag, eContactChange.SingleDragToMultiDrag, Transition_SingleDrag_MultiDrag); mStateMachine.CreateTransition(tracking_single_drag, no_contacts, eContactChange.OneToZero, Transition_SingleDrag_NoContacts); mStateMachine.CreateTransition(tracking_single_drag, ignoring_contacts, eContactChange.OneToMany, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_drag, ignoring_contacts, eContactChange.SingleDragToMultiDragTooSlow, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_drag, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(tracking_single_drag, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_poke, no_contacts, eContactChange.TwoToZero, Transition_MultiPoke_NoContacts); mStateMachine.CreateTransition(tracking_multi_poke, tracking_multi_drag, eContactChange.StillToMoving, Transition_MultiPoke_MultiDrag); mStateMachine.CreateTransition(tracking_multi_poke, ignoring_contacts, eContactChange.TwoToOne, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_poke, ignoring_contacts, eContactChange.TwoToMany, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_poke, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_poke, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_drag, no_contacts, eContactChange.TwoToZero, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_drag, ignoring_contacts, eContactChange.TwoToOne, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_drag, ignoring_contacts, eContactChange.TwoToMany, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_drag, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(tracking_multi_drag, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(ignoring_contacts, no_contacts, eContactChange.OneToZero, Transition_Trivial); mStateMachine.CreateTransition(ignoring_contacts, no_contacts, eContactChange.TwoToZero, Transition_Trivial); mStateMachine.CreateTransition(ignoring_contacts, no_contacts, eContactChange.ManyToZero, Transition_Trivial); mStateMachine.CreateTransition(ignoring_contacts, tracking_4_contacts, eContactChange.AnyToFour, Transition_Trivial); mStateMachine.CreateTransition(ignoring_contacts, tracking_5_contacts, eContactChange.AnyToFive, Transition_Trivial); mStateMachine.CreateTransition(start, no_contacts, eContactChange.NoInitialContacts, Transition_Trivial); mStateMachine.CreateTransition(start, ignoring_contacts, eContactChange.InitialContacts, Transition_Trivial); mStateMachine.CreateTransition(tracking_4_contacts, no_contacts, eContactChange.FourToZero, Transition_4Contacts_Any); mStateMachine.CreateTransition(tracking_4_contacts, ignoring_contacts, eContactChange.FourToAnyPositive, Transition_4Contacts_Any); mStateMachine.CreateTransition(tracking_5_contacts, no_contacts, eContactChange.FiveToZero, Transition_5Contacts_Any); mStateMachine.CreateTransition(tracking_5_contacts, ignoring_contacts, eContactChange.FiveToAnyPositive, Transition_5Contacts_Any); mStateMachine.SetStartingState(start); }
public void SetStartingState(txStateID starting_state) { XUtils.Assert(mCurrentState == null); XUtils.Assert(!starting_state.Equals(txStateID.kNone)); mCurrentState = GetState(starting_state); }
public static void UnitTest() { GameTime t = new GameTime(); sUnitTest = new XStateMachineUnitTest(); var sm = new XStateMachine <eTriggers>(eTriggers.None); //sm.Log( "empty" ); txStateID s1 = sm.CreateState(null); sm.SetStartingState(s1); sm.Update(eTriggers.None); sm.CreateTransition(s1, s1, eTriggers.Jump, sUnitTest.Cb1); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); sm.CreateTransition(s1, s1, eTriggers.Poke, sUnitTest.Cb2); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); //Console.WriteLine( "test removal" ); sm.RemoveTransition(s1, eTriggers.Jump); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); // sm.RemoveTransition( s1, eTriggers.Jump ); // correctly asserts missing trigger sm.RemoveTransition(s1, eTriggers.Poke); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); sm.RemoveState(s1, txStateID.kNone); sm.Update(eTriggers.None); sm.ProcessTrigger(eTriggers.Poke); // sm.SetStartingState( txStateID.kNone ); // correctly asserts illegal starting state // sm.CreateTransition( s1, txStateID.kNone, sUnitTest.Cb2, eTriggers.Jump ); // asserts correctly s1 not found // sm.CreateTransition( txStateID.kNone, txStateID.kNone, sUnitTest.Cb2, eTriggers.Jump ); // asserts correctly kNone not found //Console.WriteLine( "testing non trivial transitions and static callbacks" ); txStateID s2 = sm.CreateState(Cb3); txStateID s3 = sm.CreateState(sUnitTest.Cb2State); sm.SetStartingState(s2); sm.CreateTransition(s2, s3, eTriggers.Jump, sUnitTest.Cb1); sm.CreateTransition(s3, s2, eTriggers.Poke, sUnitTest.Cb1); sm.Update(eTriggers.None); sm.ProcessTrigger(eTriggers.Jump); sm.Update(eTriggers.None); sm.ProcessTrigger(eTriggers.Poke); sm.Update(eTriggers.None); sm.ProcessTrigger(eTriggers.Jump); sm.ProcessTrigger(eTriggers.Poke); sm.Update(eTriggers.None); //sm.Log(); //Console.WriteLine( "testing non trivial remove state" ); // sm.RemoveState( s2, s2 ); // correctly asserts can't name same state sm.RemoveState(s2, s3); //sm.Log(); //Console.WriteLine( "testing non trivial remove state, not the current" ); txStateID s4 = sm.CreateState(sUnitTest.Cb2State); sm.CreateTransition(s3, s4, eTriggers.Poke, sUnitTest.Cb1); sm.CreateTransition(s4, s3, eTriggers.Poke, sUnitTest.Cb1); //sm.Log(); sm.RemoveState(s4, s3); //sm.Log(); }