Compare commits

..

12 Commits

Author SHA1 Message Date
e4000998cc Complete box2d implementation, all collisions handled with box2d
Shield re-implemented as an entity with a box2d body
Multiball powerup now spawns balls at paddle instead of from block location
2018-11-15 10:34:22 +04:00
92201411b9 Refactor entity.PhysicsBody to physics.PhysicsBody
Create CollisionListener interface and Box2dContactListener
Register the Box2dContactListener with the box2d world
2018-11-14 22:26:02 +04:00
0994289c7e Begin box2d physics implementation.
Created box2d bodies for all game entities (except shield)
Scale units down to box2d friendly units (40:1 <-> pixels:box2d)
Use floats in places integers were used for position and sizing
2018-11-14 22:04:41 +04:00
fe2c68d39a Add FPS limit back 2018-11-13 22:55:39 +04:00
b3cbe0cda5 Remove magic value 2018-11-13 19:01:14 +04:00
e2c0bad9e0 Altered powerup weights (decreased glue, increased multi ball)
Allow bricks to have their colour changed.
2018-11-13 18:58:55 +04:00
ea6240bd2c Add PowerUpType enum
Handles weighted random seletion, color and instantiation, cleaning up
other sections of Brick and PlayState code.
2018-11-13 18:42:27 +04:00
0abae880d3 Add MenuState, start with it and switch back when a game is finished 2018-11-13 17:58:42 +04:00
fa7e93953b Speed ball up 2018-11-13 17:21:23 +04:00
409735d94e Scale entity units to new board units.
Rename Brick.BLOCK_ to Brick.BRICK_
2018-11-13 17:17:19 +04:00
1fe82cb5b2 Change viewport to FitViewport, board height/width to 4K
Enable font anti-aliasing
2018-11-13 17:14:33 +04:00
a56d721050 Move away from using pixel coordinates to "board coordinates"
Changed aspect ratio from 4:3 to 9:16
2018-11-13 15:58:01 +04:00
21 changed files with 768 additions and 232 deletions

View File

@ -46,6 +46,7 @@ project(":desktop") {
compile project(":core") compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
} }
} }
@ -56,7 +57,9 @@ project(":core") {
dependencies { dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "net.dermetfan.libgdx-utils:libgdx-utils:0.13.4" compile "net.dermetfan.libgdx-utils:libgdx-utils:0.13.4"
compile "net.dermetfan.libgdx-utils:libgdx-utils-box2d:0.13.4"
} }
} }

BIN
core/assets/playBtn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -3,16 +3,24 @@ package com.me.brickbuster;
import com.badlogic.gdx.Game; import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.me.brickbuster.state.PlayState; import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.me.brickbuster.state.MenuState;
public class BrickBuster extends Game { public class BrickBuster extends Game {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Brick Buster"; public static final String TITLE = "Brick Buster";
// 9*16 board area
public static final int BOARD_WIDTH = 2160;
public static final int BOARD_HEIGHT = 3840;
public OrthographicCamera cam;
public Viewport viewport;
public BitmapFont font; public BitmapFont font;
public SpriteBatch sb; public SpriteBatch sb;
@ -20,11 +28,18 @@ public class BrickBuster extends Game {
@Override @Override
public void create () { public void create () {
cam = new OrthographicCamera();
viewport = new FitViewport(BOARD_WIDTH, BOARD_HEIGHT, cam);
viewport.apply(true);
font = new BitmapFont(); font = new BitmapFont();
font.getData().setScale(4);
font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
sb = new SpriteBatch(); sb = new SpriteBatch();
sr = new ShapeRenderer(); sr = new ShapeRenderer();
setScreen(new PlayState(this)); setScreen(new MenuState(this));
} }
@Override @Override
@ -35,4 +50,14 @@ public class BrickBuster extends Game {
super.render(); super.render();
} }
@Override
public void resize(int width, int height) {
viewport.update(width, height);
sb.setProjectionMatrix(cam.combined);
sr.setProjectionMatrix(cam.combined);
super.resize(width, height);
}
} }

View File

