private void OnTruckTravelNewConnection(AwareTruck truck, Connection nextTravelConnection) { // Update or add new connection to list if (m_currentTruckConnections.ContainsKey(truck)) { m_currentTruckConnections[truck] = nextTravelConnection; } else { m_currentTruckConnections.Add(truck, nextTravelConnection); } }
/// <summary> /// Coroutine for waiting X seconds and then navigating from a start and end on an AwareTruck /// </summary> /// <param name="seconds"></param> /// <param name="truck"></param> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> private IEnumerator WaitAndNavigate(float seconds, AwareTruck truck, GameObject start, GameObject end) { yield return(new WaitForSeconds(seconds)); // Truck has arrived at destined End point, make it travel back List <Connection> navPath = this.NavigateAStar(start, end); if (navPath != null) { truck.DriveAlong(navPath); Debug.Log($"Truck '{truck.gameObject.name}' returning to depot/start at '{end.name}'"); } }
/// <summary> /// Functionality for when a truck reaches its final path end /// </summary> /// <param name="truck"></param> /// <param name="arrivalWaypoint"></param> private void OnTruckReachedPathEnd(AwareTruck truck, GameObject arrivalWaypoint) { // Get the instantiated truck and it's info KeyValuePair <AwareTruck, TruckInfo> kvp = InstantiatedTrucks.FirstOrDefault(x => x.Key.gameObject.name == truck.gameObject.name); // Check it isn't null if (kvp.Key != null && kvp.Value != null) { TruckInfo info = kvp.Value; // Check if the arrivalWaypoint is equal to the set end point and not arriving at Start waypoint if (arrivalWaypoint == info.End) { StartCoroutine(WaitAndNavigate(3f, truck, info.End, info.Start)); truck.DeliverPackage(); } } }
private void Update() { if (m_currentTruckConnections.Count > 0) { /// For each truck and it's current connection foreach (KeyValuePair <AwareTruck, Connection> thisKvp in m_currentTruckConnections) { /// Compare waypoint names to check if they are the same KeyValuePair <AwareTruck, Connection> matchingKvp = m_currentTruckConnections .FirstOrDefault(kvp => kvp.Value.ToNode.name == thisKvp.Value.ToNode.name && kvp.Key.name != thisKvp.Key.name); AwareTruck truckOne = thisKvp.Key; AwareTruck truckTwo = matchingKvp.Key; /// If current iterarte truck is waiting, check if they can resume /// Only check truck one as the others will be checked next iteration if (truckOne.IsWaiting) { List <AwareTruck> waitingTrucks = new List <AwareTruck>(); bool isOnSameConnection = false; foreach (KeyValuePair <AwareTruck, Connection> checkKvp in m_currentTruckConnections) { if (thisKvp.Value.ToNode.name == checkKvp.Value.ToNode.name) { //isOnSameConnection = true; waitingTrucks.Add(thisKvp.Key); } } // More than 1 truck waiting at same connection, resume first one in list if (waitingTrucks.Count > 1) { waitingTrucks[0].ResumeMovement(); Debug.Log($"Resuming Truck '{waitingTrucks[0]}' in queue of '{waitingTrucks.Count}'"); } // else if truckOne has no others waiting, resume else if (!isOnSameConnection) { truckOne.ResumeMovement(); Debug.Log($"Resuming Truck '{truckOne.name}'"); } } // Check match isn't null if (matchingKvp.Key != null && matchingKvp.Value != null) { /// If isn't waiting and connections are matching... bool eitherTruckWaiting = truckOne.IsWaiting || truckTwo.IsWaiting; if (!eitherTruckWaiting && thisKvp.Value.ToNode.name == matchingKvp.Value.ToNode.name) { if (truckOne.Cargo.PackageCount > truckTwo.Cargo.PackageCount) { ///truckOne is slower, pause it truckOne.PauseMovement(); Debug.Log($"Pausing Truck '{truckOne.name};"); } else { /// truckTwo is slower /// or cargo is same so prioritise truckTwo truckTwo.PauseMovement(); Debug.Log($"Pausing Truck '{truckTwo.name}'"); } } } } } }