Beispiel #1
0
    void Start()
    {
        vehicleFactory = FindObjectOfType <VehicleFactory>();
        string filePath = System.IO.Path.Combine(Application.dataPath, "Sumo");

        ImportAndGenerate.parseXMLfiles(filePath);
        ImportAndGenerate.CreateStreetNetwork();
        client = new TraCIClient();
        if (client.Connect(ip, port))
        {
            Debug.Log("Connected to Sumo");
            connected = true;
        }
        else
        {
            Debug.Log("Unable to connect to Sumo");
            this.enabled = false;
            return;
        }
        FindObjectOfType <CameraManager>().frameRate = 60;
        StartCoroutine(Run());
        vehicleFactory.StopAllCoroutines();
        TrafficLightManager.GetInstance().RefreshTrafficLightsAndJunctions();

        // Traffic Flow
        if (!IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_FLOW))
        {
            StartCoroutine(RunTraffic3DTrafficFlow());
        }

        // Traffic Lights
        List <string> junctionIds = client.TrafficLight.GetIdList().Content;

        foreach (string id in junctionIds)
        {
            List <string> controlledLanes = client.TrafficLight.GetControlledLanes(id).Content;
            string        currentState    = client.TrafficLight.GetState(id).Content;
            for (int i = 0; i < controlledLanes.Count; i++)
            {
                TrafficLight trafficLight = TrafficLightManager.GetInstance().GetTrafficLight(controlledLanes[i]);
                if (trafficLight != null)
                {
                    SumoTrafficLight sumoTrafficLight = sumoTrafficLights.Find(s => s.trafficLight.trafficLightId.Equals(trafficLight.trafficLightId));
                    if (sumoTrafficLight == null)
                    {
                        sumoTrafficLights.Add(new SumoTrafficLight(trafficLight, id, new HashSet <int>()
                        {
                            i
                        }));
                    }
                    else
                    {
                        sumoTrafficLight.AddIndexState(i);
                    }
                }
            }
            // Remove all current traffic light programs in Sumo
            if (!IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_LIGHTS))
            {
                client.TrafficLight.SetRedYellowGreenState(id, new string('r', currentState.Length));
                client.TrafficLight.SetPhaseDuration(id, Double.MaxValue);
            }
        }
        if (IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_LIGHTS))
        {
            TrafficLightManager.GetInstance().StopAllCoroutines();
            StartCoroutine(RunTrafficLights());
        }
        else
        {
            TrafficLightManager.GetInstance().trafficLightChangeEvent += ChangeSumoTrafficLights;

            foreach (tlLogicType tlLogicType in ImportAndGenerate.trafficLightPrograms.Values)
            {
                int      stateCounter = 0;
                Junction junction     = FindObjectsOfType <Junction>().ToList().Find(j => j.junctionId.Equals(tlLogicType.id));
                List <SumoTrafficLight> sumoTrafficLightsForJunction = sumoTrafficLights.FindAll(sumoTrafficLight => sumoTrafficLight.junctionId.Equals(tlLogicType.id));
                foreach (object obj in tlLogicType.Items)
                {
                    if (obj is phaseType)
                    {
                        stateCounter++;
                        GameObject stateObject = new GameObject("State" + stateCounter);
                        stateObject.transform.SetParent(junction.gameObject.transform);
                        JunctionState junctionState = stateObject.AddComponent <JunctionState>();
                        junctionState.stateNumber        = stateCounter;
                        junctionState.trafficLightStates = new JunctionState.TrafficLightState[sumoTrafficLightsForJunction.Count()];
                        int       trafficLightStateCounter = 0;
                        phaseType phase = (phaseType)obj;

                        foreach (SumoTrafficLight sumoTrafficLight in sumoTrafficLightsForJunction)
                        {
                            TrafficLight.LightColour lightColour = sumoTrafficLight.GetLightColourFromStateString(phase.state);
                            junctionState.trafficLightStates[trafficLightStateCounter] = new JunctionState.TrafficLightState(sumoTrafficLight.trafficLight.trafficLightId, lightColour);
                            trafficLightStateCounter++;
                        }
                    }
                }
            }
        }
    }