@ -6,124 +6,152 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
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;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.badlogic.gdx.physics.box2d.Body;
import com.me.brickbuster.Utils; import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.me.brickbuster.physics.CollisionListener;
import com.me.brickbuster.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair;
import java.util.Iterator; public class Ball extends Entity implements PhysicsBody, CollisionListener {
public class Ball extends Entity { public static final float RADIUS = 1.2f;
public static final int RADIUS = 12;
public static final Color BALL_COLOR = Color.CHARTREUSE; public static final Color BALL_COLOR = Color.CHARTREUSE;
public static final float DEFAULT_SPEED = 350; public static final float DEFAULT_SPEED = 45;
public static final float BOOST_SPEED = 450; public static final float BOOST_SPEED = 55;
public static final int BLOCKS_FOR_BOOST = 39; public static final int BLOCKS_FOR_BOOST = 39;
public Vector2 direction;
private float speed; private float speed;
private boolean isStuck = true; private boolean isStuck = true;
private boolean isDead = false; private boolean touchedPaddle = false;
private Body body;
public Ball(PlayState state) { public Ball(PlayState state) {
super(state,BrickBuster.WIDTH/2, state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); super(state, state.paddle.getX(), state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
this.speed = state.bricks.size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED; this.speed = state.bricks.size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED;
createBody();
} }
@Override @Override
public void render(ShapeRenderer sr) { public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
sr.setColor(BALL_COLOR); sr.setColor(BALL_COLOR);
sr.circle(pos.x, pos.y, RADIUS); sr.circle(pos.x * PlayState.PIXEL_PER_METER,
pos.y * PlayState.PIXEL_PER_METER,
RADIUS * PlayState.PIXEL_PER_METER);
sr.end(); sr.end();
} }
@Override @Override
public void update(float dt) { public void update(float dt) {
if (isStuck || isDead) { if (isStuck || deleted) {
if (!isDead && Gdx.input.justTouched()) { if (!deleted && Gdx.input.justTouched()) {
launch(); launch();
} else { } else {
return; return;
} }
} }
if (touchedPaddle) {
Vector2 new_pos = pos.cpy().add(direction.cpy().scl(speed * dt)); paddleCollision();
touchedPaddle = false;
boolean brickCollision = false;
Iterator<Brick> brickIterator = state.bricks.iterator();
while (!brickCollision && brickIterator.hasNext()) {
Brick brick = brickIterator.next();
Vector2[] vertices = brick.getVertices();
for(int i = 0; i < vertices.length; i++) {
Vector2 v1 = vertices[i];
Vector2 v2 = vertices[i+1 < vertices.length? i+1 : 0];
Vector2 segment = v2.cpy().sub(v1);
Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, new_pos.cpy());
if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
if (brick.hit()) {
brickIterator.remove();
}
Utils.reflect(direction, segment.nor());
brickCollision = true;
break;
}
}
} }
if (new_pos.x + RADIUS > BrickBuster.WIDTH || new_pos.x - RADIUS < 0) { if (getY() + RADIUS < 0) {
Utils.reflect(direction, Utils.VERTICAL_EDGE); deleted = true;
} else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) {
Utils.reflect(direction, Utils.HORIZONTAL_EDGE);
} else if (state.getShieldCount() > 0
&& new_pos.y - RADIUS < PlayState.SHIELD_HEIGHT * state.getShieldCount()) {
Utils.reflect(direction, Utils.HORIZONTAL_EDGE);
state.removeShield();
} else if (new_pos.y + RADIUS < 0) {
isDead = true;
return;
} else if (direction.y < 0 && new_pos.y <= state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS) {
Pair<Vector2, Vector2> paddle = state.paddle.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 (state.paddle.isSticky()) {
return;
}
}
} }
pos.add(direction.cpy().scl(speed * dt)); body.setLinearVelocity(body.getLinearVelocity().nor().scl(speed));
}
@Override
public Body getBody() {
return body;
}
@Override
public void createBody() {
BodyDef ballBody = new BodyDef();
ballBody.type = BodyDef.BodyType.DynamicBody;
ballBody.position.set(pos);
CircleShape ballShape = new CircleShape();
ballShape.setRadius(RADIUS);
FixtureDef ballFixture = new FixtureDef();
ballFixture.shape = ballShape;
ballFixture.restitution = 1f;
ballFixture.friction = 0f;
body = state.world.createBody(ballBody);
body.createFixture(ballFixture);
body.setUserData(this);
ballShape.dispose();
}
@Override
public void beginContact(Entity contacted) {
if (contacted instanceof Shield) {
contacted.delete();
}
if (contacted instanceof Paddle && !isStuck) {
touchedPaddle = true;
}
}
@Override
public void endContact(Entity contacted) {
} }
public Vector2 paddleReflectAngle() { public Vector2 paddleReflectAngle() {
float rel = MathUtils.clamp((pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2), 5, state.paddle.getWidth()-5); float trim = state.paddle.getWidth() * 0.10f;
float rel = MathUtils.clamp((pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2),
trim, state.paddle.getWidth()-trim);
float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth())); float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth()));
return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
} }
public void launch() { public void launch() {
Vector2 direction;
if (state.paddle.isSticky()) { if (state.paddle.isSticky()) {
direction = paddleReflectAngle(); direction = paddleReflectAngle();
} 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));
} }
body.setLinearVelocity(direction.scl(speed));
isStuck = false; isStuck = false;
} }
public void paddleCollision() { public void paddleCollision() {
if (state.paddle.isSticky()) { if (state.paddle.isSticky()) {
isStuck = true; isStuck = true;
pos.y = state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS; body.setLinearVelocity(new Vector2());
setY(state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
return; return;
} }
direction = paddleReflectAngle(); body.setLinearVelocity(paddleReflectAngle().scl(speed));
}
@Override
public void setX(float x) {
super.setX(x);
Vector2 bodyPos = body.getPosition();
bodyPos.x = x;
body.setTransform(bodyPos, 0);
}
@Override
public void setY(float y) {
super.setY(y);
Vector2 bodyPos = body.getPosition();
bodyPos.y = y;
body.setTransform(bodyPos, 0);
} }
public void setSpeed(float speed) { public void setSpeed(float speed) {
@ -134,15 +162,4 @@ public class Ball extends Entity {
return isStuck; return isStuck;
} }
public boolean isDead() {
return isDead;
}
public void setDirection(Vector2 direction) {
this.direction = direction;
}
public void setStuck(boolean stuck) {
isStuck = stuck;
}
} }

View File

