/// <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> /// Acts as a drawing delegate for DrawObjectWithTransform /// After performing the necessary transformation (translate/rotate) /// DrawObjectWithTransform will invoke this method /// </summary> /// <param name="o">The object to draw</param> /// <param name="e">The PaintEventArgs to access the graphics</param> private void ShipInfoDrawer(object o, PaintEventArgs e) { Ship p = o as Ship; int shipWidth = 35; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; using (Font font1 = new Font("Times New Roman", 16, FontStyle.Bold, GraphicsUnit.Pixel)) using (Pen pen = new Pen(Brushes.White, 2f)) using (SolidBrush rectangleFiller = new SolidBrush(Color.Green)) { if (p.GetHealth() == 0) { return; } PointF pointF1 = new PointF(-(shipWidth / 2) + 4, -(shipWidth / 2) - 20); e.Graphics.DrawString(p.GetName() + " " + p.GetScore(), font1, Brushes.White, pointF1); Rectangle rec = new Rectangle(-(shipWidth / 2) + 5, -(shipWidth / 2) + 40, 27, 7); e.Graphics.DrawRectangle(pen, rec); int length = p.GetHealth() * 5; Rectangle rec2 = new Rectangle(-(shipWidth / 2) + 6, -(shipWidth / 2) + 41, length, 5); e.Graphics.FillRectangle(rectangleFiller, rec2); } }
/// <summary> /// Spawns a ship using the properties of an existing ship. Used for respawning existing ships which have been killed. Spawn location is randomized. /// </summary> /// <param name="ship">ship to be respawned</param> public void RespawnShip(Ship ship) { Vector2D newLoc = new Vector2D(rand.Next(-worldSize / 2, worldSize / 2), rand.Next(-worldSize / 2, worldSize / 2)); Vector2D newOrient = new Vector2D(0, -1); Ship newShip = new Ship(ship.GetID(), newLoc, newOrient, false, ship.GetName(), ShipHealth, ship.GetScore()); // Set ship's modifiable variables newShip.setAccelRate(ShipAccel); newShip.SetRespawnDelay(ShipRespawnRate); newShip.SetFireDelay(FireDelay); newShip.setWorldSize(this.worldSize); this.addShip(newShip); }
/// <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); }
/// <summary> /// Draws a box in the right hand side of the screen that will display a players name, score, and hp. /// Count determines where on the screeen the box will be drawn. Each box is 50 pixels high, so the /// height of each box is 50 times count, where count begins at 0 for the first player /// </summary> /// <param name="ship">ship that contains the player information</param> /// <param name="e">The PaintEventArgs to access the graphics</param> /// <param name="count">tracks how many player boxes have been drawn</param> private void PlayerDrawer(PaintEventArgs e, Ship ship, int count) { int height = 50 * count; // determines how far down to draw each player box string name = ship.GetName() + ": " + ship.GetScore().ToString(); using (System.Drawing.SolidBrush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White)) using (System.Drawing.SolidBrush greenBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green)) using (System.Drawing.SolidBrush blackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black)) { e.Graphics.DrawString(name, new Font("Arial", 12), blackBrush, 10, 2 + height); // Rectangles are drawn starting from the top-left corner. // So if we want the rectangle centered on the player's location, we have to offset it // by half its size to the left (-width/2) and up (-height/2) Rectangle r = new Rectangle(5, (20 + height), 188, 20); Rectangle r2 = new Rectangle(7, (22 + height), 184, 16); Rectangle r3 = new Rectangle(9, (24 + height), 36 * ship.GetHp(), 12); e.Graphics.FillRectangle(blackBrush, r); e.Graphics.FillRectangle(whiteBrush, r2); e.Graphics.FillRectangle(greenBrush, r3); } }