Esempio n. 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++;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// If Ship s does not exist in ships adds it. If the id already exists
        /// in ships, updates reference in ships to s.
        /// </summary>
        public void AddShip(Ship ship)
        {
            // if the ship is null then do nothing
            if (ship == null)
            {
                return;
            }

            // if the ship is dead then remove it from the world
            if (!ship.IsAlive())
            {
                aliveShips.Remove(ship.GetID());
            }
            // if the ship is in the world then replace the old ship with the passed in ship
            else if (aliveShips.ContainsKey(ship.GetID()))
            {
                aliveShips[ship.GetID()] = ship;
            }
            // if the ship is not in the world then add it
            else
            {
                aliveShips.Add(ship.GetID(), ship);
            }

            // we have taken care of the alive ships, now lets keep track of all ships
            AddAllShips(ship);
        }
Esempio n. 3
0
 /// <summary>
 /// Detects whether or not a ship is hit by a projectile
 /// </summary>
 private void CollisionWithAProjectile(Ship ship, Projectile projectile)
 {
     if (ship.GetID() != projectile.GetOwner() && ship.IsAlive())
     {
         if (WithinARadius(ship.GetLocation(), projectile.GetLocation(), shipSize))
         {
             // the passed in ship was hit by a projectile so we update health and remove
             // the projectile from the world
             HitAProjectile(ship, projectile);
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Computes the acceleration, velocity, and position of the passed in ship
        /// </summary>
        public void MotionForShips(Ship ship)
        {
            // if the ship isn't alive, just skip it
            if (!ship.IsAlive())
            {
                return;
            }

            // handle left turn command
            if (ship.TurnLeft)
            {
                ship.GetDirection().Rotate(-turningRate);
                ship.TurnLeft = false;
            }
            // handle right turn command
            else if (ship.TurnRight)
            {
                ship.GetDirection().Rotate(turningRate);
                ship.TurnRight = false;
            }

            // spawn a projectile if projectile command has been given
            if (ship.FireProjectile)
            {
                Projectile p = new Projectile(ship.GetID(), projectileID++, ship.GetLocation(), ship.GetDirection());
                AddProjectile(p);
                ship.FireProjectile = false;
                ship.ResetFireTimer();

                // we just fired a projectile, increment total number of fired projectiles
                ship.ShotsFired++;
            }

            //get a zero vector
            Vector2D acceleration = new Vector2D(0.0, 0.0);

            //compute the acceleration caused by the star
            foreach (Star star in stars.Values)
            {
                Vector2D g = star.GetLocation() - ship.GetLocation();
                g.Normalize();
                acceleration = acceleration + g * star.GetMass();
            }

            if (ship.HasThrust())
            {
                //compute the acceleration due to thrust
                Vector2D t = new Vector2D(ship.GetDirection());
                t            = t * engineStrength;
                acceleration = acceleration + t;
            }

            // recalculate velocity and location
            ship.SetVelocity(ship.GetVelocity() + acceleration);
            ship.SetLocation(ship.GetVelocity() + ship.GetLocation());
            // check to see if ship is off screen
            Wraparound(ship);

            // check for collisions with any star
            foreach (Star star in stars.Values)
            {
                CollisionWithAStar(ship, star);
            }

            // check for collisions with any projectiles
            foreach (Projectile proj in projectiles.Values)
            {
                CollisionWithAProjectile(ship, proj);
            }

            ship.IncrementFireTimer();
        }