コード例 #1
0
    public MovementPlan GetStartingPlan(Vector2 currentMoveDirection, IUnit unit)
    {
        var moveDirection = unit != null
            ? ((Vector2)unit.Transform.position - unit.AngleDefinition.GetPointClosestTo(_player.transform.position)).normalized
            : currentMoveDirection;
        var targetLocation = unit?.AngleDefinition.GetPointClosestTo(_player.transform.position) ?? StartingTargetLocation(_player.transform.position, moveDirection);

        targetLocation =
            BoundaryHelper.HandleBoundaryCollision(targetLocation, moveDirection);

        // Obstacle start position = mover.position
        // Target end position = targetLocation (only adjust if there is no unit)

        _currentIntersections = _intersectionDetector.GetIntersectionsFromUnit(unit);

        var startingPlanValues = new MovementPlanValues(targetLocation, moveDirection, unit, _currentIntersections);
        var startingPlanStates = new MovementPlanStates(false, unit == null, false);

        var movementPlan = new MovementPlan(startingPlanValues, startingPlanStates);

        movementPlan.SetFirst();

        OnFirstMove?.Invoke(1);

        return(EditMovementPlanForObstacles(movementPlan, _player.transform.position));
    }
コード例 #2
0
 public void SetCurrentPlan(MovementPlan plan)
 {
     _currentPlan = plan;
     if (_currentPlan == null)
     {
         return;
     }
     _distanceToCurrentDestination = Vector2.Distance(_mover.position, _currentPlan.TargetLocation);
 }
コード例 #3
0
 private void RequestNextPlan(MovementPlan previousPlan, Action <MovementPlan> callback, bool redirected, bool movingThroughObstacle)
 {
     if (movingThroughObstacle)
     {
         callback(RequestPlanFromObstaclePath(previousPlan));
     }
     else
     {
         callback(redirected
             ? RequestPlanFromRedirectSuccess(previousPlan)
             : RequestPlanFromRedirectFailureOrFinish(previousPlan));
     }
 }
コード例 #4
0
    public MovementPlan GetPlanForNextMoveDuringObstaclePath(MovementPlan previousPlan)
    {
        var targetLocation = !_obstaclePathFinder.HasObstaclePath()
            ? _obstaclePathFinder.TargetLocationOnceFinishedWithObstaclePath
            : _obstaclePathFinder.NextPoint().ProjectedPoint;

        var moveDirection = !_obstaclePathFinder.HasObstaclePath()
            ? (_obstaclePathFinder.TargetLocationOnceFinishedWithObstaclePath -
               _obstaclePathFinder.PointBeforeStartingObstaclePath).normalized
            : (targetLocation - previousPlan.TargetLocation).normalized;

        var obstaclePlanValues = new MovementPlanValues(targetLocation, moveDirection, previousPlan.TargetUnit, previousPlan.CurrentIntersections);
        var obstaclePlanStates = new MovementPlanStates(previousPlan.HeadingForIntersection, previousPlan.TargetUnit == null, _obstaclePathFinder.HasObstaclePath());

        return(new MovementPlan(obstaclePlanValues, obstaclePlanStates));
    }
コード例 #5
0
    public MovementPlan PlanMovingTowardsIntersectingUnitFromPreviousIntersection(MovementPlan previousPlan)
    {
        var targetLocation = previousPlan.TargetUnit.AngleDefinition.GetPointClosestTo(_player.transform.position);

        var moveDirection = (targetLocation - previousPlan.TargetLocation).normalized;

        _currentIntersections = _intersectionDetector.GetIntersectionsFromUnit(previousPlan.TargetUnit);

        // Obstacle start position = previousPlan.TargetLocation
        // Target end position = targetLocation (never adjusted)
        var successfulRedirectionValues = new MovementPlanValues(targetLocation, moveDirection, previousPlan.TargetUnit, _currentIntersections);
        var successfulRedirectStates    = new MovementPlanStates(false, false, false);

        var movementPlan = new MovementPlan(successfulRedirectionValues, successfulRedirectStates);

        return(EditMovementPlanForObstacles(movementPlan, previousPlan.TargetLocation));
    }
