コード例 #1
0
    /**
     * This method simulates traffic spawning at the given in-game time.
     */
    public void SimulateAtTime(float inGameTime)
    {
        // Calculate estimated relative traffic volume by averaging the relative traffic volume throughout the hour
        float lbRelativeTrafficVolume = CalculateRelativeTrafficVolumeAtTime(Mathf.FloorToInt(inGameTime));
        float ubRelativeTrafficVolume = CalculateRelativeTrafficVolumeAtTime(Mathf.RoundToInt(inGameTime + 0.5f));
        float relativeTrafficVolume   = (lbRelativeTrafficVolume + ubRelativeTrafficVolume) / 2.0f;

        foreach (KeyValuePair <EntryRoad, List <Traffic> > trafficStartingAt in trafficsStartingAt)
        {
            if (inGameTime >= nextSpawnTimes[trafficStartingAt.Key])
            {
                Path path = nextSpawnTraffics[trafficStartingAt.Key].Value.GetRandomPath();

                LinkedListNode <Road> firstRoadNode = path.Roads.First;

                VehicleController vehicle = Instantiate(
                    vehicleFactory.GetRandomVehicle(),
                    firstRoadNode.Value.StartGrid.transform.position,
                    firstRoadNode.Value.transform.rotation);
                vehicle.Initialize(firstRoadNode);

                // Add simulation manager as the speed observer
                vehicle.AddObserver(SiteManager.Instance.simulationManager);

                // Increment spawn time and move traffic iterator
                nextSpawnTimes[trafficStartingAt.Key] += 1.0f
                                                         / (relativeTrafficVolume * trafficStartingAt.Value.Sum(i => i.TrafficVolume));
                nextSpawnTraffics[trafficStartingAt.Key] = nextSpawnTraffics[trafficStartingAt.Key].Next;
            }
        }
    }