Ejemplo n.º 1
0
    public void SetJunctionState(int junctionStateNumber)
    {
        JunctionState junctionState = GetJunctionState(junctionStateNumber);

        if (junctionState == null)
        {
            return;
        }
        SetJunctionState(junctionState);
    }
Ejemplo n.º 2
0
 public void SetJunctionState(JunctionState junctionState)
 {
     this.currentState = junctionState.GetStateNumber();
     foreach (JunctionState.TrafficLightState trafficLightState in junctionState.GetTrafficLightStates())
     {
         TrafficLightManager.GetInstance().GetTrafficLight(trafficLightState.GetTrafficLightId()).SetColour(trafficLightState.GetLightColour());
     }
     if (junctionState.GetPedestrianCrossingStates() != null)
     {
         foreach (JunctionState.PedestrianCrossingState pedestrianCrossingState in junctionState.GetPedestrianCrossingStates())
         {
             PedestrianManager.GetInstance().GetPedestrianCrossing(pedestrianCrossingState.GetPedestrianCrossingId()).SetAllowCrossing(pedestrianCrossingState.AllowCrossing());
         }
     }
 }
Ejemplo n.º 3
0
    public void SetNextJunctionState()
    {
        if (junctionStates.Length == 0)
        {
            RefreshJunctionStates();
        }
        JunctionState junctionState = GetJunctionState(currentState + 1);

        if (junctionState != null)
        {
            SetJunctionState(junctionState);
        }
        else
        {
            SetJunctionState(GetFirstJunctionState());
        }
    }
Ejemplo n.º 4
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++;
                        }
                    }
                }
            }
        }
    }