Esempio n. 1
0
        public void LoadNpcCars(World world, IList<Car> list, int quantity, Texture2D[] textures)
        {
            list.Clear(); //Delete any existing cars in operation
            var rand = new Random();
            var laneNum = 0;

            for(var i = 0; i < quantity; i++)
            {
                var spawnAttempts = 0;
                var textureId = rand.Next(0, textures.Count()-1);

                var newCar = new Car(textures[textureId]);
                world.InitializeNpcCar(newCar, laneNum, ref rand);
                world.OptimizeSpawnPosition(this, list, newCar, laneNum, ref rand, ref spawnAttempts);

                //If the car did not hit its number of maximum spawn attempts, assume it safe-spawned and spawn it
                if(spawnAttempts <= World.MaxSpawnAttempts)
                {
                    list.Add(newCar);
                    laneNum++;
                    if (laneNum >= world.Lanes.Count())
                        laneNum = 0;
                }

            }
        }
Esempio n. 2
0
        /// <summary>
        /// Checks a given area against all NPCs in order to find a collision - event terminates on the first collision in case there's more than one.
        /// </summary>
        /// <param name="cars"></param>
        /// <param name="toCheck">the area to check for a collision</param>
        /// <param name="collided">a reference to the car that collided with the given rectangle</param>
        /// <returns>true or false</returns>
        public bool CheckNpcHardCollision(IList<Car> cars, Rectangle toCheck, out Car collided)
        {
            foreach (var car in cars.Where(car => car.HardCollisionBoundary.Intersects(toCheck) && car.IsColliding == false))
            {
                collided = car;
                return true;
            }

            collided = null;
            return false;
        }
Esempio n. 3
0
 public void InitializePcCar(Car car)
 {
     var laneNum = Lanes.Count() / 2;
     car.Center = new Vector2((Lanes[laneNum].LaneBox.X + (Lanes[laneNum].LaneBox.Width - car.Width) / 2f), Bounds.Height - car.Height);
     car.Velocity = new Vector2(0f, -10f);
 }
Esempio n. 4
0
        /// <summary>
        /// Recursively looks for an optimal place to spawn an NPC car where it won't collide with any other NPCs
        /// </summary>
        /// <param name="pcCar"></param>
        /// <param name="list"></param>
        /// <param name="car">The car we intend to spawn</param>
        /// <param name="laneNum">the lane on the freeway the car belongs to</param>
        /// <param name="rand">a random number generator we pass by reference</param>
        /// <param name="attemptCount">the number of attempts it has taken us to spawn this car</param>
        public void OptimizeSpawnPosition(PcCar pcCar, IList<Car> list, Car car, int laneNum, ref Random rand, ref int attemptCount)
        {
            Car collisionCar;

            if ((CheckNpcHardCollision(list, car.SoftCollisionBoundary, out collisionCar)
                    || pcCar.SoftCollisionBoundary.Intersects(car.HardCollisionBoundary)
                ) && attemptCount <= MaxSpawnAttempts)
            {

                /* All of this code needs to execute whenever an NPC car has:
                 *  1. Spawned on top of another NPC
                 *  2. Spawned on top of the PC
                 *  3. And the maxmimum number of spawn attempts has not been reached.
                */

                attemptCount++;
                InitializeNpcCar(car, laneNum, ref rand);
                OptimizeSpawnPosition(pcCar, list, car, laneNum, ref rand, ref attemptCount);
            }
        }
Esempio n. 5
0
 public void InitializeNpcCar(Car car, int laneNum, ref Random rand)
 {
     var yPos = rand.Next(0, Bounds.Height - car.Height);
     var velocity = (rand.Next(-45, 45) + 0.5f);
     car.Center = new Vector2((Lanes[laneNum].LaneBox.X + (Lanes[laneNum].LaneBox.Width - car.Width) / 2f), yPos);
     car.Velocity = new Vector2(0f, velocity);
 }