/// <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);
                    }
        }
Beispiel #2
0
        /// <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()));
                }
            }
        }
Beispiel #3
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);
                    }
        }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
 public void UpdateShip(Ship s)
 {
     hp    = s.GetHp();
     score = s.GetScore();
 }