/// <summary> /// This will spawn 2 vehicles at each end of this transline. /// Will reuse deactivated vehicles if exist or it will create a new vehicle. /// </summary> protected void SpawnAndResetTimer() { if (timeController == null) { return; } startTime = timeController.gameTime; GameObject vehicle; if (vehiclesOutOfService.Count > 0) { vehicle = vehiclesOutOfService.First(); vehiclesOutOfService.Remove(vehicle); vehicle.SetActive(true); vehicle.GetComponent <VehicleController>().ResetVehicle(LineDirection.OutBound); } else { vehicle = VehicleController.CreateGameObject(transline, LineDirection.OutBound); vehicle.transform.SetParent(transform); transline.AddVehicle(vehicle); } if (vehiclesOutOfService.Count > 0) { vehicle = vehiclesOutOfService.First(); vehiclesOutOfService.Remove(vehicle); vehicle.SetActive(true); vehicle.GetComponent <VehicleController>().ResetVehicle(LineDirection.InBound); } else { vehicle = VehicleController.CreateGameObject(transline, LineDirection.InBound); vehicle.transform.SetParent(transform); transline.AddVehicle(vehicle); } }
/// <summary> /// Add SumoBusController to the buses coming from Sumo. /// </summary> /// <param name="busObject"></param> /// <param name="id"></param> public void HandleBusObjectsFromSumo(GameObject busObject, string id) { string[] firstSplits = id.Split('.'); string[] secondSplits = firstSplits[0].Split('_'); string busNumber = secondSplits[0]; int busNum; if (!Int32.TryParse(busNumber, out busNum)) { busNumber = Regex.Match(id, @"\d+").Value; //Debug.Log("[Regex] busNum: " + busNumber); } LineController lineController = null; foreach (var line in Lines.Values) { if (line.category != LineCategory.Bus) { continue; } var lineNums = line.lineName.Split('_').ToList(); //if (busNumber.Equals(line.lineName)) if (lineNums.Contains(busNumber)) { lineController = line; break; } } //Debug.Log(lineController); // Found the line with the same busNumber. if (lineController != null) { //Debug.Log(busObject.name + " is added to " + lineController.name); lineController.AddVehicle(busObject); var busController = busObject.AddComponent <SumoBusController>(); busController.line = lineController; //busController.capacity = lineController.vehicleCapacity; var s1 = lineController.stations.First(); var s2 = lineController.stations.Last(); var d1 = Vector3.Distance(busObject.transform.position, s1.transform.position); var d2 = Vector3.Distance(busObject.transform.position, s2.transform.position); // Check if the bus is going outbound or inbound // by comparing the distance to the first and the last station. // If closer to the first station => outbound, // if closer to the last station => inbound. if (d1 < d2) { //busController.currentStation = s1; //busController.nextStation = s1; //busController.direction = LineDirection.OutBound; busController.ResetVehicle(LineDirection.OutBound); } else { //busController.currentStation = s2; //busController.nextStation = s2; //busController.direction = LineDirection.InBound; busController.ResetVehicle(LineDirection.InBound); } // Obsolete: should be taken care of by the SumoBusController. // busController.ArrivedAtNextStation(); } }