Exemplo n.º 1
0
        private bool CheckLedgeWhileHanging()
        {
            Bounds bounds = _motor.Collider.bounds;

            const float checkWidth  = 3f;
            const float checkHeight = 3f;

            int layer = CurrentClimbingState.PivotCollider.gameObject.layer;

            float xValue = layer == _leftClimbLayer ? bounds.max.x - (checkWidth / 2) : bounds.min.x + (checkWidth / 2);
            float yValue = bounds.max.y + 1;

            var origin = new Vector3(xValue, yValue);
            var size   = new Vector2(checkWidth, 0.01f);

            RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, size, 0, Vector2.up, checkHeight, GetClimbMask());
            hits = RemoveInvalidColliders(hits);

            var hit = hits.FirstOrDefault(h => EdgeValidator.CanJumpToHang(h.collider, bounds.center, _motor.StandingCollider));

            if (hit)
            {
                NextClimbingState = GetClimbingState(CurrentClimbingState.Climb, hit.collider);
                _anim.SetBool("inverted", ClimbSide == _motor.GetDirectionFacing());
            }
            return(hit);
        }
Exemplo n.º 2
0
        public bool CheckLedgeAbove(DirectionFacing direction, out Climb outputClimb, bool retryCheck = true)
        {
            Bounds bounds = _motor.Collider.bounds;

            outputClimb = Climb.None;
            Climb currentClimb = Climb.None;

            float       checkWidth  = _retryCheckAbove ? 4f : 1f;
            const float checkHeight = 4f;

            float xValue = direction == DirectionFacing.Left ? bounds.center.x - (checkWidth / 2) : bounds.center.x + (checkWidth / 2);
            float yValue = bounds.min.y + ConstantVariables.MaxLipHeight;

            float actualHeight = bounds.size.y + checkHeight - ConstantVariables.MaxLipHeight;

            var origin = new Vector3(xValue, yValue);
            var size   = new Vector2(checkWidth, 0.01f);

            RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, size, 0, Vector2.up, actualHeight, GetClimbMask());
            hits = RemoveInvalidColliders(hits);

            var hit = hits.FirstOrDefault(h => h.point.y > bounds.max.y && EdgeValidator.CanJumpToHang(h.collider, bounds.center, _motor.StandingCollider));

            if (hit)
            {
                if (ShouldStraightClimb(hit.collider))
                {
                    if (currentClimb == Climb.None)
                    {
                        currentClimb = Climb.Up;
                    }

                    outputClimb = Climb.Up;
                }
                else
                {
                    if (currentClimb == Climb.None)
                    {
                        currentClimb = Climb.Flip;
                    }

                    outputClimb = Climb.Flip;
                }
            }

            if (outputClimb == Climb.None)
            {
                int layer = direction == DirectionFacing.Left ? _rightClimbLayer : _leftClimbLayer;
                hit = hits.FirstOrDefault(h => h.collider.gameObject.layer == layer && h.point.y <= bounds.max.y && EdgeValidator.CanMantle(h.collider, bounds.center, _motor.CrouchedCollider));

                if (hit)
                {
                    if (currentClimb == Climb.None)
                    {
                        currentClimb = Climb.Mantle;
                    }

                    outputClimb = Climb.Mantle;
                }
            }

            if (outputClimb != Climb.None)
            {
                CurrentClimbingState = GetStaticClimbingState();
                NextClimbingState    = GetClimbingState(currentClimb, hit.collider);
            }
            else if (outputClimb == Climb.None && _retryCheckAbove && retryCheck)
            {
                _retryCheckAbove = false;
                CheckLedgeAbove(direction == DirectionFacing.Left ? DirectionFacing.Right : DirectionFacing.Left, out outputClimb);
            }
            _retryCheckAbove = true;
            return(outputClimb != Climb.None);
        }
