Example #1
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();
        }