Refactor to support multiple balls

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

View File

@ -1,5 +1,6 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
@ -20,10 +21,12 @@ public class Ball extends Entity {
public Vector2 direction; public Vector2 direction;
private float speed; private float speed;
private boolean isStuck = false; private boolean isStuck = true;
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, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
this.speed = getBrickBuster().getBricks().size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : DEFAULT_SPEED;
} }
@Override @Override
@ -36,9 +39,13 @@ public class Ball extends Entity {
@Override @Override
public void update(float dt) { public void update(float dt) {
if (!getBrickBuster().isPlaying() || isStuck()) { if (isStuck || isDead) {
if (!isDead && Gdx.input.justTouched()) {
launch();
} else {
return; return;
} }
}
Vector2 new_pos = getPos().cpy().add(direction.cpy().scl(speed * dt)); 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) { } 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 < 0) { } else if (new_pos.y - RADIUS < 0) {
reset(); isDead = true;
return; return;
} }
@ -101,13 +108,12 @@ public class Ball extends Entity {
public void launch() { public void launch() {
if (getBrickBuster().getPaddle().isSticky()) { if (getBrickBuster().getPaddle().isSticky()) {
direction = paddleReflectAngle(); direction = paddleReflectAngle();
isStuck = false;
} else { } else {
// launch at random angle between 135 and 45 degrees // launch at random angle between 135 and 45 degrees
float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4;
direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle));
speed = DEFAULT_SPEED;
} }
isStuck = false;
} }
public void paddleCollision() { public void paddleCollision() {
@ -120,17 +126,12 @@ public class Ball extends Entity {
direction = paddleReflectAngle(); 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() { public boolean isStuck() {
return isStuck; return isStuck;
} }
public boolean isDead() {
return isDead;
}
} }

View File

@ -33,28 +33,33 @@ public class Paddle extends Entity {
@Override @Override
public void update(float dt) { public void update(float dt) {
Ball ball = getBrickBuster().getBall();
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
if (getX() - width/2 - PADDLE_SPEED * dt < 0) { if (getX() - width/2 - PADDLE_SPEED * dt < 0) {
setX(width/2); setX(width/2);
return; return;
} }
setX(getX() - PADDLE_SPEED * dt); setX(getX() - PADDLE_SPEED * dt);
if (!getBrickBuster().isPlaying() || ball.isStuck()) {
for (Ball ball : getBrickBuster().getBalls()) {
if (ball.isStuck()) {
ball.setX(ball.getX() - PADDLE_SPEED * dt); ball.setX(ball.getX() - PADDLE_SPEED * dt);
} }
} }
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
if (getX() + width/2 + PADDLE_SPEED * dt > BrickBuster.WIDTH) { if (getX() + width/2 + PADDLE_SPEED * dt > BrickBuster.WIDTH) {
setX(BrickBuster.WIDTH - width/2); setX(BrickBuster.WIDTH - width/2);
return; return;
} }
setX(getX() + PADDLE_SPEED * dt); setX(getX() + PADDLE_SPEED * dt);
if (!getBrickBuster().isPlaying() || ball.isStuck()) {
for (Ball ball : getBrickBuster().getBalls()) {
if (ball.isStuck()) {
ball.setX(ball.getX() + PADDLE_SPEED * dt); ball.setX(ball.getX() + PADDLE_SPEED * dt);
} }
} }
} }
}
public Pair<Vector2, Vector2> getTopEdge() { public Pair<Vector2, Vector2> getTopEdge() {
return new Pair<Vector2, Vector2>( return new Pair<Vector2, Vector2>(