diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index a9b08a1..e2d1410 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -27,20 +27,20 @@ public class BrickBuster extends ApplicationAdapter { private BitmapFont font; private SpriteBatch batch; - private Ball ball; private Paddle paddle; + private ArrayList balls; private ArrayList bricks; private ArrayList 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(); + bricks = new ArrayList(); 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(); + 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 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 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 getBalls() { + return balls; } public Paddle getPaddle() { @@ -136,4 +148,4 @@ public class BrickBuster extends ApplicationAdapter { this.powerUps.add(powerUp); } -} +} \ No newline at end of file diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 429730b..5abf268 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -1,5 +1,6 @@ package com.me.brickbuster.entity; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; @@ -20,10 +21,12 @@ public class Ball extends Entity { public Vector2 direction; private float speed; - private boolean isStuck = false; + private boolean isStuck = true; + private boolean isDead = false; public Ball(BrickBuster brickBuster) { super(brickBuster,BrickBuster.WIDTH/2, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); + this.speed = getBrickBuster().getBricks().size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : DEFAULT_SPEED; } @Override @@ -36,8 +39,12 @@ public class Ball extends Entity { @Override public void update(float dt) { - if (!getBrickBuster().isPlaying() || isStuck()) { - return; + if (isStuck || isDead) { + if (!isDead && Gdx.input.justTouched()) { + launch(); + } else { + return; + } } Vector2 new_pos = getPos().cpy().add(direction.cpy().scl(speed * dt)); @@ -71,7 +78,7 @@ public class Ball extends Entity { } else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) { Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE); } else if (new_pos.y - RADIUS < 0) { - reset(); + isDead = true; return; } @@ -101,13 +108,12 @@ public class Ball extends Entity { public void launch() { if (getBrickBuster().getPaddle().isSticky()) { direction = paddleReflectAngle(); - isStuck = false; } else { // launch at random angle between 135 and 45 degrees float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); - speed = DEFAULT_SPEED; } + isStuck = false; } public void paddleCollision() { @@ -120,17 +126,12 @@ public class Ball extends Entity { direction = paddleReflectAngle(); } - public void reset() { - Paddle paddle = getBrickBuster().getPaddle(); - setX(paddle.getX()); - setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); - paddle.setSticky(false); - paddle.setWidth(Paddle.DEFAULT_WIDTH); - getBrickBuster().setPlaying(false); - } public boolean isStuck() { return isStuck; } + public boolean isDead() { + return isDead; + } } diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 0c451af..28a18a8 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -33,15 +33,17 @@ public class Paddle extends Entity { @Override public void update(float dt) { - Ball ball = getBrickBuster().getBall(); if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { if (getX() - width/2 - PADDLE_SPEED * dt < 0) { setX(width/2); return; } setX(getX() - PADDLE_SPEED * dt); - if (!getBrickBuster().isPlaying() || ball.isStuck()) { - ball.setX(ball.getX() - PADDLE_SPEED * dt); + + for (Ball ball : getBrickBuster().getBalls()) { + if (ball.isStuck()) { + ball.setX(ball.getX() - PADDLE_SPEED * dt); + } } } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { @@ -50,8 +52,11 @@ public class Paddle extends Entity { return; } setX(getX() + PADDLE_SPEED * dt); - if (!getBrickBuster().isPlaying() || ball.isStuck()) { - ball.setX(ball.getX() + PADDLE_SPEED * dt); + + for (Ball ball : getBrickBuster().getBalls()) { + if (ball.isStuck()) { + ball.setX(ball.getX() + PADDLE_SPEED * dt); + } } } }