@ -4,41 +4,96 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.powerup.PowerUp; import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.physics.CollisionListener;
import com.me.brickbuster.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class Brick extends Entity { public class Brick extends Entity implements PhysicsBody, CollisionListener {
public static final Color BLOCK_COLOR = Color.FOREST; public static final Color DEFAULT_COLOR = Color.FOREST;
public static final int BLOCK_WIDTH = 50; public static final float BRICK_WIDTH = 5f;
public static final int BLOCK_HEIGHT = 20; public static final float BRICK_HEIGHT = 2.5f;
private Class<? extends PowerUp> powerUpType; private PowerUpType powerUpType;
private Color color;
private Vector2[] vertices; private Body body;
private boolean hitByBall = false;
public Brick(PlayState state, Class<? extends PowerUp> powerUpType, int x, int y) { public Brick(PlayState state, PowerUpType powerUpType, float x, float y) {
this(state, powerUpType, true, x, y);
}
public Brick(PlayState state, PowerUpType powerUpType, boolean hidePowerup, float x, float y) {
this(state, powerUpType, DEFAULT_COLOR, hidePowerup, x, y);
}
public Brick(PlayState state, PowerUpType powerUpType, Color color, boolean hidePowerUp, float x, float y) {
super(state, x, y); super(state, x, y);
this.powerUpType = powerUpType; this.powerUpType = powerUpType;
this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color;
this.vertices = new Vector2[] { createBody();
new Vector2(x, y),
new Vector2(x + BLOCK_WIDTH, y),
new Vector2(x + BLOCK_WIDTH, y + BLOCK_HEIGHT),
new Vector2(x, y + BLOCK_HEIGHT)
};
} }
@Override @Override
public void render(ShapeRenderer sr) { public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
sr.setColor(BLOCK_COLOR); sr.setColor(color);
sr.rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT); sr.rect(getX() * PlayState.PIXEL_PER_METER,
getY() * PlayState.PIXEL_PER_METER,
BRICK_WIDTH * PlayState.PIXEL_PER_METER,
BRICK_HEIGHT * PlayState.PIXEL_PER_METER);
sr.end(); sr.end();
} }
@Override @Override
public void update(float dt) { public void update(float dt) {
if (hitByBall) {
hit();
hitByBall = false;
}
}
@Override
public Body getBody() {
return body;
}
@Override
public void createBody() {
BodyDef brickBody = new BodyDef();
brickBody.type = BodyDef.BodyType.StaticBody;
brickBody.position.set(pos.cpy());
PolygonShape brickShape = new PolygonShape();
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2,
new Vector2(BRICK_WIDTH/2,BRICK_HEIGHT/2), 0);
FixtureDef brickFixture = new FixtureDef();
brickFixture.shape = brickShape;
brickFixture.friction = 0f;
body = state.world.createBody(brickBody);
body.createFixture(brickFixture);
body.setUserData(this);
brickShape.dispose();
}
@Override
public void beginContact(Entity contacted) {
if (contacted instanceof Ball) {
hitByBall = true;
}
}
@Override
public void endContact(Entity contacted) {
} }
public boolean hit() { public boolean hit() {
@ -49,18 +104,11 @@ public class Brick extends Entity {
} }
if (powerUpType != null) { if (powerUpType != null) {
try { state.powerUps.add(powerUpType.createInstance(state, this));
PowerUp powerUp = powerUpType.getConstructor(PlayState.class, Brick.class).newInstance(state, this);
state.powerUps.add(powerUp);
} catch(Exception e) {
System.out.println("Error spawning powerup: " + e.getMessage());
}
} }
deleted = true;
return true; return true;
} }
public Vector2[] getVertices() {
return vertices;
}
} }

View File

@ -8,6 +8,11 @@ public abstract class Entity {
protected PlayState state; protected PlayState state;
protected Vector2 pos; protected Vector2 pos;
protected boolean deleted = false;
public Entity(PlayState state, Vector2 pos) {
this(state, pos.x, pos.y);
}
public Entity(PlayState state, float x, float y) { public Entity(PlayState state, float x, float y) {
this.state = state; this.state = state;
@ -42,4 +47,12 @@ public abstract class Entity {
pos.y = y; pos.y = y;
} }
public void delete() {
this.deleted = true;
}
public boolean isDeleted() {
return deleted;
}
} }

View File

