Exemplo n.º 1
0
    /// <summary>
    /// Spawns a Flash Pedestrian given its profile and its routing objects.
    /// </summary>
    /// <param name="profile">Profile object that will be used by the pedestrian.</param>
    /// <param name="routing">Routing object that will be used by the pedestrian.</param>
    /// <param name="routing">Index of the destination point within the destination.</param>
    private void SpawnPedestrian(Vector3 spawningPoint, FlashPedestriansProfile profile,
                                 FlashPedestriansDestination destination, Itinerary itinerary, int destIndex = 0)
    {
        GameObject newAgent;

        spawningPoint += new Vector3(Random.Range(-1.0f, 1.0f), 0.0f, Random.Range(-1.0f, 1.0f));

        if (pedestrianCache.Count > 0)
        {
            newAgent = (GameObject)pedestrianCache.Dequeue();
            newAgent.transform.position = spawningPoint;
        }
        else
        {
            //Random.Range(0, pedGlobalParameters.pedestrianObject.Length)
            newAgent = Instantiate(pedGlobalParameters.pedestrianObject,
                                   spawningPoint, Quaternion.identity) as GameObject;

            newAgent.transform.SetParent(this.transform, true);
        }

        if (pedGlobalParameters.rumoursEnabled || pedGlobalParameters.bikesEnabled)
        {
            BoxCollider col = newAgent.GetComponent <BoxCollider>();

            if (col != null)
            {
                col.enabled = true;
            }
            else
            {
                newAgent.AddComponent <BoxCollider>();
            }
        }

        FlashPedestriansController controller = newAgent.GetComponent <FlashPedestriansController>();

        controller.uniqueId      = nextIdForPedestrian++;
        controller.profile       = profile;
        controller.routing       = new FlashPedestriansRouting(destination, itinerary, destIndex);
        controller.flashInformer = flashInformer;

        // Subscribe pedestrian to the itinerary informer
        flashInformer.SubscribePedestrian(controller);

        newAgent.name = "flashPedestrian_" + this.name + "_" + "_" + controller.uniqueId;

        newAgent.SetActive(true);

        // Track the pedestrian in the heatmap
        if (trackPedestriansInHeatmap && heatmapCtrl != null)
        {
            heatmapCtrl.TrackNewElement(newAgent);
        }

        // Atomic increment of the KPI property
        Interlocked.Add(ref pedGlobalParameters.numberOfPedestriansOnScenario, pedGlobalParameters.numberOfPedestriansPerAgent);

        if (++numberOfPedestriansGenerated >= maxNumberOfPedestriansToSpawn && !spawnPedestriansInInfiniteLoop)
        {
            CancelInvoke();
        }
    }
    /// <summary>
    /// Auxiliar private method. Creates and updates the graphic vehicles in the Unity scene
    /// for the current timestep of the simulation.
    /// </summary>
    private void UpdateVehiclesInScene()
    {
        int currentNumberOfVehicles = trafficData.GetNumberOfVehiclesInTimeStep(timeStepIndex);

        // Log the number of vehicles once per second (independently of game time)
        if (Time.time > nextActionTime)
        {
            nextActionTime = Time.time + period;
            log.Info("Current number of vehicles in the simulation: " + currentNumberOfVehicles);
        }

        //Check for each vehicle in the Traffic Integration Data at the current timestep
        for (int i = 0; i < currentNumberOfVehicles; i++)
        {
            VehicleTDB v = trafficData.GetVehicleAt(timeStepIndex, i);

            Vector3 v3;
            if (UseCoordinateConversion)
            {
                v3 = CoordinateConvertor.LatLonToVector3(v.latitude, v.longitude);
            }
            else
            {
                v3 = new Vector3(v.latitude, 0.0f, v.longitude);
            }

            TrafficIntegrationVehicle vController;

            if (vehControllers.TryGetValue(v.id, out vController))
            {
                if (!vController.gameObject.activeSelf)
                {
                    vController.gameObject.SetActive(true);
                }

                vController.isUpdated = true;

                var meshRenderer = vController.gameObject.GetComponent <MeshRenderer>();
                var lod          = vController.gameObject.GetComponent <LODGroup>();

                //Render an existing vehicle if it is inside the frustrum
                if (!UseFrustumForUpdate || UnityEngine.GeometryUtility.TestPlanesAABB(cameraFrustum.Frustum, new Bounds(v3, 2 * Vector3.one)) || v.type.ToUpperInvariant().Contains("BUS"))
                {
                    vController.UpdateVehiclePosition(v3.x, v3.z, v.angle);

                    if (meshRenderer != null)
                    {
                        meshRenderer.enabled = true;
                    }
                    if (lod != null)
                    {
                        lod.enabled = true;
                    }
                }
                else
                {
                    if (meshRenderer != null)
                    {
                        meshRenderer.enabled = false;
                    }
                    if (lod != null)
                    {
                        lod.enabled = false;
                    }
                }
            }
            else
            {
                //Create a new vehicle in the scene
                Vector3 vehPosition = new Vector3(v3.x, 0.0f, v3.z);

                GameObject vehObject;

                var vehType = v.type.ToUpperInvariant();

                if (vehType.Contains("BUS") && graphicBus != null)
                {
                    if (v.id.Contains("dz_repl")) // for Driebergen-Zeist case
                    {
                        vehObject = (GameObject)Instantiate(graphicAltBus, vehPosition, Quaternion.identity);
                    }
                    else
                    {
                        vehObject = (GameObject)Instantiate(graphicBus, vehPosition, Quaternion.identity);
                    }

                    if (routingContr != null)
                    {
                        routingContr.HandleBusObjectsFromSumo(vehObject, v.id);
                    }
                }
                else if (vehType.Contains("FERRY") && ferryModel != null)
                {
                    vehObject = (GameObject)Instantiate(ferryModel, vehPosition, Quaternion.identity);
                }
                else if (vehType.Contains("TRAM") && tramModel != null)
                {
                    vehObject = (GameObject)Instantiate(tramModel, vehPosition, Quaternion.identity);
                }
                else if (vehType.Contains("TRAIN") && trainModel != null)
                {
                    vehObject = (GameObject)Instantiate(trainModel, vehPosition, Quaternion.identity);
                }
                else if (vehType.Contains("PENDELTAG") && pendeltagModel != null)
                {
                    vehObject = (GameObject)Instantiate(pendeltagModel, vehPosition, Quaternion.identity);
                }
                else
                {
                    vehObject = (GameObject)Instantiate(graphicCar[Random.Range(0, graphicCar.Length)], vehPosition, Quaternion.identity);
                }

                vehObject.transform.parent = this.transform;
                vehObject.name             = v.id;
                TrafficIntegrationVehicle vc = vehObject.GetComponent <TrafficIntegrationVehicle>();
                vc.smooth                   = smoothPaths;
                vc.brakingActive            = brakingActive && (trafficContr.typeOfIntegration == TrafficIntegrationController.TypeOfIntegration.SumoLiveIntegration);
                vc.timeToBrakeInSeconds     = timeToBrakeInSeconds;
                vc.driversPatienceInSeconds = driversPatienceInSeconds;
                vc.angleOfView              = angleOfView;
                vehControllers.Add(v.id, vc);

                // Track the vehicle in the heatmap
                if (visualizeTrafficHeatmap && heatmapCtrl != null)
                {
                    heatmapCtrl.TrackNewElement(vehObject);
                }
            }
        }
    }