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); } } } }
private void StartSimulatingIntersection(Intersection intersection) { simulatedIntersections.Add(intersection); }