Ejemplo n.º 1
0
    void Update()
    {
        numberOfVehicles = Mathf.Max(numberOfVehicles, 0);
        numberOfVehicles = Mathf.Min(numberOfVehicles, 200);

        // Check to make sure the number of desired vehicles hasn't changed
        if (numberOfVehicles != numCars)
        {
            // if it has, then redraw all the cars
            numCars = numberOfVehicles;
            UpdateGrid(grid);
        }
        else
        {
            for (int i = 0; i < vehicles.Length; i++)
            {
                if (vehicles[i].arrived)
                {
                    // If a car has arrived at its destination, update its destination
                    int oldI = vehicles[i].currI;
                    int oldJ = vehicles[i].currJ;
                    int oldQ = vehicles[i].currQ;
                    if (hasCar.ContainsKey(new int[] { oldI, oldJ, oldQ }))
                    {
                        hasCar[new int[] { oldI, oldJ, oldQ }] = false;
                    }
                    else
                    {
                        hasCar.Add(new int[] { oldI, oldJ, oldQ }, false);
                    }

                    int   currI      = vehicles[i].destI;
                    int   currJ      = vehicles[i].destJ;
                    int   currQ      = vehicles[i].destQ;
                    int[] nextTileIJ = GetNextIJ(currI, currJ, currQ);
                    int   nextI      = nextTileIJ[0];
                    int   nextJ      = nextTileIJ[1];
                    int   currTile   = grid[currI, currJ];
                    int   nextTile   = grid[nextI, nextJ];
                    int   nextQ      = connections[new int[] { currTile, nextTile, currQ }];
                    if (nextQ > 9)
                    {
                        // Pick one of the two digits if the queue is represented as a two digit number (aka there are multiple choices for queues)
                        int option1 = nextQ % 10;             // ones digit
                        int option2 = (nextQ - option1) / 10; // tens digit
                        if (Random.value < 0.5f)
                        {
                            nextQ = option1;
                        }
                        else
                        {
                            nextQ = option2;
                        }
                    }
                    float[] nextPos = tilePos[new int[] { nextTile, nextQ }];
                    vehicles[i].UpdateVelocity(nextPos, nextI, nextJ, nextQ, nextTile);


                    if (hasCar.ContainsKey(new int[] { currI, currJ, currQ }))
                    {
                        hasCar[new int[] { currI, currJ, currQ }] = true;
                    }
                    else
                    {
                        hasCar.Add(new int[] { currI, currJ, currQ }, true);
                    }

                    if (intersections.Contains(new Intersection(nextI, nextJ, nextTile)))
                    {
                        // If we're about to go into an intersection, enter that intersection's queue
                        Intersection currIntersection = intersections.Find(intersection => (intersection.i == nextI && intersection.j == nextJ));
                        currIntersection.AddCar(vehicles[i]);
                    }
                }
                else
                {
                    // If it hasn't arrived yet, keep driving
                    //vehicles[i].Drive();
                    if (!hasCar.ContainsKey(new int[] { vehicles[i].destI, vehicles[i].destJ, vehicles[i].destQ }) ||
                        !hasCar[new int[] { vehicles[i].destI, vehicles[i].destJ, vehicles[i].destQ }])
                    {
                        // But only if the destination is free of cars
                        vehicles[i].Drive(false);
                    }
                    else
                    {
                        // if there are cars in front, can drive, but just slow down
                        vehicles[i].Drive(true);
                    }
                }
            }

            // Update all the intersections and their vehicle queues
            foreach (Intersection intersection in intersections)
            {
                intersection.MaintainTraffic();
            }
        }
    }