/// <summary> /// When a ship is hit by a projectile then remove the a health point remove /// the projectile form the world /// </summary> private void HitAProjectile(Ship ship, Projectile projectile) { if (kingIsOn) { // find the owner of the projectile int shipID = projectile.GetOwner(); if (allShips.TryGetValue(shipID, out Ship projectileShip)) { // if the projectile is from the king or the ship is the king then... if (ship.IsKing() || projectileShip.IsKing()) { // subtract a health point from the ship ship.SetHP(ship.GetHP() - 1); if (!ship.IsAlive()) { // increase the ship's score who shot the projectile by 1 projectileShip.SetScore(projectileShip.GetScore() + 1); } // register that the projectile hit a non-team member ship.ShotsHit++; } if (ship.IsKing() && !ship.IsAlive()) { // make sure that the ship that died is no longer king ship.SetKing(false); ship.SetName(ship.GetName().Substring(5)); // select a new king Ship newKingShip = RandomShip(); newKingShip.SetKing(true); newKingShip.SetName("King " + newKingShip.GetName()); } } // set the projectile to dead projectile.Alive(false); } else { // subtract a health point ship.SetHP(ship.GetHP() - 1); if (!ship.IsAlive()) { // find the owner of the projectile int shipID = projectile.GetOwner(); allShips.TryGetValue(shipID, out Ship projectileShip); // increase the ship's score who shot the projectile by 1 projectileShip.SetScore(projectileShip.GetScore() + 1); } // set the projectile to dead projectile.Alive(false); // register that the projectile hit a ship ship.ShotsHit++; } }
/// <summary> /// Detects whether or not a ship is hit by a projectile /// </summary> private void CollisionBetweenAStarAndProjectile(Star star, Projectile projectile) { if (WithinARadius(star.GetLocation(), projectile.GetLocation(), starSize)) { // the passed in projectile hit a star so we remove it from the world projectile.Alive(false); } }
/// <summary> /// When the projectile is off the screen then remove the projectile /// from the world and return true. Else, if it was not removed return /// false; /// </summary> private bool ProjectileOffScreen(Projectile projectile) { int borderCoordinate = universeSize / 2; // check to see if its on the edge of the world if (Math.Abs(projectile.GetLocation().GetX()) >= borderCoordinate) { projectile.Alive(false); return(true); } // check to see if its on the edge of the world else if (Math.Abs(projectile.GetLocation().GetY()) >= borderCoordinate) { projectile.Alive(false); return(true); } return(false); }