/// <summary> /// Subscribes to all vehicles in the simulation /// </summary> public void Subscribe() { List <byte> carInfo = new List <byte> { Traci.TraCIConstants.POSITION_3D }; // Get all the car ids we need to keep track of. Traci.TraCIResponse <List <String> > CarIds = Client.Vehicle.GetIdList(); // Subscribe to all cars from 0 to 2147483647, and get their 3d position data CarIds.Content.ForEach(car => Client.Vehicle.Subscribe(car, 0, 2147483647, carInfo)); }
/// <summary> /// Update is called once per frame /// If the client is defined, it will attempt to get a list of vehicles who are currently active and set their positions/create them accordingly. /// </summary> void Update() { if (Client != null) { if (!TrafficLightsLoaded) { GameObject.Find("TrafficLights").GetComponent <TrafficLight>().Get_Traffic_Lights(); TrafficLightsLoaded = true; } if (OccupancyVisual) { if (VisualsSwitched) { //GameObject.Find("Occupancy_Legend").SetActive(true); foreach (Transform child in Cars_GO.transform) { GameObject.Destroy(child.gameObject); } VisualsSwitched = false; } Transform e = GameObject.Find("Edges").transform; if (e != null) { foreach (Transform child in e) { float o = (float)Client.Lane.GetLastStepOccupancy(child.gameObject.name).Content; if (o >= 0.9f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_7", typeof(Material)) as Material; } else if (o >= 0.8f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_6", typeof(Material)) as Material; } else if (o >= 0.5f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_5", typeof(Material)) as Material; } else if (o >= 0.1f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_4", typeof(Material)) as Material; } else if (o >= 0.01f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_3", typeof(Material)) as Material; } else if (o >= 0.001f) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_2", typeof(Material)) as Material; } else { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_1", typeof(Material)) as Material; } //int o = Client.Lane.GetLastStepVehicleNumber(child.gameObject.name).Content; //if (o >= 10) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_7", typeof(Material)) as Material; //} //else if (o >= 5) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_6", typeof(Material)) as Material; //} //else if (o >= 4) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_5", typeof(Material)) as Material; //} //else if (o >= 3) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_4", typeof(Material)) as Material; //} //else if (o >= 2) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_3", typeof(Material)) as Material; //} //else if (o >= 1) //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_2", typeof(Material)) as Material; //} //else //{ // child.gameObject.GetComponent<Renderer>().material = Resources.Load("Materials/Occupancy_Material_1", typeof(Material)) as Material; //} } } Transform j = GameObject.Find("Junctions").transform; if (j != null) { foreach (Transform child in j) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Occupancy_Material_2", typeof(Material)) as Material; } } } if (CarVisual) { if (Client != null) { if (VisualsSwitched) { //GameObject.Find("Occupancy_Legend").SetActive(false); Transform e = GameObject.Find("Edges").transform; foreach (Transform child in e) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Road_Material", typeof(Material)) as Material; } VisualsSwitched = false; Transform j = GameObject.Find("Junctions").transform; if (j != null) { foreach (Transform child in j) { child.gameObject.GetComponent <Renderer>().material = Resources.Load("Materials/Road_Material", typeof(Material)) as Material; } } } Cars_GO = GameObject.Find("Cars"); // Get all the car ids we need to keep track of. Traci.TraCIResponse <List <String> > CarIds = Client.Vehicle.GetIdList(); CarIds.Content.ForEach(carId => { Traci.Types.Position3D pos = Client.Vehicle.GetPosition3D(carId).Content; float rot = (float)Client.Vehicle.GetAngle(carId).Content; //Client.Vehicle.GetVehicleClass(carId).Content.Contains("bus"); Transform CarTransform = Cars_GO.transform.Find(carId); if (CarTransform != null) { if (pos == null) { GameObject.Destroy(GameObject.Find(carId)); } else { CarTransform.position = new Vector3((float)pos.X, 0.0f, (float)pos.Y); //if (CarTransform.rotation.y != rot) //{ // CarTransform.Rotate(0.0f, rot, 0.0f); //} CarTransform.localEulerAngles = new Vector3(0, rot - 90.0f, 0); } } else { GameObject car = GameObject.Instantiate(Resources.Load("Prefabs/Vehicle", typeof(GameObject)) as GameObject, new Vector3((float)pos.X, 0.0f, (float)pos.Y), new Quaternion(0.0f, 0.0f, 0.0f, 1.0f), Cars_GO.transform); if (carId.Contains("bus")) { car.transform.Find("Bus").gameObject.SetActive(true); car.transform.Find("Car").gameObject.SetActive(false); } else if (carId.Contains("moto")) { car.transform.Find("Motorcycle").gameObject.SetActive(true); car.transform.Find("Car").gameObject.SetActive(false); } else if (carId.Contains("truck")) { car.transform.Find("BoxTruck").gameObject.SetActive(true); car.transform.Find("Car").gameObject.SetActive(false); } else if (carId.Contains("bicycle")) { car.transform.Find("Bicycle").gameObject.SetActive(true); car.transform.Find("Car").gameObject.SetActive(false); } else if (carId.Contains("ped")) { car.transform.Find("Pedestrian").gameObject.SetActive(true); car.transform.Find("Car").gameObject.SetActive(false); } car.name = carId; car.transform.parent = Cars_GO.transform; car.transform.position = new Vector3((float)pos.X, 0.0f, (float)pos.Y); } }); } } Elapsedtime += Time.deltaTime; if (Elapsedtime > 1) { Client.Control.SimStep(); Elapsedtime = 0; } } }