Refactor to support multiple balls

This commit is contained in:
2018-11-12 18:12:42 +04:00
parent ed8c90be1a
commit 83e270062b
3 changed files with 58 additions and 40 deletions

View File

@ -27,20 +27,20 @@ public class BrickBuster extends ApplicationAdapter {
private BitmapFont font;
private SpriteBatch batch;
private Ball ball;
private Paddle paddle;
private ArrayList<Ball> balls;
private ArrayList<Brick> bricks;
private ArrayList<PowerUp> powerUps;
private boolean playing = false;
@Override
public void create () {
font = new BitmapFont();
batch = new SpriteBatch();
ball = new Ball(this);
paddle = new Paddle(this);
powerUps = new ArrayList<PowerUp>();
bricks = new ArrayList<Brick>();
for (int col = 0; col < 13; col++) {
for (int row = 0; row < 7; row++) {
@ -56,6 +56,9 @@ public class BrickBuster extends ApplicationAdapter {
bricks.add(new Brick(this, powerUpType, x, HEIGHT - y));
}
}
balls = new ArrayList<Ball>();
balls.add(new Ball(this));
}
@Override
@ -74,7 +77,9 @@ public class BrickBuster extends ApplicationAdapter {
for (PowerUp powerUp : powerUps) {
powerUp.render();
}
ball.render();
for (Ball ball : balls) {
ball.render();
}
paddle.render();
long finish_render = System.nanoTime() - start_render;
@ -86,13 +91,19 @@ public class BrickBuster extends ApplicationAdapter {
}
public void update(float dt) {
if (Gdx.input.justTouched() && (!isPlaying() || ball.isStuck())) {
playing = true;
ball.launch();
for (Iterator<Ball> it = balls.iterator(); it.hasNext();) {
Ball ball = it.next();
ball.update(dt);
if (ball.isDead()) {
it.remove();
}
}
ball.update(dt);
paddle.update(dt);
if (balls.isEmpty()) {
reset();
}
paddle.update(dt);
for (Iterator<PowerUp> it = powerUps.iterator(); it.hasNext();) {
PowerUp powerUp = it.next();
@ -104,24 +115,25 @@ public class BrickBuster extends ApplicationAdapter {
if (getBricks().isEmpty()) {
create();
playing = false;
}
}
public void reset() {
Paddle paddle = getPaddle();
Ball ball = new Ball(this);
ball.setX(paddle.getX());
ball.setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + Ball.RADIUS);
balls.add(ball);
paddle.setSticky(false);
paddle.setWidth(Paddle.DEFAULT_WIDTH);
}
@Override
public void dispose () {
}
public boolean isPlaying() {
return playing;
}
public void setPlaying(boolean playing) {
this.playing = playing;
}
public Ball getBall() {
return ball;
public ArrayList<Ball> getBalls() {
return balls;
}
public Paddle getPaddle() {
@ -136,4 +148,4 @@ public class BrickBuster extends ApplicationAdapter {
this.powerUps.add(powerUp);
}
}
}