public GameObject FindNearestWayPointByType(Vector3 fromPosition, TacticalWaypoint.WaypointType type) { List <GameObject> waypointArray = null; switch (type) { case TacticalWaypoint.WaypointType.CoverPoint: waypointArray = coverPoints; break; case TacticalWaypoint.WaypointType.AmbushPoint: waypointArray = ambushPoints; break; default: break; } GameObject nearestWaypoint = null; if (waypointArray != null) { float distance = Mathf.Infinity; foreach (var w in waypointArray) { Vector3 diff = w.transform.position - fromPosition; if (diff.magnitude < distance) { nearestWaypoint = w; distance = diff.magnitude; } } } return(nearestWaypoint); }
public void GoToCapturePoint(CapturePoint.CapturePointId captureId, TacticalWaypoint.WaypointType findWaypointType, bool preferTeamCover = true) { CapturePoint point = capturePointMap.capturePoints[captureId]; targetArea = point.GetComponent <SphereCollider>(); List <TacticalWaypoint> waypointTypeOnCapturePoint = null; switch (findWaypointType) { case TacticalWaypoint.WaypointType.NonePoint: waypointTypeOnCapturePoint = null; break; case TacticalWaypoint.WaypointType.CoverPoint: waypointTypeOnCapturePoint = point.coverpointsOnCapturePoint; break; case TacticalWaypoint.WaypointType.AmbushPoint: waypointTypeOnCapturePoint = point.ambushPointsOnCapturePoint; break; default: waypointTypeOnCapturePoint = null; break; } // If we want to find a cover point, and the capture point has one if (waypointTypeOnCapturePoint != null && waypointTypeOnCapturePoint.Count != 0) { // If we want cover that is closer to the players team side if (preferTeamCover) { // Create a local list of cover points that belong to a team List <TacticalWaypoint> teamWaypointTypes = new List <TacticalWaypoint>(); foreach (var waypointType in waypointTypeOnCapturePoint) { if (waypointType.closerToWhichTeamSide == team.playerTeam) { teamWaypointTypes.Add(waypointType); } } // If we have some team cover points to go to, then pick a random one if (teamWaypointTypes.Count != 0) { //targetTransform = teamWaypointTypes[Random.Range(0, teamWaypointTypes.Count)].transform; TacticalWaypoint tw = teamWaypointTypes[Random.Range(0, teamWaypointTypes.Count)]; targetTransform = tw.transform; targetArea = tw.GetComponent <SphereCollider>(); } else { // If there are no team cover points found, just pick a random one //targetTransform = waypointTypeOnCapturePoint[Random.Range(0, waypointTypeOnCapturePoint.Count)].transform; TacticalWaypoint tw = waypointTypeOnCapturePoint[Random.Range(0, waypointTypeOnCapturePoint.Count)]; targetTransform = tw.transform; targetArea = tw.GetComponent <SphereCollider>(); } } else { // For now go to a random cover point // targetTransform = waypointTypeOnCapturePoint[Random.Range(0, waypointTypeOnCapturePoint.Count)].transform; TacticalWaypoint tw = waypointTypeOnCapturePoint[Random.Range(0, waypointTypeOnCapturePoint.Count)]; targetTransform = tw.transform; targetArea = tw.GetComponent <SphereCollider>(); } } else { // If we dont want to find either a cover point, or an ambush type, then just go to the capture point //targetTransform = capturePointMap.capturePoints[captureId].transform; CapturePoint tw = capturePointMap.capturePoints[captureId]; targetTransform = tw.transform; targetArea = tw.GetComponent <SphereCollider>(); } }