/// <summary> /// Finds the closest travel sphere relative to the entity /// </summary> private IDetectorSphere FindClosestSphereDetector() { SceneController sceneController = GameManager.Instance.sceneController; IDetectorSphere closestSphere = null; float closestDistance = 100000; foreach (IDetectorSphere sphere in sceneController.sphereDetectors) { float distance = Vector3.Distance(transform.position, sphere.GetSpherePosition()); if (distance < closestDistance) { closestDistance = distance; closestSphere = sphere; } } return(closestSphere); }
/// <summary> /// This autoregisters the entity on spaqwn to its nearest detector. /// </summary> private void AutoRegisterToDetector() { SceneController sceneController = GameManager.Instance.sceneController; float calculatedDistance = 0; float shortestDistance = 10000000f; IDetectorSphere closestDetector = null; foreach (IDetectorSphere detector in sceneController.sphereDetectors) { calculatedDistance = Vector3.Magnitude(detector.GetSpherePosition()); if (calculatedDistance < shortestDistance) { closestDetector = detector; shortestDistance = calculatedDistance; } } closestDetector.RegisterEntity(this.gameObject); }
/// <summary> /// Finds the closest enemy contained within the sphere area /// </summary> public GameObject FindClosestEnemy() { //Ensure thhat the current team is set if (currentTeam == null) { LoadCurrentTeam(); } IDetectorSphere sphereDetector = FindClosestSphereDetector(); if (sphereDetector.GetDetectedEntities().Count == 0) { return(null); } ITeam entityTeam = null; GameObject closestEnemy = null; float closestDistance = 100000; foreach (GameObject entity in sphereDetector.GetDetectedEntities()) { // Pass whether object has NOT beed removed or missing if (entity != null) { float distance = Vector3.Distance(transform.position, entity.transform.position); entityTeam = entity.GetComponent <ITeam>(); if (distance < closestDistance && currentTeam.GetTeamColor() != entityTeam.GetTeamColor()) { closestDistance = distance; closestEnemy = entity; } } } return(closestEnemy); }