Allow shields to stack.

Refactor Ball.render to keep a local instance of BrickBuster
This commit is contained in:
Matt Low 2018-11-12 21:46:02 +04:00
parent a199c82fd8
commit 70ece88a89
4 changed files with 33 additions and 20 deletions

View File

@ -37,7 +37,7 @@ public class BrickBuster extends ApplicationAdapter {
private ArrayList<Brick> bricks;
private ArrayList<PowerUp> powerUps;
private boolean shieldActive = false;
private int shieldCount = 0;
@Override
public void create () {
@ -88,10 +88,10 @@ public class BrickBuster extends ApplicationAdapter {
}
paddle.render(sr);
if (shieldActive) {
if (getShieldCount() > 0) {
sr.begin(ShapeRenderer.ShapeType.Filled);
sr.setColor(Color.SALMON);
sr.rect(0, 0, WIDTH, SHIELD_HEIGHT);
sr.rect(0, 0, WIDTH, getShieldCount() * SHIELD_HEIGHT);
sr.end();
}
@ -157,12 +157,18 @@ public class BrickBuster extends ApplicationAdapter {
return null;
}
public void setShieldActive(boolean shieldActive) {
this.shieldActive = shieldActive;
public int getShieldCount() {
return shieldCount;
}
public boolean isShieldActive() {
return shieldActive;
public void addShield() {
shieldCount++;
paddle.setY(paddle.getY() + SHIELD_HEIGHT);
}
public void removeShield() {
shieldCount--;
paddle.setY(paddle.getY() - SHIELD_HEIGHT);
}
public ArrayList<Ball> getBalls() {

View File

@ -26,7 +26,7 @@ public class Ball extends Entity {
private boolean isDead = false;
public Ball(BrickBuster brickBuster) {
super(brickBuster,BrickBuster.WIDTH/2, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
super(brickBuster,BrickBuster.WIDTH/2, brickBuster.getPaddle().getY() + Paddle.PADDLE_HEIGHT + RADIUS);
this.speed = getBrickBuster().getBricks().size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED;
}
@ -48,10 +48,12 @@ public class Ball extends Entity {
}
}
BrickBuster game = getBrickBuster();
Vector2 new_pos = getPos().cpy().add(direction.cpy().scl(speed * dt));
boolean brickCollision = false;
Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator();
Iterator<Brick> brickIterator = game.getBricks().iterator();
while (!brickCollision && brickIterator.hasNext()) {
Brick brick = brickIterator.next();
Vector2[] vertices = brick.getVertices();
@ -76,20 +78,21 @@ public class Ball extends Entity {
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
} else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) {
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
} else if (new_pos.y - RADIUS < BrickBuster.SHIELD_HEIGHT && getBrickBuster().isShieldActive()) {
} else if (game.getShieldCount() > 0
&& new_pos.y - RADIUS < BrickBuster.SHIELD_HEIGHT * game.getShieldCount()) {
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
getBrickBuster().setShieldActive(false);
game.removeShield();
} else if (new_pos.y + RADIUS < 0) {
isDead = true;
return;
} else if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) {
Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge();
} else if (direction.y < 0 && new_pos.y <= game.getPaddle().getY() + Paddle.PADDLE_HEIGHT + RADIUS) {
Pair<Vector2, Vector2> paddle = game.getPaddle().getTopEdge();
Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy());
if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
paddleCollision();
if (getBrickBuster().getPaddle().isSticky()) {
if (game.getPaddle().isSticky()) {
return;
}
}
@ -120,7 +123,7 @@ public class Ball extends Entity {
Paddle paddle = getBrickBuster().getPaddle();
if (paddle.isSticky()) {
isStuck = true;
setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
setY(paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
return;
}
direction = paddleReflectAngle();

View File

@ -17,8 +17,8 @@ public class Paddle extends Entity {
public static final int PADDLE_Y = 15;
public static final int PADDLE_SPEED = 375;
private boolean sticky = false;
private int width = DEFAULT_WIDTH;
private boolean sticky = false;
public Paddle(BrickBuster brickBuster) {
super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y);
@ -73,6 +73,10 @@ public class Paddle extends Entity {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public boolean isSticky() {
return sticky;
}
@ -81,7 +85,7 @@ public class Paddle extends Entity {
this.sticky = sticky;
}
public void setWidth(int width) {
this.width = width;
}
}

View File

@ -12,7 +12,7 @@ public class ShieldPowerUp extends PowerUp {
@Override
public void activate() {
getBrickBuster().setShieldActive(true);
getBrickBuster().addShield();
}
}