@ -6,30 +6,43 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.me.brickbuster.physics.CollisionListener;
import com.me.brickbuster.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;
public class Paddle extends Entity { public class Paddle extends Entity implements PhysicsBody {
public static final Color STICKY_COLOR = Color.GRAY;
public static final Color PADDLE_COLOR = Color.BLACK; public static final Color PADDLE_COLOR = Color.BLACK;
public static final int DEFAULT_WIDTH = 100;
public static final int PADDLE_HEIGHT = 10;
public static final int PADDLE_Y = 15;
public static final int PADDLE_SPEED = 375;
private int width = DEFAULT_WIDTH; public static final float DEFAULT_WIDTH = 7.5f;
public static final float PADDLE_HEIGHT = 0.75f;
public static final float PADDLE_Y = 1.25f;
public static final float PADDLE_SPEED = 40f;
private float width = DEFAULT_WIDTH;
private boolean sticky = false; private boolean sticky = false;
private Body body;
public Paddle(PlayState brickBuster) { public Paddle(PlayState brickBuster) {
super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y); super(brickBuster, PlayState.BOARD_WIDTH/2, PADDLE_Y);
createBody();
} }
@Override @Override
public void render(ShapeRenderer sr) { public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
sr.setColor(sticky? Color.GRAY : PADDLE_COLOR); sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR);
sr.rect(getX() - width/2, getY(), width, PADDLE_HEIGHT); sr.rect((getX() - width/2) * PlayState.PIXEL_PER_METER,
getY() * PlayState.PIXEL_PER_METER,
width * PlayState.PIXEL_PER_METER,
PADDLE_HEIGHT * PlayState.PIXEL_PER_METER);
sr.end(); sr.end();
} }
@ -40,7 +53,7 @@ public class Paddle extends Entity {
setX(width/2); setX(width/2);
return; return;
} }
pos.x = pos.x - PADDLE_SPEED * dt; setX(pos.x - PADDLE_SPEED * dt);
for (Ball ball : state.balls) { for (Ball ball : state.balls) {
if (ball.isStuck()) { if (ball.isStuck()) {
@ -49,11 +62,11 @@ public class Paddle extends Entity {
} }
} }
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
if (pos.x + width/2 + PADDLE_SPEED * dt > BrickBuster.WIDTH) { if (pos.x + width/2 + PADDLE_SPEED * dt > PlayState.BOARD_WIDTH) {
setX(BrickBuster.WIDTH - width/2); setX(PlayState.BOARD_WIDTH - width/2);
return; return;
} }
pos.x = pos.x + PADDLE_SPEED * dt; setX(pos.x + PADDLE_SPEED * dt);
for (Ball ball : state.balls) { for (Ball ball : state.balls) {
if (ball.isStuck()) { if (ball.isStuck()) {
@ -63,19 +76,59 @@ public class Paddle extends Entity {
} }
} }
public Pair<Vector2, Vector2> getTopEdge() { @Override
return new Pair<Vector2, Vector2>( public void createBody() {
new Vector2(pos.x - width/2, pos.y + PADDLE_HEIGHT), BodyDef paddleBody = new BodyDef();
new Vector2(pos.x + width/2, pos.y + PADDLE_HEIGHT) paddleBody.type = BodyDef.BodyType.KinematicBody;
); paddleBody.position.set(pos);
EdgeShape paddleShape = new EdgeShape();
paddleShape.set(new Vector2(-width/2, PADDLE_HEIGHT),
new Vector2(width/2, PADDLE_HEIGHT));
FixtureDef paddleFixture = new FixtureDef();
paddleFixture.shape = paddleShape;
//paddleFixture.isSensor = true;
body = state.world.createBody(paddleBody);
body.createFixture(paddleFixture);
body.setUserData(this);
paddleShape.dispose();
} }
public int getWidth() { @Override
public Body getBody() {
return body;
}
@Override
public void setX(float x) {
super.setX(x);
Vector2 bodyPos = body.getPosition();
bodyPos.x = x;
body.setTransform(bodyPos, 0);
}
@Override
public void setY(float y) {
super.setY(y);
Vector2 bodyPos = body.getPosition();
bodyPos.y = y;
body.setTransform(bodyPos, 0);
}
public float getWidth() {
return width; return width;
} }
public void setWidth(int width) { public void setWidth(float width) {
if (this.width == width) {
return;
}
state.world.destroyBody(body);
this.width = width; this.width = width;
createBody();
} }
public boolean isSticky() { public boolean isSticky() {

View File

@ -0,0 +1,67 @@
package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState;
public class Shield extends Entity implements PhysicsBody {
public static final float SHIELD_HEIGHT = 0.75f;
private Body body;
public Shield(PlayState state) {
super(state, 0, state.getShieldCount() * SHIELD_HEIGHT);
createBody();
}
@Override
public void render(ShapeRenderer sr) {
sr.begin(ShapeRenderer.ShapeType.Filled);
sr.setColor(PowerUpType.SHIELD.getColor());
sr.rect(0, pos.y * PlayState.PIXEL_PER_METER,
PlayState.BOARD_WIDTH * PlayState.PIXEL_PER_METER,
SHIELD_HEIGHT * PlayState.PIXEL_PER_METER);
sr.end();
}
@Override
public void update(float dt) {
if (deleted) {
state.removeShield(this);
}
}
@Override
public void createBody() {
BodyDef brickBody = new BodyDef();
brickBody.type = BodyDef.BodyType.StaticBody;
brickBody.position.set(pos.cpy());
PolygonShape brickShape = new PolygonShape();
brickShape.setAsBox(PlayState.BOARD_WIDTH/2, SHIELD_HEIGHT/2,
new Vector2(PlayState.BOARD_WIDTH/2,SHIELD_HEIGHT/2), 0);
FixtureDef brickFixture = new FixtureDef();
brickFixture.shape = brickShape;
brickFixture.friction = 0f;
body = state.world.createBody(brickBody);
body.createFixture(brickFixture);
body.setUserData(this);
brickShape.dispose();
}
@Override
public Body getBody() {
return body;
}
}

View File

@ -1,13 +1,13 @@
package com.me.brickbuster.entity.powerup; package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.entity.Brick; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class GluePowerUp extends PowerUp { public class GluePowerUp extends PowerUp {
public GluePowerUp(PlayState state, Brick brick) { public GluePowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, Color.WHITE); super(state, brick, color);
} }
@Override @Override

View File

@ -1,20 +1,20 @@
package com.me.brickbuster.entity.powerup; package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.entity.Brick; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class LongerPaddlePowerUp extends PowerUp { public class LongerPaddlePowerUp extends PowerUp {
public LongerPaddlePowerUp(PlayState state, Brick brick) { public LongerPaddlePowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, Color.OLIVE); super(state, brick, color);
} }
@Override @Override
public void activate() { public void activate() {
if (state.paddle.getWidth() < 250) { if (state.paddle.getWidth() < Paddle.DEFAULT_WIDTH*2.5) {
state.paddle.setWidth(state.paddle.getWidth() + 50); state.paddle.setWidth(state.paddle.getWidth() + Paddle.DEFAULT_WIDTH/2);
} }
} }
} }

View File

@ -4,15 +4,15 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Ball; import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Brick; import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class MultiBallPowerUp extends PowerUp { public class MultiBallPowerUp extends PowerUp {
private Vector2 pos; private Vector2 pos;
public MultiBallPowerUp(PlayState state, Brick brick) { public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, Color.ROYAL); super(state, brick, color);
this.pos = getPos().cpy(); this.pos = getPos().cpy();
} }
@ -20,10 +20,7 @@ public class MultiBallPowerUp extends PowerUp {
public void activate() { public void activate() {
for (int x = 0; x < 2; x++) { for (int x = 0; x < 2; x++) {
Ball ball = new Ball(state); Ball ball = new Ball(state);
ball.setPos(pos.cpy()); ball.launch();
float angle = MathUtils.random(MathUtils.PI*2);
ball.setDirection(new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)));
ball.setStuck(false);
state.balls.add(ball); state.balls.add(ball);
} }
} }

View File

@ -4,54 +4,93 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.me.brickbuster.Utils; import com.me.brickbuster.Utils;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.entity.Entity; import com.me.brickbuster.entity.Entity;
import com.me.brickbuster.entity.Paddle;
import com.me.brickbuster.physics.CollisionListener;
import com.me.brickbuster.physics.PhysicsBody;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;
public abstract class PowerUp extends Entity { public abstract class PowerUp extends Entity implements PhysicsBody, CollisionListener {
public static final int RADIUS = 10; public static final float RADIUS = 1.2f;
public static final int FALL_SPEED = 100; public static final float FALL_SPEED = 15f;
private Color color; private Color color;
private boolean isCaught; private boolean isCaught;
public PowerUp(PlayState state, Brick brick, Color color) { private Body body;
super(state, brick.getX() + Brick.BLOCK_WIDTH/2, brick.getY() + Brick.BLOCK_HEIGHT/2);
public PowerUp(PlayState state, Vector2 pos, Color color) {
super(state, pos);
this.color = color; this.color = color;
createBody();
} }
@Override @Override
public void render(ShapeRenderer sr) { public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
sr.setColor(color); sr.setColor(color);
sr.circle(getX(), getY(), RADIUS); sr.circle(getX() * PlayState.PIXEL_PER_METER,
getY() * PlayState.PIXEL_PER_METER,
RADIUS * PlayState.PIXEL_PER_METER);
sr.end(); sr.end();
} }
@Override @Override
public void update(float dt) { public void update(float dt) {
setY(getY() - FALL_SPEED * dt); if (isCaught) {
Pair<Vector2, Vector2> paddle = state.paddle.getTopEdge();
Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, getPos().cpy());
if (nearest.dst(getX(), getY()) <= RADIUS) {
activate(); activate();
isCaught = true; delete();
} }
if (getY() + RADIUS < 0) { if (getY() + RADIUS < 0) {
delete();
}
}
@Override
public Body getBody() {
return body;
}
@Override
public void createBody() {
BodyDef ballBody = new BodyDef();
ballBody.type = BodyDef.BodyType.DynamicBody;
ballBody.position.set(pos);
CircleShape ballShape = new CircleShape();
ballShape.setRadius(RADIUS);
FixtureDef ballFixture = new FixtureDef();
ballFixture.shape = ballShape;
ballFixture.isSensor = true;
body = state.world.createBody(ballBody);
body.createFixture(ballFixture);
body.setUserData(this);
body.setLinearVelocity(0f, -FALL_SPEED);
ballShape.dispose();
}
@Override
public void beginContact(Entity contacted) {
if (contacted instanceof Paddle) {
isCaught = true; isCaught = true;
} }
} }
@Override
public void endContact(Entity contacted) {}
public abstract void activate(); public abstract void activate();
public boolean isCaught() {
return isCaught;
}
} }

