/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); // Allows the game to exit if (currentGamePadState.Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.Escape)) this.Exit(); // State transitions switch (gameState) { case State.TITLE: // Capture button sequence if (currentKeyboardState.GetPressedKeys().Length > 0 || currentGamePadState.Buttons.A == ButtonState.Pressed || currentGamePadState.Buttons.B == ButtonState.Pressed || currentGamePadState.Buttons.X == ButtonState.Pressed || currentGamePadState.Buttons.Y == ButtonState.Pressed) { checkButtonSequence(currentKeyboardState, previousKeyboardState, currentGamePadState, previousGamePadState); } if (myStartScale >= 1) myStartScaleIncrement = -0.002f; else if (myStartScale <= 0.8) myStartScaleIncrement = 0.002f; myStartScale += myStartScaleIncrement; if (currentKeyboardState.IsKeyDown(Keys.Enter) || currentGamePadState.IsButtonDown(Buttons.Start)) { // Initialize all the levels gameState = State.RUNNING; initLevels(false); } break; case State.RUNNING: if (currentLevel != null && currentLevel.runningState != CompletionState.dead && (currentKeyboardState.IsKeyDown(Keys.P) || currentGamePadState.IsButtonDown(Buttons.Start))) gameState = State.PAUSE; else { // Check the level status if (currentLevel == null) break; if (currentLevel.runningState == CompletionState.complete) { currentLevel = currentLevel.nextLevel; if (currentLevel == null) // YOU WIN!!! Game Over this.Exit(); } // Begin loading the new level if (currentLevel.loadedState == LoadingState.uninitialized) currentLevel.LoadLevel(Content, PlayerIndex.One); // Update the current level else currentLevel.Update(gameTime); } break; case State.PAUSE: if (currentKeyboardState.IsKeyDown(Keys.R) || (currentGamePadState.IsButtonDown(Buttons.Start) && !previousGamePadState.IsButtonDown(Buttons.Start))) gameState = State.RUNNING; break; default: break; } // Save previous keyboard state previousKeyboardState = currentKeyboardState; previousGamePadState = currentGamePadState; base.Update(gameTime); }
/// <summary> /// Initializes all the levels on disk. Also loads the first level /// </summary> private void initLevels(bool isdeveloper) { currentLevel = null; if (isdeveloper) { currentLevel = new Level(GraphicsDevice, "dummyLevel"); currentLevel.loadingSignal += new ChangedEventHandler(startLoadingScreen); currentLevel.LoadLevel(Content, PlayerIndex.One); return; } // parse directory of levels int i = 1; Level previousLevel = null; while (i <= Directory.GetFiles(Content.RootDirectory + "//levels//", "level*.xnb").Length) { Level nextLevel; // Only increment previous level after the second loop if (i > 2) previousLevel = previousLevel.nextLevel; // Check for the next level string curLevelName = "level" + i; if (File.Exists(Content.RootDirectory + "//levels//" + curLevelName + ".xnb")) { nextLevel = new Level(GraphicsDevice, curLevelName); nextLevel.loadingSignal += new ChangedEventHandler(startLoadingScreen); if (currentLevel == null) currentLevel = previousLevel = nextLevel; else previousLevel.nextLevel = nextLevel; } else break; ++i; } if (currentLevel != null) currentLevel.LoadLevel(Content, PlayerIndex.One); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { currentLevel = new Level(GraphicsDevice, "invalid"); currentButtonSequence = 0; base.Initialize(); }