Exemplo n.º 1
0
    private void processCarsWithLaneNumberAndRefernceIntersection(int laneNumber, Intersection refIntersection)
    {
        //TODO: sort list first instead of doing it on the fly
        AbstractCar[] sortedCars = cars
                                   .Where(c => c.position.referenceIntersection == refIntersection)
                                   .Where(c => c.position.laneNumber == laneNumber)
                                   .OrderByDescending(c => c.position.offset)
                                   .ToArray();
        for (int j = 0; j < sortedCars.Length; j++)
        {
            AbstractCar car = sortedCars[j];
            // check if this is the first car
            // TODO: Reduce code copying
            if (j == 0)
            {
                // move the first car into the intersection if necessary
                float stoppingOffset = trafficMath.stoppingDistanceForCurrentDrive(car.position);
                if (car.position.offset < stoppingOffset)
                {
                    car.SmartAdjustSpeedAccordingToStoppingDistance(stoppingOffset - car.position.offset, 2f);
                    car.position.offset += car.GetSpeed() * Time.deltaTime;
                    //we should not exceed the stopping distance
                    // If the car is close enough to the intersection
                    if (stoppingOffset - car.position.offset < 0.05f)
                    {
                        car.position.offset = stoppingOffset;
                        car.DecreaseSpeed(100f);                        // force stop the car

                        moveCarToIntersection(car);
                    }
                }

                car.updateCarPosition();
            }
            else
            {
                // let the car follow the previous car
                int   MIN_DIST       = 2;
                float stoppingOffset = sortedCars[j - 1].position.offset - MIN_DIST;
                if (car.position.offset < stoppingOffset)
                {
                    car.SmartAdjustSpeedAccordingToStoppingDistance(stoppingOffset - car.position.offset, 2f);
                    car.position.offset += car.GetSpeed() * Time.deltaTime;
                }

                car.updateCarPosition();
            }
        }
    }