Allow shields to stack.
Refactor Ball.render to keep a local instance of BrickBuster
This commit is contained in:
parent
a199c82fd8
commit
70ece88a89
@ -37,7 +37,7 @@ public class BrickBuster extends ApplicationAdapter {
|
|||||||
private ArrayList<Brick> bricks;
|
private ArrayList<Brick> bricks;
|
||||||
private ArrayList<PowerUp> powerUps;
|
private ArrayList<PowerUp> powerUps;
|
||||||
|
|
||||||
private boolean shieldActive = false;
|
private int shieldCount = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create () {
|
public void create () {
|
||||||
@ -88,10 +88,10 @@ public class BrickBuster extends ApplicationAdapter {
|
|||||||
}
|
}
|
||||||
paddle.render(sr);
|
paddle.render(sr);
|
||||||
|
|
||||||
if (shieldActive) {
|
if (getShieldCount() > 0) {
|
||||||
sr.begin(ShapeRenderer.ShapeType.Filled);
|
sr.begin(ShapeRenderer.ShapeType.Filled);
|
||||||
sr.setColor(Color.SALMON);
|
sr.setColor(Color.SALMON);
|
||||||
sr.rect(0, 0, WIDTH, SHIELD_HEIGHT);
|
sr.rect(0, 0, WIDTH, getShieldCount() * SHIELD_HEIGHT);
|
||||||
sr.end();
|
sr.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,12 +157,18 @@ public class BrickBuster extends ApplicationAdapter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setShieldActive(boolean shieldActive) {
|
public int getShieldCount() {
|
||||||
this.shieldActive = shieldActive;
|
return shieldCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShieldActive() {
|
public void addShield() {
|
||||||
return shieldActive;
|
shieldCount++;
|
||||||
|
paddle.setY(paddle.getY() + SHIELD_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeShield() {
|
||||||
|
shieldCount--;
|
||||||
|
paddle.setY(paddle.getY() - SHIELD_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Ball> getBalls() {
|
public ArrayList<Ball> getBalls() {
|
||||||
|
@ -26,7 +26,7 @@ public class Ball extends Entity {
|
|||||||
private boolean isDead = false;
|
private boolean isDead = false;
|
||||||
|
|
||||||
public Ball(BrickBuster brickBuster) {
|
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;
|
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));
|
Vector2 new_pos = getPos().cpy().add(direction.cpy().scl(speed * dt));
|
||||||
|
|
||||||
boolean brickCollision = false;
|
boolean brickCollision = false;
|
||||||
Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator();
|
Iterator<Brick> brickIterator = game.getBricks().iterator();
|
||||||
while (!brickCollision && brickIterator.hasNext()) {
|
while (!brickCollision && brickIterator.hasNext()) {
|
||||||
Brick brick = brickIterator.next();
|
Brick brick = brickIterator.next();
|
||||||
Vector2[] vertices = brick.getVertices();
|
Vector2[] vertices = brick.getVertices();
|
||||||
@ -76,20 +78,21 @@ public class Ball extends Entity {
|
|||||||
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
|
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
|
||||||
} else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) {
|
} else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) {
|
||||||
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
|
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);
|
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
|
||||||
getBrickBuster().setShieldActive(false);
|
game.removeShield();
|
||||||
} else if (new_pos.y + RADIUS < 0) {
|
} else if (new_pos.y + RADIUS < 0) {
|
||||||
isDead = true;
|
isDead = true;
|
||||||
return;
|
return;
|
||||||
} else if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) {
|
} else if (direction.y < 0 && new_pos.y <= game.getPaddle().getY() + Paddle.PADDLE_HEIGHT + RADIUS) {
|
||||||
Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge();
|
Pair<Vector2, Vector2> paddle = game.getPaddle().getTopEdge();
|
||||||
Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
|
Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
|
||||||
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy());
|
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy());
|
||||||
|
|
||||||
if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
|
if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
|
||||||
paddleCollision();
|
paddleCollision();
|
||||||
if (getBrickBuster().getPaddle().isSticky()) {
|
if (game.getPaddle().isSticky()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +123,7 @@ public class Ball extends Entity {
|
|||||||
Paddle paddle = getBrickBuster().getPaddle();
|
Paddle paddle = getBrickBuster().getPaddle();
|
||||||
if (paddle.isSticky()) {
|
if (paddle.isSticky()) {
|
||||||
isStuck = true;
|
isStuck = true;
|
||||||
setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
|
setY(paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
direction = paddleReflectAngle();
|
direction = paddleReflectAngle();
|
||||||
|
@ -17,8 +17,8 @@ public class Paddle extends Entity {
|
|||||||
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 int width = DEFAULT_WIDTH;
|
private int width = DEFAULT_WIDTH;
|
||||||
|
private boolean sticky = false;
|
||||||
|
|
||||||
public Paddle(BrickBuster brickBuster) {
|
public Paddle(BrickBuster brickBuster) {
|
||||||
super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y);
|
super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y);
|
||||||
@ -73,6 +73,10 @@ public class Paddle extends Entity {
|
|||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setWidth(int width) {
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isSticky() {
|
public boolean isSticky() {
|
||||||
return sticky;
|
return sticky;
|
||||||
}
|
}
|
||||||
@ -81,7 +85,7 @@ public class Paddle extends Entity {
|
|||||||
this.sticky = sticky;
|
this.sticky = sticky;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWidth(int width) {
|
|
||||||
this.width = width;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ public class ShieldPowerUp extends PowerUp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void activate() {
|
public void activate() {
|
||||||
getBrickBuster().setShieldActive(true);
|
getBrickBuster().addShield();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user