/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } updateable = new List <iUpdateable>(); oldkeyboardState = keyboardState; keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.P) && !oldkeyboardState.IsKeyDown(Keys.P)) { NextLevel(); } if (keyboardState.IsKeyDown(Keys.Q) && !oldkeyboardState.IsKeyDown(Keys.Q)) { PreviousLevel(); } if (keyboardState.IsKeyDown(Keys.E) && !oldkeyboardState.IsKeyDown(Keys.E)) { player.curPosition = current_maze.endingPosition; } if (keyboardState.IsKeyDown(Keys.S) && !oldkeyboardState.IsKeyDown(Keys.S)) { player.curPosition = current_maze.startingPosition; } if (!win && !game_over) { if (player.Bounds.X > graphics.GraphicsDevice.Viewport.Width) { NextLevel(); return; } current_maze = levels[current_level]; // TODO: Add your update logic here updateable.Add(player); foreach (Spike spike in current_maze.spikes) { updateable.Add(spike); } foreach (Cell cell in current_maze.cells) { updateable.Add(cell); } foreach (iUpdateable obj in updateable) { obj.Update(gameTime); } foreach (Cell cell in current_maze.getNearCells(player)) { foreach (iCollidable collidable in cell.collidables) { Wall wall = collidable as Wall; Spike spike = collidable as Spike; if (wall != null) { if (player.Bounds.CollidesWith(wall.Bounds) && !wall.destroyed) { float delta; switch (player.state) { case State.East: delta = (player.Bounds.X + player.Bounds.Width) - wall.Bounds.X; player.curPosition.X = wall.Bounds.X - player.Bounds.Width - delta; break; case State.North: delta = (wall.Bounds.Y + wall.Bounds.Height) - player.Bounds.Y; player.curPosition.Y = wall.Bounds.Y + wall.Bounds.Height + delta; break; case State.West: delta = (wall.Bounds.X + wall.Bounds.Width) - player.Bounds.X; player.curPosition.X = wall.Bounds.X + wall.Bounds.Width + delta + 1; break; case State.South: delta = (player.Bounds.Y + player.Bounds.Height) - wall.Bounds.Y; player.curPosition.Y = wall.Bounds.Y - player.Bounds.Height - delta; break; } } if (player.bomb.Explosion.CollidesWith(wall.Bounds) && player.bomb.detonated && !wall.destroyed) { if (wall.isBombable) { wall.Bounds.X = -50; wall.Bounds.Y = -50; } } } if (spike != null) { if (player.Bounds.CollidesWith(spike.Bounds) && spike.on) { player.ouchSFX.Play(1, 0, 0); GameOverDeath(); } if (player.bomb.Explosion.CollidesWith(spike.Bounds) && player.bomb.detonated && spike.on) { spike.destroyed = true; } } } } foreach (Cell cell in current_maze.getNearCells(player.bomb)) { foreach (iCollidable collidable in cell.collidables) { Wall wall = collidable as Wall; Spike spike = collidable as Spike; if (wall != null) { if (player.bomb.Explosion.CollidesWith(wall.Bounds) && player.bomb.detonated && !wall.destroyed) { if (wall.isBombable) { wall.Bounds.X = -50; wall.Bounds.Y = -50; } } } if (spike != null) { if (player.bomb.Explosion.CollidesWith(spike.Bounds) && player.bomb.detonated && spike.on) { spike.destroyed = true; } } } } if (player.Bounds.CollidesWith(player.bomb.Explosion) && player.bomb.detonated) { player.ouchBombSFX.Play(1, 0, 0); GameOverDeath(); } score--; 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } updateable = new List <iUpdateable>(); if (viewState == ViewState.IDLE) { oldkeyboardState = keyboardState; keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.P) && !oldkeyboardState.IsKeyDown(Keys.P)) { NextLevel(); } if (keyboardState.IsKeyDown(Keys.Q) && !oldkeyboardState.IsKeyDown(Keys.Q)) { PreviousLevel(); } if (keyboardState.IsKeyDown(Keys.E) && !oldkeyboardState.IsKeyDown(Keys.E)) { player.curPosition = current_maze.endingPosition; } if (keyboardState.IsKeyDown(Keys.S) && !oldkeyboardState.IsKeyDown(Keys.S)) { player.curPosition = current_maze.startingPosition; } if (keyboardState.IsKeyDown(Keys.B) && !oldkeyboardState.IsKeyDown(Keys.B)) { if (current_level == 1) { player.curPosition = new Vector2(551, 451); } if (current_level == 2) { player.curPosition = new Vector2(951, 401); } } if (!win && !game_over) { if (player.Bounds.X > graphics.GraphicsDevice.Viewport.Width) { NextLevel(); return; } current_maze = levels[current_level]; // TODO: Add your update logic here updateable.Add(player); foreach (Spike spike in current_maze.spikes) { updateable.Add(spike); } foreach (Cell cell in current_maze.cells) { updateable.Add(cell); } foreach (iUpdateable obj in updateable) { obj.Update(gameTime); } foreach (Cell cell in current_maze.getNearCells(player)) { foreach (iCollidable collidable in cell.collidables) { if (collidable is Wall wall) { if (player.Bounds.CollidesWith(wall.Bounds) && !wall.destroyed) { float delta; switch (player.moving_state) { case MovingState.East: delta = (player.Bounds.X + player.Bounds.Width) - wall.Bounds.X; player.curPosition.X = wall.Bounds.X - player.Bounds.Width - delta; break; case MovingState.North: delta = (wall.Bounds.Y + wall.Bounds.Height) - player.Bounds.Y; player.curPosition.Y = wall.Bounds.Y + wall.Bounds.Height + delta; break; case MovingState.West: delta = (wall.Bounds.X + wall.Bounds.Width) - player.Bounds.X; player.curPosition.X = wall.Bounds.X + wall.Bounds.Width + delta + 1; break; case MovingState.South: delta = (player.Bounds.Y + player.Bounds.Height) - wall.Bounds.Y; player.curPosition.Y = wall.Bounds.Y - player.Bounds.Height - delta; break; } } if (player.bomb.Explosion.CollidesWith(wall.Bounds) && player.bomb.detonated && !wall.destroyed) { if (wall.isBombable) { // Wall crumble apart particle system implemented here wallParticleSystem = newWallParticleSystem(graphics.GraphicsDevice, 100, particle, wall.Position()); wall.Bounds.X = -50; wall.Bounds.Y = -50; score += 5000; } } } if (collidable is Spike spike) { if (player.Bounds.CollidesWith(spike.Bounds) && spike.on) { player.ouchSFX.Play(1, 0, 0); GameOverDeath(); } if (player.bomb.Explosion.CollidesWith(spike.Bounds) && player.bomb.detonated && spike.on) { spike.destroyed = true; score += 2000; } } if (collidable is Exit exit) { if (player.Bounds.CollidesWith(exit.Bounds)) { NextLevelExit(); // Change to Transition Slide return; } } } } foreach (Cell cell in current_maze.getNearCells(player.bomb)) { foreach (iCollidable collidable in cell.collidables) { Wall wall = collidable as Wall; Spike spike = collidable as Spike; if (wall != null) { if (player.bomb.Explosion.CollidesWith(wall.Bounds) && player.bomb.detonated && !wall.destroyed) { if (wall.isBombable) { // Wall crumble apart particle system implemented here wallParticleSystem = newWallParticleSystem(graphics.GraphicsDevice, 100, particle, wall.Position()); wall.Bounds.X = -50; wall.Bounds.Y = -50; score += 5000; } } } if (spike != null) { if (player.bomb.Explosion.CollidesWith(spike.Bounds) && player.bomb.detonated && spike.on) { spike.destroyed = true; score += 2000; } } } } if (player.Bounds.CollidesWith(player.bomb.Explosion) && player.bomb.detonated) { player.ouchBombSFX.Play(1, 0, 0); GameOverDeath(); } score--; } wallTimer += gameTime.ElapsedGameTime; if (wallTimer.TotalMilliseconds > wallTimeLimit) { wallParticleSystem = newWallParticleSystem(graphics.GraphicsDevice, 1, particle, Vector2.Zero); } wallParticleSystem.Update(gameTime); } else if (viewState == ViewState.TRANSITION_RIGHT) { } base.Update(gameTime); }