Example #1
0
 /// <summary>
 /// Change the state by reference to the State object.  Only works if the State is already registered.
 /// </summary>
 /// <param name="state"></param>
 public void ChangeState(State state)
 {
     if (invertedstates.ContainsKey(state)) {
         ChangeState(invertedstates[state]);
     }
 }
Example #2
0
 /// <summary>
 /// Add a State with a specific id.
 /// </summary>
 /// <param name="state">The State to add.</param>
 /// <param name="id">The id for the State.</param>
 /// <returns>The int id of the added State.</returns>
 public int AddState(State state, int id)
 {
     states.Add(id, state);
     invertedstates.Add(state, id);
     state.Machine = this;
     return id;
 }
Example #3
0
        /// <summary>
        /// Add a state and automatically grab functions by their name.
        /// </summary>
        /// <param name="names">The common name in all the functions, like "Walking" in "UpdateWalking" "EnterWalking" and "ExitWalking"</param>
        /// <returns>The id to refer to the state.</returns>
        public void AddState(params string[] names)
        {
            foreach (var name in names) {
                if (Entity == null) throw new ArgumentException("State Machine must be Added to an Entity first!");

                var state = new State();

                //Using reflection to find all the appropriate functions!
                MethodInfo mi;
                mi = Entity.GetType().GetMethod("Enter" + name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (mi != null) {
                    state.OnEnter = (Action)Delegate.CreateDelegate(typeof(Action), Entity, mi);
                }

                mi = Entity.GetType().GetMethod("Update" + name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (mi != null) {
                    state.OnUpdate = (Action)Delegate.CreateDelegate(typeof(Action), Entity, mi);
                }

                mi = Entity.GetType().GetMethod("Exit" + name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (mi != null) {
                    state.OnExit = (Action)Delegate.CreateDelegate(typeof(Action), Entity, mi);
                }

                //Using reflection to assign the id to the right property
                FieldInfo fi;
                fi = Entity.GetType().GetField("state" + name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                fi.SetValue(Entity, nextStateId);

                AddState(state);
            }
        }
Example #4
0
 /// <summary>
 /// Add a State.
 /// </summary>
 /// <param name="state">The State to add.</param>
 /// <returns>The int id of the added State.</returns>
 public int AddState(State state)
 {
     states.Add(nextStateId, state);
     invertedstates.Add(state, nextStateId);
     state.Id = nextStateId;
     state.Machine = this;
     nextStateId++;
     return nextStateId - 1;
 }