Пример #1
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()));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Acts as a drawing delegate for DrawObjectWithTransform
        /// After performing the necessary transformation (translate/rotate)
        /// DrawObjectWithTransform will invoke this method.
        /// Draws a ship with the images that are loaded in the LoadImages() method.
        /// </summary>
        /// <param name="o">The object to draw</param>
        /// <param name="e">The PaintEventArgs to access the graphics</param>
        private void ShipDrawer(object o, PaintEventArgs e)
        {
            Ship ship = o as Ship;

            int width  = 35;
            int height = 35;

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 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(-(width / 2), -(height / 2), width, height);
            int       shipID = ship.GetShipID() % 8;

            if (!ship.GetThrust())
            {
                e.Graphics.DrawImage(shipImages[shipID], r);
            }
            else
            {
                e.Graphics.DrawImage(shipImages[shipID + 8], r);
            }
        }