/// <summary> /// Creates a new stack within the StateMachine. /// </summary> /// <param name="stackName">The string key for the new stack.</param> /// <param name="rootState">The start state for the new stack.</param> public void CreateStack(String stackName, AbstractState rootState) { String s = stackName.ToLower(); if (stackDictionary.ContainsKey(s)) throw new ArgumentException("Stack '" + s + "' already exists in the state machine."); Stack<AbstractState> stack = new Stack<AbstractState>(); stackDictionary.Add(s, stack); this.PushState(s, rootState); }
/// <summary> /// Constructor. /// </summary> /// <param name="gameInstance">The instance for this game.</param> /// <param name="rootState">The root state to start the StateMachine on.</param> public StateMachine(AbstractState rootState) { currentStack = "main"; stackDictionary = new Dictionary<String,Stack<AbstractState>>(); this.CreateStack(currentStack, rootState); Game.GameProcessing += new EventHandler<EventArgs>(GameProcessing); Game.InputSystem.CommandStarted += new EventHandler<Input.InputSystem.CommandEventArgs>(InputSystem_CommandStarted); Game.InputSystem.CommandEnded += new EventHandler<Input.InputSystem.CommandEventArgs>(InputSystem_CommandEnded); Game.InputSystem.CommandTriggered += new EventHandler<Input.InputSystem.CommandEventArgs>(InputSystem_CommandTriggered); }
/// <summary> /// Invoked after a state is popped from the stack, after that state's StateEnded /// but before this state's next GameLoopTick. /// </summary> /// <param name="previousState"> /// The state most recently popped from the state machine. Can be used with /// inherited states to access data (i.e., a dialog box's stateful contents). /// </param> protected internal virtual void StateResumed(AbstractState previousState) { #if DEBUG Console.WriteLine(this.GetType().Name + ".StateResumed(" + previousState.GetType().Name + ")"); #endif }
/// <summary> /// Pushes a state onto the specified stack. /// </summary> /// <param name="stackName">The stack on which to place the state.</param> /// <param name="newState">The state to add to the specified stack.</param> public void PushState(String stackName, AbstractState newState) { String s = stackName.ToLower(); if (stackDictionary.ContainsKey(s) == false) throw new ArgumentException("Stack '" + s + "' does not exist in the state machine."); if (s != "main" || this.stackDictionary["main"].Count > 0) { newState.Parent = this.stackDictionary[s].Peek(); newState.Parent.StatePaused(); } this.stackDictionary[s].Push(newState); newState.StateMachine = this; newState.StateStarted(); }
/// <summary> /// Pushes a state onto the currently active stack (making it the new running state). /// </summary> /// <param name="newState">The state to add to the currently active stack.</param> public void PushState(AbstractState newState) { this.PushState(this.currentStack, newState); }