View File

@ -0,0 +1,68 @@
package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.PlayState;
public enum PowerUpType {
GLUE(GluePowerUp.class, Color.WHITE, 20),
LONGER_PADDLE(LongerPaddlePowerUp.class, Color.OLIVE, 40),
MULTI_BALL(MultiBallPowerUp.class, Color.ROYAL, 30),
SHIELD(ShieldPowerUp.class, Color.SALMON, 40),
;
private static final int WEIGHT_SUM;
private Class<? extends PowerUp> cls;
private Color color;
private int weight;
PowerUpType(Class<? extends PowerUp> cls, Color color, int weight) {
this.cls = cls;
this.color = color;
this.weight = weight;
}
public Color getColor() {
return color;
}
public static PowerUpType getWeightedRandom() {
int distance = MathUtils.random(WEIGHT_SUM);
for (PowerUpType type : values()) {
distance -= type.weight;
if (distance < 0) {
return type;
}
}
return null;
}
public PowerUp createInstance(PlayState state, Brick brick) {
return createInstance(state,
new Vector2(brick.getX()+Brick.BRICK_WIDTH/2,
brick.getY()+Brick.BRICK_HEIGHT/2));
}
public PowerUp createInstance(PlayState state, Vector2 pos) {
try {
return cls.getConstructor(PlayState.class, Vector2.class, Color.class)
.newInstance(state, pos, color);
} catch(Exception e) {
System.out.println("Error instantiating PoewrUp: " + e.getMessage());
}
return null;
}
static {
int weightSum = 0;
for (PowerUpType type : values()) {
weightSum += type.weight;
}
WEIGHT_SUM = weightSum;
}
}

