Exemplo n.º 1
0
    public void SendStateData()
    {
        if (webSocket == null)
        {
            Debug.LogError("Unable to send data, client was not set up or does not have an active connection!");
            return;
        }

        JSONObject j = new JSONObject(JSONObject.Type.OBJECT);

        List <int> sentIds    = new List <int>();
        List <int> doubleInts = new List <int>();

        for (int i = 0; i < TrafficManager.Instance.Trafficlights.Count; i++)
        {
            Trafficlight trafficlight = TrafficManager.Instance.Trafficlights[i];

            //Invalid stoplight data, just for visual aspect.
            if (trafficlight.stoplineCollider == null || trafficlight.hasLeftLaneCollider == null)
            {
                continue;
            }

            //Ignore trafficlights who got count at 0 if 'onlySendStatesWithACount' is checked.
            if (onlySendStatesWithACount && trafficlight.WaitingAgents.Count < 1)
            {
                continue;
            }

            if (sentIds.Contains(trafficlight.Id))
            {
                Debug.LogWarning("Sent double traffic light id! " + trafficlight.Id);
                continue;
            }

            sentIds.Add(trafficlight.Id);


            JSONObject arrayObject = new JSONObject();
            arrayObject.AddField("trafficLight", trafficlight.Id);
            arrayObject.AddField("count", trafficlight.WaitingAgents.Count);
            j.Add(arrayObject);
        }


        string encodedString = "{\"state\":" + j.Print() + "}";

        Send(encodedString);
    }
Exemplo n.º 2
0
    public void AssignNextTrafficlight()
    {
        if (TrafficlightQueue.Count < 1)
        {
            WaitingForTrafficLight = null;
            return;
        }

        WaitingForTrafficLight = TrafficlightQueue.Dequeue();

        if (!WaitingForTrafficLight.WaitingAgents.Contains(this))
        {
            WaitingForTrafficLight.WaitingAgents.Add(this);
        }
    }
Exemplo n.º 3
0
    private void SetAllTrafficLights(Trafficlight.eTrafficState newState)
    {
        for (int i = 0; i < TrafficLanes.Count; i++)
        {
            for (int j = 0; j < TrafficLanes[i].trafficLights.Count; j++)
            {
                Trafficlight trafficLight = TrafficLanes[i].trafficLights[j];
                if (trafficLight == null)
                {
                    continue;
                }

                SetTrafficlightStateById(trafficLight.Id, newState);
            }
        }
    }
Exemplo n.º 4
0
    public void SetTrafficlightStateById(int id, Trafficlight.eTrafficState newTrafficLightState)
    {
        for (int i = 0; i < Trafficlights.Count; i++)
        {
            Trafficlight trafficLight = Trafficlights[i];

            if (trafficLight == null)
            {
                continue;
            }

            if (trafficLight.Id != id)
            {
                continue;
            }

            trafficLight.TrafficState = newTrafficLightState;
        }
    }