diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index faa335d..e26f994 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -39,6 +39,9 @@ public class BrickBuster extends ApplicationAdapter { if (MathUtils.randomBoolean(0.08f)) { powerUpType = GluePowerUp.class; } + else if (MathUtils.randomBoolean(0.25f)) { + powerUpType = LongerPaddlePowerUp.class; + } bricks.add(new Brick(this, powerUpType, x, HEIGHT - y)); } } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 74f05b7..97a0daa 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -88,9 +88,20 @@ public class Ball extends Entity { getPos().add(direction.cpy().scl(speed * dt)); } + public Vector2 paddleReflectAngle() { + Paddle paddle = getBrickBuster().getPaddle(); + float rel = MathUtils.clamp((getX() - paddle.getX()) + (paddle.getWidth()/2), 5, paddle.getWidth()-5); + float newAngle = MathUtils.PI - (MathUtils.PI * (rel / paddle.getWidth())); + return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); + } + public void launch() { - float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; - direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); + if (getBrickBuster().getPaddle().isSticky()) { + direction = paddleReflectAngle(); + } else { + float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; + direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); + } speed = DEFAULT_SPEED; isStuck = false; } @@ -103,16 +114,15 @@ public class Ball extends Entity { setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); return; } - float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2; - float rel = MathUtils.clamp((getX() - paddleCenter) + 50, 5, 95); - float newAngle = MathUtils.PI - (MathUtils.PI * (rel/100)); - direction = new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); + direction = paddleReflectAngle(); } public void reset() { - setX(getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2); + Paddle paddle = getBrickBuster().getPaddle(); + setX(paddle.getX()); setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); - getBrickBuster().getPaddle().setSticky(false); + paddle.setSticky(false); + paddle.setWidth(Paddle.DEFAULT_WIDTH); getBrickBuster().setPlaying(false); } diff --git a/core/src/com/me/brickbuster/entity/LongerPaddlePowerUp.java b/core/src/com/me/brickbuster/entity/LongerPaddlePowerUp.java new file mode 100644 index 0000000..d2ad861 --- /dev/null +++ b/core/src/com/me/brickbuster/entity/LongerPaddlePowerUp.java @@ -0,0 +1,18 @@ +package com.me.brickbuster.entity; + +import com.badlogic.gdx.graphics.Color; +import com.me.brickbuster.BrickBuster; + +public class LongerPaddlePowerUp extends PowerUp { + public LongerPaddlePowerUp(BrickBuster brickBuster, Brick brick) { + super(brickBuster, brick, Color.OLIVE); + } + + @Override + public void activate() { + Paddle paddle = getBrickBuster().getPaddle(); + if (paddle.getWidth() < 250) { + paddle.setWidth(paddle.getWidth() + 50); + } + } +} diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 820d485..2516a3b 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -11,22 +11,23 @@ import net.dermetfan.utils.Pair; public class Paddle extends Entity { public static final Color PADDLE_COLOR = Color.BLACK; - public static final int PADDLE_WIDTH = 100; + public static final int DEFAULT_WIDTH = 100; public static final int PADDLE_HEIGHT = 10; public static final int PADDLE_Y = 15; public static final int PADDLE_SPEED = 375; private boolean sticky = false; + private int width = DEFAULT_WIDTH; public Paddle(BrickBuster brickBuster) { - super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y); + super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y); } @Override public void render() { getShapeRenderer().begin(ShapeType.Filled); - getShapeRenderer().setColor(isSticky()? Color.GRAY : PADDLE_COLOR); - getShapeRenderer().rect(getX(), getY(), PADDLE_WIDTH, PADDLE_HEIGHT); + getShapeRenderer().setColor(sticky? Color.GRAY : PADDLE_COLOR); + getShapeRenderer().rect(getX() - width/2, getY(), width, PADDLE_HEIGHT); getShapeRenderer().end(); } @@ -34,7 +35,7 @@ public class Paddle extends Entity { public void update(float dt) { Ball ball = getBrickBuster().getBall(); if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - if (getX() - PADDLE_SPEED * dt < 0) { + if (getX() - width/2 - PADDLE_SPEED * dt < 0) { return; } setX(getX() - PADDLE_SPEED * dt); @@ -43,7 +44,7 @@ public class Paddle extends Entity { } } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - if (getX() + PADDLE_SPEED * dt + PADDLE_WIDTH > BrickBuster.WIDTH) { + if (getX() + width/2 + PADDLE_SPEED * dt > BrickBuster.WIDTH) { return; } setX(getX() + PADDLE_SPEED * dt); @@ -55,11 +56,15 @@ public class Paddle extends Entity { public Pair getTopEdge() { return new Pair( - new Vector2(getX(), getY() + PADDLE_HEIGHT), - new Vector2(getX() + PADDLE_WIDTH, getY() + PADDLE_HEIGHT) + new Vector2(getX() - width/2, getY() + PADDLE_HEIGHT), + new Vector2(getX() + width/2, getY() + PADDLE_HEIGHT) ); } + public int getWidth() { + return width; + } + public boolean isSticky() { return sticky; } @@ -67,4 +72,8 @@ public class Paddle extends Entity { public void setSticky(boolean sticky) { this.sticky = sticky; } + + public void setWidth(int width) { + this.width = width; + } }