/// <summary> /// Called when an Alien is able to launch a bomb. Sets the position of the bomb according to the /// alien's position then adds it to the list of Projectiles. /// </summary> /// <param name="alien">The Alien that will launch the bomb.</param> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Launch(Rectangle alien, GameTime gameTime) { int xCoordinate = alien.X + (alien.Width / 2); int yCoordinate = alien.Bottom + 1; ProjectileSprite projectile = new ProjectileSprite(game, bombImage, false, 6F); projectile.Initialize(); projectile.SetPosition(xCoordinate, yCoordinate); bullets.Add(projectile); }
/// <summary> /// Checks if there was a collision between a bomb and a player. /// </summary> /// <param name="bomb">Bomb that was launched by an Alien to be checked for collisions.</param> protected override bool checkCollision(ProjectileSprite bomb) { bool collision = false; if (bomb.GetBoundary().Intersects(player.GetBoundary())) { onPlayerCollision(player, 0); collision = true; playerDeathSound.Play(); } return(collision); }
/// <summary> /// Called when a Player is able to launch a laser. Sets the position of the laser according to the /// Players's position then adds it to the list of Projectiles. Controls how rapidly a player /// can fire a laser through throttling. /// </summary> /// <param name="player">The border of the player object</param> /// <param name="gameTime">Provides a snapshot of timing values</param> public override void Launch(Rectangle player, GameTime gameTime) { int yCoordinate = player.Top - 1 - imageLaser.Height; int xCoordinate = player.X + (player.Width / 2 - 1); if (gameTime.TotalGameTime - previousLaunchTime > tolerance) { projectile = new ProjectileSprite(game, imageLaser, true, 8F); projectile.Initialize(); projectile.SetPosition(xCoordinate, yCoordinate); bullets.Add(projectile); previousLaunchTime = gameTime.TotalGameTime; laserSound.Play(); } }
/// <summary> /// Checks if there was a collision between a laser and an alien by iterating through the AlienSquad /// array. Returns True if a collision was detected, False if there was no collision. Also checks for /// a collision with the mothership. /// </summary> /// <param name="laser">Laser that was launched by a player to be checked for collisions.</param> /// <returns>A boolean representing if a collision was detected</returns> protected override bool checkCollision(ProjectileSprite laser) { bool collision = false; int pts = 0; for (int row = 0; row < alienSquad.getAlienRowCount(); row++) { for (int col = 0; col < alienSquad.getAlienColumnCount(); col++) { if (laser.GetBoundary().Intersects(alienSquad[row, col].GetBoundary())) { if (alienSquad[row, col].GetAlienState() == AlienState.ACTIVE) { // Only gives points if Alien dies, on higher difficulties multiple hits will not award points if (alienSquad[row, col].GetHitPoints() == 1) { pts = 10 + (alienSquad.getAlienRowCount() - 1 - row) * 10; } onAlienCollision(alienSquad[row, col], pts); collision = true; //remove laser after it hits alien laser.Dispose(); bullets.Remove(laser); return(collision); } } } } if (laser.GetBoundary().Intersects(mothership.GetBoundary())) { if (mothership.GetAlienState() == AlienState.ACTIVE) { onMothershipCollision(mothership, 100); collision = true; // remove laser after it hits alien laser.Dispose(); bullets.Remove(laser); return(collision); } } return(collision); }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { if (game.GetGameState() == Game1.GameState.Playing) { for (int ctr = 0; ctr < bullets.Count; ctr++) { bullets[ctr].Update(gameTime); ProjectileSprite bullet = bullets[ctr]; // if bullets encounter a collision with a sprite if (checkCollision(bullets[ctr])) { } // if bullets go past screen else if (bullets[ctr].GetPosition().Y > screenHeight || bullets[ctr].GetPosition().Y < 0) { bullets[ctr].Dispose(); bullets.Remove(bullets[ctr]); } } } base.Update(gameTime); }
/// <summary> /// Abstract checkCollision method that each derived class must override /// </summary> /// <param name="projectile">The projectile object</param> /// <returns>Whether a collision occured or not</returns> protected abstract bool checkCollision(ProjectileSprite projectile);