コード例 #1
0
        private void AddCar(Street street, int lane, float positionAlongStreet, bool directionTowardsEnd)
        {
            Vector3 positionStreet;
            Vector3 directionStreet;

            float sidePosition = City.sidewalkWidth + (lane - 0.5f) * City.laneWidth;

            if (directionTowardsEnd)
            {
                positionStreet  = new Vector3(sidePosition, 0, positionAlongStreet);
                directionStreet = Vector3.forward;
            }
            else
            {
                positionStreet  = new Vector3(street.width - sidePosition, 0, street.length - positionAlongStreet);
                directionStreet = Vector3.back;
            }

            Vector3    position  = street.transform.TransformPoint(positionStreet);
            Vector3    direction = street.transform.TransformDirection(directionStreet);
            Quaternion rotation  = Quaternion.LookRotation(direction, Vector3.up);

            GameObject car = Instantiate(trafficCarPrefab, position, rotation, carTrafficRoot);

            CarTracker carTracker = car.GetComponent <CarTracker>();

            carTracker.city = city;
            trafficCarTrackers.Add(carTracker);

            CarAudio carAudio = car.GetComponent <CarAudio>();

            carAudio.gameMode = gameMode;

            float initialSpeed = speedPerLaneMph[lane] * PhysicsHelper.milesPerHourToMetersPerSecond;

            car.GetComponent <Car>().InitializeSpeed(initialSpeed);
        }
コード例 #2
0
        private void Update()
        {
            // See if game camera is in the same location as before.
            CarTracker focusedCarTracker = gameMode.currentCamera.trackedCar;

            if (focusedCarTracker.street != null && focusedCarTracker.street != focusedStreet || focusedCarTracker.intersection != null && focusedCarTracker.intersection != focusedIntersection)
            {
                // We have moved to a different street or intersection so we need to recalculate the simulated streets and intersections.
                focusedIntersection = focusedCarTracker.intersection;
                focusedStreet       = focusedCarTracker.street;

                void AddIntersectionVicinity(Intersection intersection, int distance)
                {
                    if (distance == 0)
                    {
                        return;
                    }

                    newSimulatedIntersections.Add(intersection);
                    if (intersection.northStreet != null)
                    {
                        AddStreetVicinity(intersection.northStreet, distance);
                    }
                    if (intersection.southStreet != null)
                    {
                        AddStreetVicinity(intersection.southStreet, distance);
                    }
                    if (intersection.eastStreet != null)
                    {
                        AddStreetVicinity(intersection.eastStreet, distance);
                    }
                    if (intersection.westStreet != null)
                    {
                        AddStreetVicinity(intersection.westStreet, distance);
                    }
                }

                void AddStreetVicinity(Street street, int distance)
                {
                    if (distance == 0)
                    {
                        return;
                    }

                    newSimulatedStreets.Add(street);
                    AddIntersectionVicinity(street.startIntersection, distance - 1);
                    AddIntersectionVicinity(street.endIntersection, distance - 1);
                }

                if (focusedStreet != null)
                {
                    AddStreetVicinity(focusedStreet, 3);
                }
                else
                {
                    AddIntersectionVicinity(focusedIntersection, 2);
                }

                // Update simulated streets.
                foreach (Street street in simulatedStreets)
                {
                    if (!newSimulatedStreets.Contains(street))
                    {
                        // This street is no longer being simulated, so we can remove it, including all the cars left in it.
                        streetsToBeStopped.Add(street);
                    }
                }

                foreach (Street street in streetsToBeStopped)
                {
                    StopSimulatingStreet(street);
                }

                streetsToBeStopped.Clear();

                foreach (Street street in newSimulatedStreets)
                {
                    if (!simulatedStreets.Contains(street))
                    {
                        // This street isn't being simulated, so start doing it.
                        StartSimulatingStreet(street);
                    }
                }

                newSimulatedStreets.Clear();

                // Update simulated intersections.
                foreach (Intersection intersection in simulatedIntersections)
                {
                    if (!newSimulatedIntersections.Contains(intersection))
                    {
                        // This street is no longer being simulated, so we can remove it, including all the cars left in it.
                        intersectionsToBeStopped.Add(intersection);
                    }
                }

                foreach (Intersection intersection in intersectionsToBeStopped)
                {
                    StopSimulatingIntersection(intersection);
                }

                intersectionsToBeStopped.Clear();

                foreach (Intersection intersection in newSimulatedIntersections)
                {
                    if (!simulatedIntersections.Contains(intersection))
                    {
                        // This street isn't being simulated, so start doing it.
                        StartSimulatingIntersection(intersection);
                    }
                }

                newSimulatedIntersections.Clear();
            }

            // Remove cars that left the simulation area.
            foreach (CarTracker carTracker in trafficCarTrackers)
            {
                if (carTracker.street != null && !simulatedStreets.Contains(carTracker.street) ||
                    carTracker.intersection != null && !simulatedIntersections.Contains(carTracker.intersection))
                {
                    Destroy(carTracker.gameObject);
                    carTrackersToBeRemoved.Add(carTracker);
                }
            }

            foreach (CarTracker carTracker in carTrackersToBeRemoved)
            {
                trafficCarTrackers.Remove(carTracker);
            }

            carTrackersToBeRemoved.Clear();

            // Add cars at the edges of the simulation areas.
            foreach (Street street in simulatedStreets)
            {
                if (street.isOneWay)
                {
                    if (street.oneWayDirectionGoesToStart)
                    {
                        if (!simulatedIntersections.Contains(street.endIntersection))
                        {
                            SimulateStreetEntrance(street, false);
                        }
                    }
                    else
                    {
                        if (!simulatedIntersections.Contains(street.startIntersection))
                        {
                            SimulateStreetEntrance(street, true);
                        }
                    }
                }
                else
                {
                    if (!simulatedIntersections.Contains(street.endIntersection))
                    {
                        SimulateStreetEntrance(street, false);
                    }

                    if (!simulatedIntersections.Contains(street.startIntersection))
                    {
                        SimulateStreetEntrance(street, true);
                    }
                }
            }
        }