public bool FoundPath(Vector2 checkFromPosition, Vector2 checkToPosition, Vector2 moveDirection) { var obstacle = ObstacleHelper.GetObstacle(checkFromPosition, checkToPosition); if (BoundaryHelper.ContainedInObstacleCollider(checkToPosition) || BoundaryHelper.TargetLocationEndsInsideObstacle(checkToPosition)) { checkToPosition = BoundaryHelper.FindPositionThroughObstacle(checkToPosition, checkFromPosition); } checkToPosition += moveDirection * 5f; if (obstacle == null) { return(false); } TargetLocationOnceFinishedWithObstaclePath = checkToPosition; PointBeforeStartingObstaclePath = checkFromPosition; var adjustedPointToCheckClosestToStart = checkFromPosition; var closestPointToStart = obstacle.FindClosestPointOnSegmentFromProjectedPoint(adjustedPointToCheckClosestToStart); while (closestPointToStart == null) { adjustedPointToCheckClosestToStart += moveDirection * StepTowardsObstacleSide; closestPointToStart = obstacle.FindClosestPointOnSegmentFromProjectedPoint(adjustedPointToCheckClosestToStart); } var adjustedPointToCheckClosestToEnd = checkToPosition; var closestPointToEnd = obstacle.FindClosestPointOnSegmentFromProjectedPoint(adjustedPointToCheckClosestToEnd); while (closestPointToEnd == null) { adjustedPointToCheckClosestToEnd -= moveDirection * StepTowardsObstacleSide; closestPointToEnd = obstacle.FindClosestPointOnSegmentFromProjectedPoint(adjustedPointToCheckClosestToEnd); } _currentObstaclePath = obstacle.BestPath(closestPointToStart, closestPointToEnd, checkToPosition); return(_currentObstaclePath != null && _currentObstaclePath.Count > 0); }
public Queue <IUnit> GetIntersectionsRelativeTo(IUnit firstUnit) { var possibleIntersections = new List <IUnit>(); var firstStartCheckPoint = firstUnit.Transform.position; foreach (var unit in _units) { if (unit == firstUnit || unit == null || !BoundaryHelper.OnScreen(unit.Transform.position) || unit.Transform.gameObject.activeInHierarchy == false || unit.KillHandler.KillPoint.HasValue || unit.AngleDefinition.IntersectionPoint != Vector2.zero && BoundaryHelper.ContainedInObstacleCollider(unit.AngleDefinition.IntersectionPoint)) { continue; } var firstRearCheckPoint = firstUnit.AngleDefinition.RearPointRelative; var rearPoint = unit.AngleDefinition.RearPointRelative; var forwardPoint = unit.AngleDefinition.ForwardPointRelative; if (IntersectionMaths.IsIntersecting(firstStartCheckPoint, firstRearCheckPoint, rearPoint, forwardPoint)) { var intersect = IntersectionMaths.FindIntersection(firstStartCheckPoint, firstRearCheckPoint, rearPoint, forwardPoint); if (intersect != null) { var directionThroughKillPoint = (unit.KillHandler.GetFauxKillPoint() - (Vector2)unit.Transform.position).normalized; if (possibleIntersections.Any(t => Vector2.Distance(intersect.Value, t.AngleDefinition.IntersectionPoint) < 1f)) { continue; } if (!NodeGrid.Instance.NodeFromWorldPosition(intersect.Value).IsWalkable || !BoundaryHelper.OnScreen(intersect.Value) || BoundaryHelper.ContainedInObstacleCollider(intersect.Value)) { continue; } if (Vector2.Distance(unit.Transform.position, intersect.Value) < 2f || Vector2.Distance(unit.Transform.position, firstStartCheckPoint) < 1f || Vector2.Distance(firstUnit.Transform.position, intersect.Value) < 1f) { continue; } unit.AngleDefinition.SetIntersectionPoint(intersect.Value); possibleIntersections.Add(unit); } } } if (possibleIntersections.Count <= 0) { return(null); } var orderedIntersections = possibleIntersections.OrderBy(t => Vector2.Distance(firstStartCheckPoint, t.AngleDefinition.IntersectionPoint)); var intersectOrder = new Queue <IUnit>(); for (var i = 0; i < orderedIntersections.Count(); i++) { intersectOrder.Enqueue(orderedIntersections.ElementAt(i)); } return(intersectOrder.Count > 0 ? intersectOrder : null); }