View File

@ -1,13 +1,13 @@
package com.me.brickbuster.entity.powerup; package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.entity.Brick; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.state.PlayState; import com.me.brickbuster.state.PlayState;
public class ShieldPowerUp extends PowerUp { public class ShieldPowerUp extends PowerUp {
public ShieldPowerUp(PlayState state, Brick brick) { public ShieldPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, Color.SALMON); super(state, brick, color);
} }
@Override @Override

View File

@ -0,0 +1,53 @@
package com.me.brickbuster.physics;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.me.brickbuster.entity.Entity;
public class Box2dContactListener implements ContactListener {
@Override
public void beginContact(Contact contact) {
Object userDataA = contact.getFixtureA().getBody().getUserData();
Object userDataB = contact.getFixtureB().getBody().getUserData();
if (userDataA == null || userDataB == null ||
!(userDataA instanceof Entity) || !(userDataB instanceof Entity)) {
return;
}
if (userDataA instanceof CollisionListener) {
((CollisionListener) userDataA).beginContact((Entity) userDataB);
}
if (userDataB instanceof CollisionListener) {
((CollisionListener) userDataB).beginContact((Entity) userDataA);
}
}
@Override
public void endContact(Contact contact) {
Object userDataA = contact.getFixtureA().getBody().getUserData();
Object userDataB = contact.getFixtureB().getBody().getUserData();
if (userDataA == null || userDataB == null ||
!(userDataA instanceof Entity) || !(userDataB instanceof Entity)) {
return;
}
if (userDataA instanceof CollisionListener) {
((CollisionListener) userDataA).endContact((Entity) userDataB);
}
if (userDataB instanceof CollisionListener) {
((CollisionListener) userDataB).endContact((Entity) userDataA);
}
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {}
}

View File

@ -0,0 +1,11 @@
package com.me.brickbuster.physics;
import com.me.brickbuster.entity.Entity;
public interface CollisionListener {
void beginContact(Entity contacted);
void endContact(Entity contacted);
}

View File

@ -0,0 +1,11 @@
package com.me.brickbuster.physics;
import com.badlogic.gdx.physics.box2d.Body;
public interface PhysicsBody {
void createBody();
Body getBody();
}

View File

@ -0,0 +1,49 @@
package com.me.brickbuster.state;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.me.brickbuster.BrickBuster;
public class MenuState extends State {
private Texture playButton;
public MenuState(BrickBuster game) {
super(game);
}
@Override
public void setup() {
this.playButton = new Texture(Gdx.files.internal("playBtn.png"));
}
@Override
public void render() {
game.sb.begin();
game.sb.draw(playButton,
BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2,
BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2);
game.sb.end();
}
@Override
public void update(float dt) {
if (Gdx.input.justTouched()) {
Vector3 clickLoc = game.cam.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
if (clickLoc.x >= BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2
&& clickLoc.x <= BrickBuster.BOARD_WIDTH/2+playButton.getWidth()/2
&& clickLoc.y >= BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2
&& clickLoc.y <= BrickBuster.BOARD_HEIGHT/2+playButton.getHeight()/2) {
game.setScreen(new PlayState(game));
dispose();
}
}
}
@Override
public void dispose() {
super.dispose();
playButton.dispose();
}
}

View File

@ -2,61 +2,125 @@ package com.me.brickbuster.state;
import com.badlogic.gdx.Gdx; 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.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.entity.Ball; import com.me.brickbuster.entity.*;
import com.me.brickbuster.entity.Brick; import com.me.brickbuster.entity.powerup.PowerUp;
import com.me.brickbuster.entity.Paddle; import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.entity.powerup.*; import com.me.brickbuster.physics.Box2dContactListener;
import java.util.*; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PlayState extends State { public class PlayState extends State {
public static final int SHIELD_HEIGHT = 5; public static final float PIXEL_PER_METER = 40f; // Board dimension: 54x96 meters
public static final float BOARD_WIDTH = 54f;
public static final float BOARD_HEIGHT = 96f;
public static final float EDGE_PADDING = .125f;
public static final float POWERUP_CHANCE = 0.15f; public static final float POWERUP_CHANCE = 0.15f;
public static final Map<Class<? extends PowerUp>, Integer> powerUpWeights; public static final int COLUMNS = 9;
private static final int weightSum; public static final int ROWS = 8;
public static final Vector2 lowerLeftCorner =
new Vector2(EDGE_PADDING,EDGE_PADDING);
public static final Vector2 lowerRightCorner =
new Vector2(BrickBuster.BOARD_WIDTH/PIXEL_PER_METER-EDGE_PADDING,EDGE_PADDING);
public static final Vector2 upperRightCorner =
new Vector2(BrickBuster.BOARD_WIDTH/PIXEL_PER_METER-EDGE_PADDING, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER-EDGE_PADDING);
public static final Vector2 upperLeftCorner =
new Vector2(EDGE_PADDING, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER-EDGE_PADDING);
public World world;
public Body playArea;
public Array<Body> bodies;
public List<PowerUp> powerUps; public List<PowerUp> powerUps;
public Paddle paddle; public Paddle paddle;
public List<Ball> balls; public List<Ball> balls;
public List<Brick> bricks; public List<Brick> bricks;
public List<Shield> shields;
private int shieldCount = 0;
private float updateTime = 0f; private float updateTime = 0f;
private final Box2DDebugRenderer debugRenderer;
public PlayState(BrickBuster game) { public PlayState(BrickBuster game) {
super(game); super(game);
debugRenderer = new Box2DDebugRenderer();
} }
@Override @Override
public void setup() { public void setup() {
// Initialize a world with no gravity
world = new World(new Vector2(), false);
world.setContactListener(new Box2dContactListener());
// define a playArea body with position set to 0
BodyDef playAreaDef = new BodyDef();
playAreaDef.type = BodyDef.BodyType.StaticBody;
playAreaDef.position.set(new Vector2());
EdgeShape screenEdge = new EdgeShape();
playArea = world.createBody(playAreaDef);
// Right edge
screenEdge.set(lowerRightCorner, upperRightCorner);
playArea.createFixture(screenEdge, 0f);
// Top edge
screenEdge.set(upperRightCorner, upperLeftCorner);
playArea.createFixture(screenEdge, 0f);
// Left edge
screenEdge.set(upperLeftCorner, lowerLeftCorner);
playArea.createFixture(screenEdge, 0f);
// Bottom edge
//screenEdge.set(lowerLeftCorner, lowerRightCorner);
//playArea.createFixture(screenEdge, 0f);
screenEdge.dispose();
powerUps = new ArrayList<PowerUp>(); powerUps = new ArrayList<PowerUp>();
paddle = new Paddle(this); paddle = new Paddle(this);
float brick_padding = ((BrickBuster.BOARD_WIDTH/PIXEL_PER_METER) - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1);
bricks = new ArrayList<Brick>(); bricks = new ArrayList<Brick>();
for (int col = 0; col < 13; col++) { for (int col = 0; col < COLUMNS; col++) {
for (int row = 0; row < 7; row++) { for (int row = 0; row < ROWS; row++) {
int x = 15 + (col * (Brick.BLOCK_WIDTH + 10)); float x = brick_padding + (col * (Brick.BRICK_WIDTH + brick_padding));
int y = 15 + Brick.BLOCK_HEIGHT + (row * (Brick.BLOCK_HEIGHT + 10)); float y = brick_padding + Brick.BRICK_HEIGHT + (row * (Brick.BRICK_HEIGHT + brick_padding));
Class<? extends PowerUp> powerUpType = null;
PowerUpType powerUpType = null;
if (MathUtils.randomBoolean(POWERUP_CHANCE)) { if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
powerUpType = getWeightedPowerUp(); powerUpType = PowerUpType.getWeightedRandom();
} }
bricks.add(new Brick(this, powerUpType, x, BrickBuster.HEIGHT - y));
bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER - y));
} }
} }
balls = new ArrayList<Ball>(); balls = new ArrayList<Ball>();
balls.add(new Ball(this)); balls.add(new Ball(this));
shields = new ArrayList<Shield>();
} }
@Override @Override
public void render() { public void render() {
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for (Body b : bodies) {
Entity e = (Entity) b.getUserData();
if (e instanceof Ball || e instanceof PowerUp) {
e.setPos(b.getPosition());
}
}
long start = System.nanoTime(); long start = System.nanoTime();
for (Brick block : bricks) { for (Brick block : bricks) {
block.render(game.sr); block.render(game.sr);
@ -67,20 +131,18 @@ public class PlayState extends State {
for (Ball ball : balls) { for (Ball ball : balls) {
ball.render(game.sr); ball.render(game.sr);
} }
for (Shield shield : shields) {
shield.render(game.sr);
}
paddle.render(game.sr); paddle.render(game.sr);
if (getShieldCount() > 0) { //debugRenderer.render(world, game.cam.combined.cpy().scl(PIXEL_PER_METER));
game.sr.begin(ShapeType.Filled);
game.sr.setColor(Color.SALMON);
game.sr.rect(0, 0, BrickBuster.WIDTH, getShieldCount() * SHIELD_HEIGHT);
game.sr.end();
}
long renderTime = System.nanoTime() - start; long renderTime = System.nanoTime() - start;
game.sb.begin(); game.sb.begin();
game.font.setColor(Color.GRAY); game.font.setColor(Color.GRAY);
game.font.draw(game.sb, String.format("FPS: %d Update: %.2f ms Render: %.2f ms", game.font.draw(game.sb, String.format("FPS: %d Update: %.2f ms Render: %.2f ms",
Gdx.graphics.getFramesPerSecond(), updateTime/1000000f, renderTime/1000000f), 0, 13); Gdx.graphics.getFramesPerSecond(), updateTime/1000000f, renderTime/1000000f), 10, BrickBuster.BOARD_HEIGHT-10);
game.sb.end(); game.sb.end();
} }
@ -92,8 +154,9 @@ public class PlayState extends State {
for (Iterator<Ball> it = balls.iterator(); it.hasNext();) { for (Iterator<Ball> it = balls.iterator(); it.hasNext();) {
Ball ball = it.next(); Ball ball = it.next();
ball.update(dt); ball.update(dt);
if (ball.isDead()) { if (ball.isDeleted()) {
it.remove(); it.remove();
world.destroyBody(ball.getBody());
} }
} }
if (balls.isEmpty()) { if (balls.isEmpty()) {
@ -103,38 +166,74 @@ public class PlayState extends State {
for (Iterator<PowerUp> it = powerUps.iterator(); it.hasNext();) { for (Iterator<PowerUp> it = powerUps.iterator(); it.hasNext();) {
PowerUp powerUp = it.next(); PowerUp powerUp = it.next();
powerUp.update(dt); powerUp.update(dt);
if(powerUp.isCaught()) { if(powerUp.isDeleted()) {
it.remove(); it.remove();
world.destroyBody(powerUp.getBody());
} }
} }
if (bricks.isEmpty()) { for (Iterator<Shield> it = shields.iterator(); it.hasNext();) {
// TODO: Fix this - go to an after-game menu Shield shield = it.next();
//create(); shield.update(dt);
if(shield.isDeleted()) {
it.remove();
world.destroyBody(shield.getBody());
}
} }
for (Iterator<Brick> it = bricks.iterator(); it.hasNext();) {
Brick brick = it.next();
brick.update(dt);
if (brick.isDeleted()) {
it.remove();
world.destroyBody(brick.getBody());
}
}
if (bricks.isEmpty()) {
game.setScreen(new MenuState(game));
dispose();
return;
}
world.step(dt, 6, 2);
updateTime = System.nanoTime() - start; updateTime = System.nanoTime() - start;
} }
@Override
public void dispose() {
super.dispose();
world.dispose();
powerUps.clear();
powerUps = null;
balls.clear();
balls = null;
bricks.clear();
bricks = null;
paddle = null;
}
public int getShieldCount() { public int getShieldCount() {
return shieldCount; return shields.size();
} }
public void addShield() { public void addShield() {
shieldCount++; Shield shield = new Shield(this);
paddle.setY(paddle.getY() + SHIELD_HEIGHT); shields.add(shield);
paddle.setY(paddle.getY() + Shield.SHIELD_HEIGHT);
for (Ball ball : balls) { for (Ball ball : balls) {
if (ball.isStuck()) { if (ball.isStuck()) {
ball.setY(ball.getY() + SHIELD_HEIGHT); ball.setY(ball.getY() + Shield.SHIELD_HEIGHT);
} }
} }
} }
public void removeShield() { public void removeShield(Shield shield) {
shieldCount--; paddle.setY(paddle.getY() - Shield.SHIELD_HEIGHT);
paddle.setY(paddle.getY() - SHIELD_HEIGHT);
for (Ball ball : balls) { for (Ball ball : balls) {
if (ball.isStuck()) { if (ball.isStuck()) {
ball.setY(ball.getY() - SHIELD_HEIGHT); ball.setY(ball.getY() - Shield.SHIELD_HEIGHT);
} }
} }
} }
@ -148,32 +247,4 @@ public class PlayState extends State {
paddle.setWidth(Paddle.DEFAULT_WIDTH); paddle.setWidth(Paddle.DEFAULT_WIDTH);
} }
private static final Class<? extends PowerUp> getWeightedPowerUp() {
int remaining = MathUtils.random(weightSum);
for (Map.Entry<Class<? extends PowerUp>, Integer> entry : PlayState.powerUpWeights.entrySet()) {
remaining -= entry.getValue();
if (remaining < 0) {
return entry.getKey();
}
}
return null;
}
static {
Map<Class<? extends PowerUp>, Integer> tmp = new HashMap<Class<? extends PowerUp>, Integer>();
// Assign PowerUp weights here
tmp.put(GluePowerUp.class, 30);
tmp.put(LongerPaddlePowerUp.class, 40);
tmp.put(MultiBallPowerUp.class, 20);
tmp.put(ShieldPowerUp.class, 40);
powerUpWeights = Collections.unmodifiableMap(tmp);
int sum = 0;
for (int x : PlayState.powerUpWeights.values()) {
sum += x;
}
weightSum = sum;
}
} }

View File

@ -6,6 +6,7 @@ import com.me.brickbuster.BrickBuster;
public abstract class State extends ScreenAdapter { public abstract class State extends ScreenAdapter {
protected final BrickBuster game; protected final BrickBuster game;
private boolean disposed;
public State(BrickBuster game) { public State(BrickBuster game) {
this.game = game; this.game = game;
@ -15,7 +16,14 @@ public abstract class State extends ScreenAdapter {
@Override @Override
public final void render(float delta) { public final void render(float delta) {
update(delta); update(delta);
render(); if (!disposed) {
render();
}
}
@Override
public void dispose() {
this.disposed = true;
} }
public abstract void setup(); public abstract void setup();

View File

@ -7,9 +7,12 @@ import com.me.brickbuster.BrickBuster;
public class DesktopLauncher { public class DesktopLauncher {
public static void main (String[] arg) { public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = BrickBuster.WIDTH; config.width = 450;
config.height = BrickBuster.HEIGHT; config.height = 800;
config.title = BrickBuster.TITLE; config.title = BrickBuster.TITLE;
//config.vSyncEnabled = false;
//config.foregroundFPS = 0;
new LwjglApplication(new BrickBuster(), config); new LwjglApplication(new BrickBuster(), config);
} }
} }