예제 #1
0
        private MoveResult Reposition(IHitBox hitBox, MoveResult result, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var moveX = result.HasFlag(MoveResult.X) &&
                        !result.HasFlag(MoveResult.BlockedOnNegativeX) &&
                        !result.HasFlag(MoveResult.BlockedOnPositiveX);

            if (moveX)
            {
                aabb.Position.X += interpolation.X;
            }

            bool surfaced = CanSetSurfaced(hitBox);
            var  moveY    = result.HasFlag(MoveResult.Y) &&
                            !result.HasFlag(MoveResult.BlockedOnNegativeY) &&
                            !result.HasFlag(MoveResult.BlockedOnPositiveY);

            if ((moveY || !surfaced) && !result.HasFlag(MoveResult.Died))
            {
                hitBox.State = HitBoxState.Airborne;
            }
            else
            {
                hitBox.State = HitBoxState.Surfaced;
            }

            if (moveY && !result.HasFlag(MoveResult.Died))
            {
                aabb.Position.Y += interpolation.Y;
            }

            var reverse = CollisionDetection.CanMove(aabb.Bounds, -interpolation, DetectionType.Retrace);

            if (moveX)             // fix issue when moving on X axis to the left.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeX) && interpolation.X > 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveX) && interpolation.X < 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
            }

            if (moveY)             // fix issue when hitting an impassable on Y axis when jumping.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeY) && interpolation.Y > 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveY) && interpolation.Y < 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
            }

            return(result);
        }