/// <summary> /// Updates the the ships dictionary to contain the given ship /// </summary> /// <param name="ship"></param> public void Update(Ship ship) { int id = ship.GetShipID(); if (ship.GetHp() == 0) { Ships.Remove(ship.GetShipID()); if (!Explosions.ContainsKey(ship.GetShipID())) { Explosions.Add(id, new Explosion(id, ship.GetLocation().GetX(), ship.GetLocation().GetY())); } foreach (Ship s in sortedPlayerList) { if (s.GetShipID() == ship.GetShipID()) { s.UpdateShip(ship); } } } else { int oldScore = -1; if (Explosions.ContainsKey(id) && Explosions[id].Dead) { Explosions.Remove(id); } bool found = false; foreach (Ship s in sortedPlayerList) { if (s.GetShipID() == ship.GetShipID()) { found = true; oldScore = s.GetScore(); s.UpdateShip(ship); break; } } Ships[id] = ship; if (!found) { sortedPlayerList.Add(Ships[ship.GetShipID()]); sortedPlayerList.Sort((a, b) => b.GetScore().CompareTo(a.GetScore())); } if (found && (ship.GetScore() > oldScore)) { sortedPlayerList.Sort((a, b) => b.GetScore().CompareTo(a.GetScore())); } } }
/// <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); } }
public void UpdateShip(Ship s) { hp = s.GetHp(); score = s.GetScore(); }