/// <summary> /// Creates a new StateBasedGame. /// </summary> public StateBasedGame() { Graphics = new GraphicsDeviceManager(this); SwitchingStates = true; SwitchType = true; SwitchTimer = 1000; states = new Dictionary<int, BasicGameState>(); currentState = null; nextState = null; }
/// <summary> /// Add a new state to the container. /// if the satte is the first, which will be added to the /// list then this state becomes the current state. /// </summary> /// <param name="state">the state to add</param> public void AddState(BasicGameState state) { state.Init(this); if (currentState == null) { currentState = state; currentState.Enter(this); } states.Add(state.GetStateID(), state); }
/// <summary> /// Sets a State. Note that this is an instant change, no transitioning. /// </summary> /// <param name="id"></param> public void SetState(int id) { currentState = states[id]; }
/// <summary> /// Called for updates. /// </summary> /// <param name="gameTime"> the game time of the current context</param> protected override void Update(GameTime gameTime) { // if (SwitchingStates) { if (SwitchTimer <= 0) { if (!SwitchType) { currentState = nextState; currentState.Enter(this); SwitchType = true; SwitchTimer = 1000; } else SwitchingStates = false; } else SwitchTimer -= gameTime.ElapsedGameTime.Milliseconds; } else { currentState.Update(gameTime, this); } base.Update(gameTime); }
/// <summary> /// Enters a state. /// </summary> /// <param name="id"> the id of the state</param> public void EnterState(int id) { currentState.Exit(this); nextState = states[id]; SwitchingStates = true; SwitchType = false; SwitchTimer = 1000; }