Exemplo n.º 1
0
    private void BouncePhase()
    {
        Vector2 colliderCenter          = new Vector2(transform.position.x, transform.position.y) + boxCollider.offset;
        Vector2 bottomLeftCorner        = new Vector2(colliderCenter.x - (boxCollider.size.x / 2), colliderCenter.y - (boxCollider.size.y / 2));
        Vector2 bottomRightCorner       = new Vector2(colliderCenter.x + (boxCollider.size.x / 2), colliderCenter.y - (boxCollider.size.y / 2));
        Vector2 topLeftCorner           = new Vector2(colliderCenter.x - (boxCollider.size.x / 2), colliderCenter.y + (boxCollider.size.y / 2));
        Vector2 topRightCorner          = new Vector2(colliderCenter.x + (boxCollider.size.x / 2), colliderCenter.y + (boxCollider.size.y / 2));
        float   verticalDistanceCheck   = boxCollider.size.y / 8;
        float   horizontalDistanceCheck = boxCollider.size.x / 8;

        // check vertical bounces
        if (currentVelocity.y < 0)
        {
            if (MovementUtilities.Box2DObstaclePresent(bottomLeftCorner, Vector2.down, verticalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(colliderCenter + (Vector2.down * boxCollider.size.y / 2), Vector2.down, verticalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(bottomRightCorner, Vector2.down, verticalDistanceCheck))
            {
                currentVelocity.y = -currentVelocity.y;
            }
        }
        else if (currentVelocity.y > 0)
        {
            if (MovementUtilities.Box2DObstaclePresent(topLeftCorner, Vector2.up, verticalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(colliderCenter + (Vector2.up * boxCollider.size.y / 2), Vector2.up, verticalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(topRightCorner, Vector2.up, verticalDistanceCheck))
            {
                currentVelocity.y = -currentVelocity.y;
            }
        }

        // check horizontal bounces
        if (currentVelocity.x < 0)
        {
            if (MovementUtilities.Box2DObstaclePresent(topLeftCorner, Vector2.left, horizontalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(colliderCenter + (Vector2.left * boxCollider.size.x / 2), Vector2.left, horizontalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(bottomLeftCorner, Vector2.left, horizontalDistanceCheck))
            {
                currentVelocity.x = -currentVelocity.x;
            }
        }
        else if (currentVelocity.x > 0)
        {
            if (MovementUtilities.Box2DObstaclePresent(topRightCorner, Vector2.right, horizontalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(colliderCenter + (Vector2.right * boxCollider.size.x / 2), Vector2.right, horizontalDistanceCheck) ||
                MovementUtilities.Box2DObstaclePresent(bottomRightCorner, Vector2.right, horizontalDistanceCheck))
            {
                currentVelocity.x = -currentVelocity.x;
            }
        }

        this.transform.Translate(currentVelocity * Time.deltaTime);
    }