/// <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) { // For Mobile devices, this logic will close the Game when the Back button is pressed if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here KeyboardState keystate = Keyboard.GetState(); //GamePadState gamepadState = GamePad.GetState (PlayerIndex.One); float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; if (gameState == GameState.TitleScreen) { if (keystate.IsKeyDown(Keys.Space)) { StartNewGame(); gameState = GameState.Playing; } } else if (gameState == GameState.Playing) { player.Update(gameTime); LevelManager.Update(gameTime); if (player.Dead) { if (player.LivesRemaining > 0) { gameState = GameState.PlayerDead; deathTimer = 0.0f; } else { gameState = GameState.GameOver; deathTimer = 0.0f; } } } else if (gameState == GameState.PlayerDead) { player.Update(gameTime); LevelManager.Update(gameTime); deathTimer += elapsed; if (deathTimer > deathDelay) { player.WorldLocation = Vector2.Zero; LevelManager.ReloadLevel(); player.Revive(); gameState = GameState.Playing; } } else if (gameState == GameState.GameOver) { deathTimer += elapsed; if (deathTimer > deathDelay) { gameState = GameState.TitleScreen; } } base.Update(gameTime); }
/// <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) { // Allows the game to exit KeyboardState keyState = Keyboard.GetState(); GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; switch (gameState) { case GameState.TitleScreen: if (entro) { MediaPlayer.Stop(); MediaPlayer.Play(title); entro = false; } if (keyState.IsKeyDown(Keys.Enter) || gamepadState.Buttons.A == ButtonState.Pressed) { switch (menuOptions) { case MenuOptions.Play: StartNewGame(); gameState = GameState.Playing; MediaPlayer.Play(normal); break; case MenuOptions.Instructions: gameState = GameState.Help; helpIndex = 0; break; case MenuOptions.Exit: Exit(); break; } } else if (keyState.IsKeyDown(Keys.Down) && lastState.IsKeyUp(Keys.Down) || keyState.IsKeyDown(Keys.S) && lastState.IsKeyUp(Keys.S)) { if (menuOptions != MenuOptions.Exit) { ++menuOptions; } } else if (keyState.IsKeyDown(Keys.Up) && lastState.IsKeyUp(Keys.Up) || keyState.IsKeyDown(Keys.W) && lastState.IsKeyUp(Keys.W)) { if (menuOptions != MenuOptions.Play) { --menuOptions; } } break; case GameState.Help: if (keyState.IsKeyDown(Keys.D) && lastState.IsKeyUp(Keys.D) || keyState.IsKeyDown(Keys.Right) && lastState.IsKeyUp(Keys.Right)) { helpIndex++; if (helpIndex == 3) { gameState = GameState.TitleScreen; } } else if (keyState.IsKeyDown(Keys.A) && lastState.IsKeyUp(Keys.A) || keyState.IsKeyDown(Keys.Left) && lastState.IsKeyUp(Keys.Left)) { helpIndex--; if (helpIndex == -1) { gameState = GameState.TitleScreen; } } else if (keyState.IsKeyDown(Keys.Escape) && lastState.IsKeyUp(Keys.Escape)) { gameState = GameState.TitleScreen; } break; case GameState.Playing: player.Update(gameTime); LevelManager.Update(gameTime); sec += (float)gameTime.ElapsedGameTime.TotalSeconds; if (player.Dead) { MediaPlayer.Pause(); MediaPlayer.Play(rip); MediaPlayer.IsRepeating = false; if (player.LivesRemaining > 0) { gameState = GameState.PlayerDead; deathTimer = 0.0f; } else { gameState = GameState.GameOver; deathTimer = 0.0f; } } if (OnDrugs) { if (!entro && sec > 4.2f) { MediaPlayer.Pause(); MediaPlayer.Play(high); MediaPlayer.IsRepeating = true; entro = true; } if (!TileMap.OnDrugs) { MediaPlayer.Pause(); MediaPlayer.Play(gettingHi); sec = 0.0f; entro = false; MediaPlayer.IsRepeating = false; TileMap.OnDrugs = true; if (!TileMap.isRectanglePassable(player.CollisionRectangle)) { player.Kill(); } } } else { if (!entro && sec > 2.0f) { MediaPlayer.Pause(); MediaPlayer.Play(normal); MediaPlayer.IsRepeating = true; entro = true; } if (TileMap.OnDrugs) { MediaPlayer.Pause(); MediaPlayer.Play(gettingNormal); sec = 0.0f; entro = false; MediaPlayer.IsRepeating = false; TileMap.OnDrugs = false; if (!TileMap.isRectanglePassable(player.CollisionRectangle)) { player.GoSafe(); } } } if (keyState.IsKeyDown(Keys.Escape) && lastState.IsKeyUp(Keys.Escape)) { MediaPlayer.Volume = 0.3f; gameState = GameState.Paused; } break; case GameState.Paused: sec += (float)gameTime.ElapsedGameTime.TotalSeconds; if (OnDrugs && (!entro && sec > 4.2f)) { MediaPlayer.Pause(); MediaPlayer.Play(high); MediaPlayer.IsRepeating = true; entro = true; } else if (!OnDrugs && (!entro && sec > 2.0f)) { MediaPlayer.Pause(); MediaPlayer.Play(normal); MediaPlayer.IsRepeating = true; entro = true; } if (keyState.IsKeyDown(Keys.Escape) && lastState.IsKeyUp(Keys.Escape)) { MediaPlayer.Volume = 1.0f; gameState = GameState.Playing; } break; case GameState.PlayerDead: player.Update(gameTime); LevelManager.Update(gameTime); deathTimer += elapsed; if (deathTimer > deathDelay) { player.WorldLocation = Vector2.Zero; LevelManager.ReloadLevel(); player.Revive(); entro = false; gameState = GameState.Playing; } break; case GameState.GameOver: player.Update(gameTime); deathTimer += elapsed; if (deathTimer > deathDelay) { gameState = GameState.TitleScreen; player.WorldLocation = Vector2.Zero; } break; } lastState = keyState; screen.Update(gameTime); base.Update(gameTime); }