コード例 #6
0
    private MovementPlan EditMovementPlanForObstacles(MovementPlan movementPlan, Vector2 fromPosition)
    {
        var obstaclePriority = _obstaclePathFinder.HasObstaclePath();

        if (!obstaclePriority)
        {
            obstaclePriority =
                _obstaclePathFinder.FoundPath(fromPosition, movementPlan.TargetLocation, movementPlan.MoveDirection);
        }

        if (!obstaclePriority)
        {
            return(movementPlan);
        }
        var targetLocation      = _obstaclePathFinder.NextPoint().ProjectedPoint;
        var targetMoveDirection = (targetLocation - fromPosition).normalized;
        var updatedPlanValues   = new MovementPlanValues(targetLocation, targetMoveDirection, movementPlan.TargetUnit, _currentIntersections);
        var updatedPlanStates   = new MovementPlanStates(movementPlan.HeadingForIntersection, movementPlan.Finished, true);

        return(new MovementPlan(updatedPlanValues, updatedPlanStates));
    }
コード例 #7
0
    public MovementPlan FinishPlanOrMoveToNextIntersect(MovementPlan previousPlan)
    {
        MovementPlan movementPlan;

        if (previousPlan.ValidIntersections())
        {
            _currentIntersections = previousPlan.CurrentIntersections;
            var targetUnit     = _currentIntersections.Dequeue();
            var targetLocation = targetUnit.AngleDefinition.IntersectionPoint;

            // Obstacle start position = previousPlan.TargetLocation
            // Target end position = targetLocation (never adjusted)

            var nextIntersectionMovementValues = new MovementPlanValues(targetLocation, previousPlan.MoveDirection, targetUnit, _currentIntersections);
            var nextIntersectionMovementStates = new MovementPlanStates(true, false, false);

            movementPlan = new MovementPlan(nextIntersectionMovementValues, nextIntersectionMovementStates);

            return(EditMovementPlanForObstacles(movementPlan, previousPlan.TargetLocation));
        }

        var finishedTargetPosition = FinishedTargetLocation(previousPlan.TargetLocation, previousPlan.MoveDirection);

        finishedTargetPosition =
            BoundaryHelper.HandleBoundaryCollision(finishedTargetPosition, previousPlan.MoveDirection);

        // Obstacle start position = previousPlan.TargetLocation
        // Target end position = Adjusted(finishedTargetPosition);

        var finishedMovementValues = new MovementPlanValues(finishedTargetPosition, previousPlan.MoveDirection, null, null);
        var finishedMovementStates = new MovementPlanStates(false, true, false);

        movementPlan = new MovementPlan(finishedMovementValues, finishedMovementStates);

        return(EditMovementPlanForObstacles(movementPlan, previousPlan.TargetLocation));
    }
コード例 #8
0
 private MovementPlan RequestPlanFromObstaclePath(MovementPlan previousPlan)
 {
     return(_movementPlanner.GetPlanForNextMoveDuringObstaclePath(previousPlan));
 }
コード例 #9
0
 private MovementPlan RequestPlanFromRedirectSuccess(MovementPlan previousPlan)
 {
     return(_movementPlanner.PlanMovingTowardsIntersectingUnitFromPreviousIntersection(previousPlan));
 }
コード例 #10
0
 private MovementPlan RequestPlanFromRedirectFailureOrFinish(MovementPlan previousPlan)
 {
     return(_movementPlanner.FinishPlanOrMoveToNextIntersect(previousPlan));
 }
コード例 #11
0
 public void Reset()
 {
     OnArrival?.Invoke();
     _currentMoveSpeed = _defaultMoveSpeed;
     _currentPlan      = null;
 }