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));
    }
Exemple #2
0
    public MovementPackage(Transform mover, Destination destination, float distanceScalar)
    {
        _mover = mover;
        MovementCount++;
        DistanceScalar             = distanceScalar;
        Destination                = destination;
        Destination.TargetLocation = BoundaryHelper.HandleBoundaryCollision(Destination.TargetLocation, destination.MoveDirection);
        if (BoundaryHelper.WillBeMovingThroughBoundary(_mover.transform.position, Destination.TargetLocation))
        {
            Destination = null;
        }

        if (Vector2.Distance(destination.TargetLocation, mover.transform.position) > 0.1f)
        {
            OnFirstMove?.Invoke();
        }
    }
    public void SetMovementPackage(MovementPackage movementPackage)
    {
        _requested       = false;
        _movementPackage = movementPackage;

        if (movementPackage?.Destination != null)
        {
            _distanceToCurrentDestination = Vector2.Distance(_transform.position, _movementPackage.Destination.TargetLocation);
            OnNewMovementStart?.Invoke(_movementPackage.Destination.DestinationType);

            if (MovementPackage.MovementCount % 2 != 0)
            {
                OnFirstMove?.Invoke();
            }
            else
            {
                OnKill?.Invoke();
            }
        }
    }
    public void Tick()
    {
        if (!IsMoving)
        {
            _mover.GetComponent <Collider2D>().enabled = true;
            return;
        }

        if (_currentPlan.TargetUnit != null &&
            Vector2.Distance(_currentPlan.TargetUnit.Transform.position, _mover.position) < 3f)
        {
            _mover.GetComponent <Collider2D>().enabled = false;
        }

        if (_currentPlan.IsFirst)
        {
            OnFirstMove?.Invoke();
        }

        if (InRequestRange() && _currentPlan.HeadingForObstacle)
        {
            _mover.GetComponent <Collider2D>().enabled = false;

            MovementPlanRequested?.Invoke(_currentPlan, SetCurrentPlan, false, _currentPlan.HeadingForObstacle);
            _currentMoveSpeed = _defaultMoveSpeed;
        }

        HandleIntersection();

        HandlePotentialFinished();

        IncreaseSpeedOnFinalMovement();

        if (Arrived(_currentPlan.TargetLocation))
        {
            Reset();
            return;
        }

        _mover.position = Vector2.MoveTowards(_mover.position, _currentPlan.TargetLocation, _currentMoveSpeed * Time.deltaTime);
    }