Exemplo n.º 3
0
        public bool CheckGrab(DirectionFacing direction = DirectionFacing.None, bool holdingUp = false)
        {
            Bounds bounds      = _motor.Collider.bounds;
            float  checkLength = ConstantVariables.GrabDistance;

            var   origin   = new Vector2(bounds.center.x, bounds.max.y - 0.5f);
            float distance = bounds.size.x + checkLength * 2;

            RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, new Vector2(distance, 0.1f), 0, Vector2.down, 0.2f, GetClimbMask());

            if (hits.Any())
            {
                Debug.Log("grab");
            }

            int layer = 0;

            if (direction == DirectionFacing.Left)
            {
                layer = _rightClimbLayer;
            }
            else if (direction == DirectionFacing.Right)
            {
                layer = _leftClimbLayer;
            }

            RaycastHit2D hit = hits.FirstOrDefault(h => (holdingUp || (h.collider.CanClimbDown() == false || h.collider.gameObject.layer == layer)) && EdgeValidator.CanJumpToHang(h.collider, bounds.center, _motor.StandingCollider));

            if (hit)
            {
                CurrentClimbingState = GetClimbingState(
                    hit.collider.gameObject.layer == _leftClimbLayer ? Climb.AcrossRight : Climb.AcrossLeft,
                    hit.collider,
                    true);
                _motor.Anim.SetAcrossTrigger();
            }
            return(hit);
        }
Exemplo n.º 4
0
        public bool CheckLedgeAcross(DirectionFacing direction, Bounds?projectedBounds = null)
        {
            NextClimbs.Clear();
            Bounds bounds = projectedBounds != null
                                ? (Bounds)projectedBounds
                                : _motor.Collider.bounds;

            float       maxNonHangDistance = 6f;
            float       checkLength        = 7f;
            const float maxHeightAbove     = 1f;
            const float maxHeightBelow     = 2f;
            const float spaceInFront       = 2f;

            float checkDepth = bounds.size.y + maxHeightAbove + maxHeightBelow;

            float x;

            if (CurrentClimbingState.PivotCollider == null || CurrentClimbingState.CanClimbDown)
            {
                x            = direction == DirectionFacing.Left ? bounds.min.x - spaceInFront : bounds.max.x + spaceInFront;
                checkLength -= spaceInFront;
            }
            else
            {
                x = direction == DirectionFacing.Left ? bounds.min.x : bounds.max.x;
            }

            float y = bounds.center.y - maxHeightBelow + maxHeightAbove;

            var origin = new Vector2(x, y);

            var size = new Vector2(0.01f, checkDepth);

            Vector2 castDirection = direction == DirectionFacing.Left ? Vector2.left : Vector2.right;

            RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, size, 0, castDirection, checkLength, GetClimbMask());
            hits = RemoveInvalidColliders(hits);

            RaycastHit2D hit = hits.FirstOrDefault(h => h.point.y < bounds.center.y && Mathf.Abs(h.point.y - bounds.center.y) < maxNonHangDistance && EdgeValidator.CanJumpToOrFromEdge(h.collider, bounds.center, _motor.CrouchedCollider));

            bool  shouldHang        = false;
            Climb directionalAcross = direction == DirectionFacing.Left ? Climb.AcrossLeft : Climb.AcrossRight;

            if (hit)
            {
                shouldHang = CurrentClimbingState.PivotCollider != null && hit.collider.transform.parent == CurrentClimbingState.PivotCollider.transform.parent;
                if (shouldHang && EdgeValidator.CanJumpToHang(hit.collider, bounds.center, _motor.StandingCollider) == false)
                {
                    hit = new RaycastHit2D();
                }
            }
            else
            {
                hit = hits.FirstOrDefault(h => EdgeValidator.CanJumpToHang(h.collider, bounds.center, _motor.StandingCollider));

                if (hit)
                {
                    shouldHang = true;
                }
            }

            if (hit)
            {
                NextClimbingState = GetClimbingState(directionalAcross, hit.collider, shouldHang);
                _anim.SetBool("shouldHang", shouldHang);
                _anim.SetBool("inverted", ClimbSide == direction);
            }
            return(hit);
        }