Added longer paddle powerup, fixed issues with sticky paddle bouncing

This commit is contained in:
BlueNutterfly 2018-11-12 11:02:26 +04:00
parent 1f9c2e6db0
commit e8901d2ed4
4 changed files with 56 additions and 16 deletions

View File

@ -39,6 +39,9 @@ public class BrickBuster extends ApplicationAdapter {
if (MathUtils.randomBoolean(0.08f)) { if (MathUtils.randomBoolean(0.08f)) {
powerUpType = GluePowerUp.class; powerUpType = GluePowerUp.class;
} }
else if (MathUtils.randomBoolean(0.25f)) {
powerUpType = LongerPaddlePowerUp.class;
}
bricks.add(new Brick(this, powerUpType, x, HEIGHT - y)); bricks.add(new Brick(this, powerUpType, x, HEIGHT - y));
} }
} }

View File

@ -88,9 +88,20 @@ public class Ball extends Entity {
getPos().add(direction.cpy().scl(speed * dt)); 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() { public void launch() {
float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; if (getBrickBuster().getPaddle().isSticky()) {
direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); 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; speed = DEFAULT_SPEED;
isStuck = false; isStuck = false;
} }
@ -103,16 +114,15 @@ public class Ball extends Entity {
setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
return; return;
} }
float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2; direction = paddleReflectAngle();
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));
} }
public void reset() { 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); setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
getBrickBuster().getPaddle().setSticky(false); paddle.setSticky(false);
paddle.setWidth(Paddle.DEFAULT_WIDTH);
getBrickBuster().setPlaying(false); getBrickBuster().setPlaying(false);
} }

View File

@ -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);
}
}
}

View File

@ -11,22 +11,23 @@ import net.dermetfan.utils.Pair;
public class Paddle extends Entity { public class Paddle extends Entity {
public static final Color PADDLE_COLOR = Color.BLACK; 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_HEIGHT = 10;
public static final int PADDLE_Y = 15; public static final int PADDLE_Y = 15;
public static final int PADDLE_SPEED = 375; public static final int PADDLE_SPEED = 375;
private boolean sticky = false; private boolean sticky = false;
private int width = DEFAULT_WIDTH;
public Paddle(BrickBuster brickBuster) { public Paddle(BrickBuster brickBuster) {
super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y); super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y);
} }
@Override @Override
public void render() { public void render() {
getShapeRenderer().begin(ShapeType.Filled); getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(isSticky()? Color.GRAY : PADDLE_COLOR); getShapeRenderer().setColor(sticky? Color.GRAY : PADDLE_COLOR);
getShapeRenderer().rect(getX(), getY(), PADDLE_WIDTH, PADDLE_HEIGHT); getShapeRenderer().rect(getX() - width/2, getY(), width, PADDLE_HEIGHT);
getShapeRenderer().end(); getShapeRenderer().end();
} }
@ -34,7 +35,7 @@ public class Paddle extends Entity {
public void update(float dt) { public void update(float dt) {
Ball ball = getBrickBuster().getBall(); Ball ball = getBrickBuster().getBall();
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
if (getX() - PADDLE_SPEED * dt < 0) { if (getX() - width/2 - PADDLE_SPEED * dt < 0) {
return; return;
} }
setX(getX() - PADDLE_SPEED * dt); setX(getX() - PADDLE_SPEED * dt);
@ -43,7 +44,7 @@ public class Paddle extends Entity {
} }
} }
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { 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; return;
} }
setX(getX() + PADDLE_SPEED * dt); setX(getX() + PADDLE_SPEED * dt);
@ -55,11 +56,15 @@ public class Paddle extends Entity {
public Pair<Vector2, Vector2> getTopEdge() { public Pair<Vector2, Vector2> getTopEdge() {
return new Pair<Vector2, Vector2>( return new Pair<Vector2, Vector2>(
new Vector2(getX(), getY() + PADDLE_HEIGHT), new Vector2(getX() - width/2, getY() + PADDLE_HEIGHT),
new Vector2(getX() + PADDLE_WIDTH, getY() + PADDLE_HEIGHT) new Vector2(getX() + width/2, getY() + PADDLE_HEIGHT)
); );
} }
public int getWidth() {
return width;
}
public boolean isSticky() { public boolean isSticky() {
return sticky; return sticky;
} }
@ -67,4 +72,8 @@ public class Paddle extends Entity {
public void setSticky(boolean sticky) { public void setSticky(boolean sticky) {
this.sticky = sticky; this.sticky = sticky;
} }
public void setWidth(int width) {
this.width = width;
}
} }