Beispiel #2
0
    void OnGUI()
    {
        GUILayout.Label("Terrain + Street Network Generation Settings", EditorStyles.boldLabel);
        terrain = EditorGUILayout.Toggle("Generate Terrain?", terrain);
        trees   = EditorGUILayout.Toggle("Generate Trees?", trees);
        grass   = EditorGUILayout.Toggle("Generate Grass?", grass);
        streets = EditorGUILayout.Toggle("Generate Streets?", streets);

        buildings       = EditorGUILayout.Toggle("Generate Buildings?", buildings);
        polyBuildings   = EditorGUILayout.Toggle("| -> PolyBuildings?", !randomBuildings);
        randomBuildings = EditorGUILayout.Toggle("| -> Random Buildings?", !polyBuildings);
        buildingsCount  = EditorGUILayout.TextField("Buildings Count?", buildingsCount);
        GUILayout.Space(16);

        GUILayout.Label("Ego Vehicle Generation Settings", EditorStyles.boldLabel);
        egoVehicle = EditorGUILayout.BeginToggleGroup("Generate ego Vehicle?", egoVehicle);
        op         = (egoVehicleOptions)EditorGUILayout.EnumPopup("egoVehicle integration:", op);
        EditorGUILayout.EndToggleGroup();

        cameraScriptEnable = EditorGUILayout.Toggle("Automatic camera script?", !egoVehicle);

        sumoIntegration = EditorGUILayout.BeginToggleGroup("SUMO interface?", sumoIntegration);
        sumoIPPort      = EditorGUILayout.TextField("SUMO IP:Port", sumoIPPort);
        sumoGUI         = EditorGUILayout.Toggle("SUMO GUI (console if unchecked)", sumoGUI);
        EditorGUILayout.EndToggleGroup();

        GUILayout.Space(16);

        sumoFilesPath = EditorGUILayout.TextField("SUMO Files", sumoFilesPath);
        if (GUILayout.Button("Change Folder Location"))
        {
            sumoFilesPath = EditorUtility.OpenFolderPanel("Chose the folder containing the SUMO files (map.edg.xml, map.nod.xml, map.net.xml, map.rou.xml)", Application.dataPath, "");
            EditorGUILayout.TextField("SUMO Files", sumoFilesPath);
        }
        GUILayout.Space(16);

        GUILayout.Label("Processing", EditorStyles.boldLabel);
        if (GUILayout.Button("Start"))
        {
            // Disable main Camera of new scene
            try
            {
                GameObject mainCamera = GameObject.Find("Main Camera");
                mainCamera.SetActive(false);
            }
            catch (Exception ex)
            {
                MonoBehaviour.print(ex.GetBaseException());
            }

            if (streets)
            {
                EditorUtility.DisplayProgressBar("Generation Progress", "Parsing SUMO files", 0.0f);
                ImportAndGenerate.parseXMLfiles(sumoFilesPath);

                EditorUtility.DisplayProgressBar("Generation Progress", "Generating Street Network", 0.2f);
                ImportAndGenerate.drawStreetNetwork();
            }

            if (terrain)
            {
                EditorUtility.DisplayProgressBar("Generation Progress", "Generating Terrain, Trees, Grass", 0.3f);
                ImportAndGenerate.generateTerrain(trees, grass);
            }

            if (buildings)
            {
                EditorUtility.DisplayProgressBar("Generation Progress", "Generating buildings", 0.4f);
                if (randomBuildings)
                {
                    ImportAndGenerate.generatingBuildings(Convert.ToInt32(buildingsCount));
                }
                else if (polyBuildings)
                {
                    ImportAndGenerate.generatingPolyBuildings();
                }
            }

            EditorUtility.DisplayProgressBar("Generation Progress", "Generating Ego Vehicle", 0.8f);
            Boolean udp = (op.Equals(egoVehicleOptions.UDP) ? true : false);
            if (egoVehicle)
            {
                ImportAndGenerate.insertEgoVehicle(udp);
            }

            EditorUtility.DisplayProgressBar("Generation Progress", "Generating SUMO interface", 0.8f);
            ImportAndGenerate.openSUMOFilesAndsaveTotextFile(sumoIntegration, sumoGUI, sumoIPPort, egoVehicle);

            if (cameraScriptEnable)
            {
                EditorUtility.DisplayProgressBar("Generation Progress", "Generating cameras", 1.0f);
                ImportAndGenerate.addCameraScript();
            }


            EditorUtility.ClearProgressBar();

            this.Close();
        }

        GUILayout.Space(16);
    }