private void UpdatePlayerShots(GameTime gameTime) { // if we are allowed to fire, add a shot to the list if (InputManager.ControlState.Fire && gameTime.TotalGameTime.TotalMilliseconds - lastTime > 500) { // create a new shot over the ship PlayerShot ps = new PlayerShot(this.contentManager); ps.Position.X = (player.Position.X + player.Width / 2) - ps.Width / 2; ps.Position.Y = player.Position.Y - ps.Height; playerShots.Add(ps); lastTime = gameTime.TotalGameTime.TotalMilliseconds; AlienAttackGame.AudioManager.PlayCue(AudioManager.Cue.PlayerShot); } // enumerate the player shots on the screen for (int i = 0; i < playerShots.Count; i++) { PlayerShot playerShot = playerShots[i]; playerShot.Update(gameTime); // if it's off the top of the screen, remove it from the list if (playerShot.Position.Y + playerShot.Height < 0) { playerShots.RemoveAt(i); playerShot = null; } } }
public bool HandlePlayerShotCollision(PlayerShot playerShot) { for (int y = 0; y < EnemyRows; y++) { for (int x = 0; x < EnemyCols; x++) { // if a player shot hit an enemy, destroy the enemy if (enemies[y, x] != null && CheckCollision(playerShot, enemies[y, x])) { Explosion explosion = new Explosion(this.Content); explosion.Position = enemies[y, x].Position; explosions.Add(explosion); enemies[y, x] = null; return(true); } } } return(false); }
private void HandleCollisions(GameTime gameTime) { // see if a player shot hit an enemy for (int i = 0; i < playerShots.Count; i++) { PlayerShot playerShot = playerShots[i]; // check the shot and see if it it collided with an enemy if (playerShot != null && enemyGroup.HandlePlayerShotCollision(playerShots[i])) { // remove the shot, add the score playerShots.RemoveAt(i); score += 100; AlienAttackGame.AudioManager.PlayCue(AudioManager.Cue.Explosion); } } // see if an enemy shot hit the player if (player != null && enemyGroup.HandleEnemyShotCollision(player)) { // blow up the player playerExplosion = new Explosion(this.contentManager); playerExplosion.Position = player.Position; player = null; AlienAttackGame.AudioManager.PlayCue(AudioManager.Cue.Explosion); } // see if an enemy hit the player directly if (player != null && enemyGroup.HandleEnemyPlayerCollision(player)) { // blow up the player playerExplosion = new Explosion(this.contentManager); playerExplosion.Position = player.Position; player = null; AlienAttackGame.AudioManager.PlayCue(AudioManager.Cue.Explosion); loseGame = true; } // if the player explosion animation is running, update it if (playerExplosion != null) { // if this is the last frame if (playerExplosion.Update(gameTime) && !loseGame) { // remove it playerExplosion = null; // we lose if we have no lives left if (lives == 0) { loseGame = true; } else { // subract 1 life and reset the board lives--; enemyGroup.Reset(); playerShots.Clear(); player = new Player(this.contentManager); } } } }