private void CollisionDetection(Ship s, Ship e, int index) { //check cannon balls for (int j = s.getCBA().Count - 1; j >= 0; j--) { CannonBall cB = s.getCBA().ElementAt(j); if (cB.BoundingBox.Intersects(e.BoundingBox)) { Console.WriteLine("\nCOLLISION CB "+ j + ": \n" + cB.BoundingBox.Print() + "\nAGAINST: \n" + e.BoundingBox.Print()+"\n"); //deal damage to enemy e.takeDamage(cB.getDamage()); //remove cannon ball //if the ship is not a player frigate, remove the cannon ball upon contact if (s.GetType() != typeof(Player_Frigate)) s.getCBA().Remove(cB); //if it is a player frigate, check to see if it's ability is activated, if not, then remove the cannon ball upon contact. Else, leave the cannon balls alone else { if (((Player)(s)).getShipState() != Ship.ShipState.AbilityActivated) s.getCBA().Remove(cB); } }//end if collision //check if the ship should be sunk }//end for j //if s is the player, then check if it is a brig and has its ability activated if (s == player) { if (player.GetType() == typeof(Player_Brig) && player.getShipState() == Ship.ShipState.AbilityActivated) if (s.BoundingBox.Intersects(e.BoundingBox)) { e.takeDamage(s.getDamage()); } } //check if the ship should be sunk if (e.getHealth() <= 0) { if (e.GetType() == typeof(Player_Brig) || e.GetType() == typeof(Player_Frigate) || e.GetType() == typeof(Player_ManOfWar)) { UnloadContent(); EndRun(); } } }
/// <summary> /// Collision detection between two ships. Checks the cannon ball array of the first ship against the second ship to see if the first ship has hit the second. If it has, damage is dealt, and this function checks to see if the second ship has lost all its health and if it has removes the ship. Works for both player vs enemy and enemy vs player and handles all special cases /// </summary> /// <param name="s">The ship dealing damage</param> /// <param name="e">The ship having damage dealt against it</param> /// <param name="index">Only needs to be used if the function is being called from a loop iterating through a list. it allows the use of RemoveAt() which is O(1) over Remove() which is O(n)</param> private void CollisionDetection(Ship s, Ship e, int index) { //check cannon balls for (int j = s.CannonBalls.Count - 1; j >= 0; j--) { CannonBall cB = s.CannonBalls.ElementAt(j); if (cB.BoundingBox.Intersects(e.BoundingBox)) { //deal damage to enemy e.takeDamage(cB.Damage); //remove cannon ball s.CannonBalls.Remove(cB); }//end if collision //check if the ship should be sunk }//end for j //if s is the player, then check if it is a brig and has its ability activated if (s == player) { if (player.GetType() == typeof(Player_Brig) && player.getShipState() == Player.ShipState.AbilityActivated) if (s.BoundingBox.Intersects(e.BoundingBox)) { e.takeDamage(s.Damage); } } //if the ship is a fireboat, check if it has collided with the other ship. If it has, then do damage and remove it from the list and return if (s.GetType() == typeof(FireBoat)) { if (s.BoundingBox.Intersects(e.BoundingBox)) { EnemyList.RemoveAt(index); return; } } //check if the ship should be sunk if (e.Health <= 0) { if (e.GetType() == typeof(Player_Brig) || e.GetType() == typeof(Player_Frigate) || e.GetType() == typeof(Player_ManOfWar)) { gameState = GameState.GameOver; } else { //add score score += (((Enemy)(e)).getScore() * scoreMultiplier); //drop multipliers int numberOfMultis = ((Enemy)(e)).getScore() / 5; for (int m = 0; m < numberOfMultis; m++) { Multiplier mp = new Multiplier(MULTIPLIER_DATA, e.Position, e.Position, (float)(randomGenerator.Next(360)) * (float)(Math.PI / 180), multiplierTexture, gameTimer.RawTime); playerInteractableList.Add(mp); } //determine if this ship will drop a powerup DropPowerup((Enemy)e); //remove from list EnemyList.RemoveAt(index); } } }