コード例 #1
0
        /// <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++;
            }
        }
コード例 #2
0
        /// <summary>
        /// Draws a ship object from the world.
        /// </summary>
        private void ShipDrawer(object o, PaintEventArgs e)
        {
            Ship s = o as Ship;

            if (s.GetHP() > 0)
            {
                Image image = DrawShip(s);

                Rectangle r = new Rectangle(-(shipRadius / 2), -(shipRadius / 2), shipRadius, shipRadius);
                e.Graphics.DrawImage(image, r);
            }
        }