コード例 #1
0
    void UpdateOncomingCar(int carId)
    {
        // We scan each lane
        for (int i = 0; i < trucksInLane.Length; i++)
        {
            List <GameObject> trucks = trucksInLane[i];
            bool foundCar            = false;
            bool carsBehind          = false;
            int  foundInLaneIndex    = -1;

            // For each of the trucks in the lane
            for (int j = 0; j < trucks.Count; j++)
            {
                CarController cc = trucks[j].GetComponent <CarController>();

                // If the car is found
                if (cc.carId == carId)
                {
                    // We remove it from the list
                    trucks.Remove(trucks[j]);
                    foundCar = true;

                    // Do the removed car have cars behind it?
                    carsBehind      |= trucks.Count > 0;
                    foundInLaneIndex = i;
                }
            }

            // If we removed a car and it had cars behind it
            if (foundCar && carsBehind)
            {
                // We update the removed car oncoming car, oncoming car :)
                int oncomingLaneId = (i + 2) % 4;

                // If there is an oncoming car in the lane opposite to the removed car
                if (trucksInLane[oncomingLaneId].Count > 0)
                {
                    GameObject    oncomingCar = trucksInLane[oncomingLaneId][0];
                    CarController occ         = oncomingCar.GetComponent <CarController>();
                    occ.SetOncomingCar(trucksInLane[foundInLaneIndex][0]);
                    occ.CarLog("I have a new opposite car!");
                }
            }
        }
    }