/// <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++; } }
//******************** World methods for the ship ********************// /// <summary> /// Creates a new ship with the given name and id, then adds it to the /// world /// </summary> public void MakeNewShip(String name, int id) { if (kingIsOn && allShips.Count == 0) { name = "King " + name; // make a ship Ship s = new Ship(name, id, new Vector2D(0, 0), new Vector2D(0, -1), startingHitPoints, respawnDelay, projectileFiringDelay); // make ship king s.SetKing(true); // find a random location and a random direction Respawn(s); // add the ship to the world AddShip(s); } else { // make a ship Ship s = new Ship(name, id, new Vector2D(0, 0), new Vector2D(0, -1), startingHitPoints, respawnDelay, projectileFiringDelay); // find a random location and a random direction Respawn(s); // add the ship to the world AddShip(s); } }
/// <summary> /// The passed in ship has hit a star so the health points of the /// ship must be set to zero /// </summary> private void HitAStar(Ship ship) { // if king game mode is turned on if (kingIsOn) { if (ship.IsKing()) { // 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()); } } // kill the ship ship.SetHP(0); }