public int CountOfTypeInOOC(ServiceLocator.ID id, ServiceLocator.ThingType type) { if (id == ServiceLocator.ID.p0) { if (p0_Interactable_ObjsOfConcern.Length == 0) { return(0); } else { int counter = 0; for (int i = 0; i < p0_Interactable_ObjsOfConcern.Length; i++) { if (p0_Interactable_ObjsOfConcern[i].type == type) { counter++; } } return(counter); } } else { if (p1_Interactable_ObjsOfConcern.Length == 0) { return(0); } else { int counter = 0; for (int i = 0; i < p1_Interactable_ObjsOfConcern.Length; i++) { if (p1_Interactable_ObjsOfConcern[i].type == type) { counter++; } } return(counter); } } }
public Thing ClosestThingOfTypeInOOC(ServiceLocator.ID id, ServiceLocator.ThingType type) { Thing[] examinedOOC; Thing examinedObjectOfInterest; if (id == ServiceLocator.ID.p0) { examinedOOC = p0_Interactable_ObjsOfConcern; examinedObjectOfInterest = p0_InteractableInterested; } else { examinedOOC = p1_Interactable_ObjsOfConcern; examinedObjectOfInterest = p1_InteractableInterested; } // if there's nothing, closest is nothing if (examinedOOC.Length == 0) { return(null); } // if there's something in the list, but none of the right type, closest is nothing else if (CountOfTypeInOOC(id, type) == 0) { return(null); } // if there's one of type, then the closest is only one else if (CountOfTypeInOOC(id, type) == 1) { for (int i = 0; i < examinedOOC.Length; i++) { if (examinedOOC[i].type == type) { return(examinedOOC[i]); } } } // if there's more than one of type, then the closest must be found in order to be returned else { List <Thing> allOfTypeInOOC = new List <Thing>(); for (int i = 0; i < examinedOOC.Length; i++) { if (examinedOOC[i].type == type) { allOfTypeInOOC.Add(examinedOOC[i]); } } float shortestDist = 9999999; Thing currentClosestThing = null; for (int i = 0; i < allOfTypeInOOC.Count; i++) { Vector3 pos1 = GetWhereIsThing(examinedObjectOfInterest); Vector3 pos2 = GetWhereIsThing(allOfTypeInOOC[i]); if (Vector3.Distance(pos1, pos2) < shortestDist) { currentClosestThing = allOfTypeInOOC[i]; } } return(currentClosestThing); } return(null); }