Add BrickLayout interace with an implementing GridLayout

Use new GridLayout to generate initial level
Remove some constructors in Birck
Add Color to BrickType enum, alter Brick to use it
This commit is contained in:
Matt Low 2018-11-19 23:44:36 +04:00
parent 719c32502c
commit 5611bbc611
5 changed files with 130 additions and 57 deletions

View File

@ -22,7 +22,6 @@ import com.me.brickbuster.state.PlayState;
public class Brick extends Entity implements PhysicsBody, CollisionListener { public class Brick extends Entity implements PhysicsBody, CollisionListener {
public static final Color DEFAULT_COLOR = Color.FOREST;
public static final float BRICK_WIDTH = 5f; public static final float BRICK_WIDTH = 5f;
public static final float BRICK_HEIGHT = 2.5f; public static final float BRICK_HEIGHT = 2.5f;
@ -33,7 +32,6 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
private BrickShape shape; private BrickShape shape;
private PowerUpType powerUpType; private PowerUpType powerUpType;
private Color color;
private Pixmap pm; private Pixmap pm;
private Texture solid; private Texture solid;
private TextureRegion region; private TextureRegion region;
@ -41,26 +39,13 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
private Body body; private Body body;
private boolean hitByBall = false; private boolean hitByBall = false;
public Brick(PlayState state, PowerUpType powerUpType, float x, float y) { public Brick(PlayState state, BrickType type, BrickShape shape, PowerUpType powerUpType, float x, float y) {
this(state, powerUpType, true, x, y);
}
public Brick(PlayState state, BrickShape shape, PowerUpType powerUpType, float x, float y) {
this(state, BrickType.STANDARD_10, shape, powerUpType, DEFAULT_COLOR, true, x, y);
}
public Brick(PlayState state, PowerUpType powerUpType, boolean hidePowerup, float x, float y) {
this(state, BrickType.STANDARD_10, BrickShape.RECTANGLE, powerUpType, DEFAULT_COLOR, hidePowerup, x, y);
}
public Brick(PlayState state, BrickType type, BrickShape shape, PowerUpType powerUpType, Color color, boolean hidePowerUp, float x, float y) {
super(state, x, y); super(state, x, y);
this.type = type; this.type = type;
this.shape = shape; this.shape = shape;
this.powerUpType = powerUpType; this.powerUpType = powerUpType;
this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color;
this.pm = new Pixmap(1,1, Pixmap.Format.RGBA8888); this.pm = new Pixmap(1,1, Pixmap.Format.RGBA8888);
pm.setColor(color); pm.setColor(type.getColor());
pm.fill(); pm.fill();
this.solid = new Texture(pm); this.solid = new Texture(pm);
this.region = new TextureRegion(solid); this.region = new TextureRegion(solid);

View File

@ -1,19 +1,31 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color;
public enum BrickType { public enum BrickType {
STANDARD_10, STANDARD_10(new Color(0xf1f1f1ff)),
STANDARD_20, STANDARD_20(new Color(0xff8f00ff)),
STANDARD_30, STANDARD_30(Color.CYAN),
STANDARD_40, STANDARD_40(Color.GREEN),
STANDARD_50, STANDARD_50(Color.RED),
STANDARD_60, STANDARD_60(new Color(0x0070ffff)),
STANDARD_70, STANDARD_70(new Color(0xff00ffff)),
STANDARD_80, STANDARD_80(new Color(0xffff00ff)),
EXPLOSIVE, EXPLOSIVE(Color.GRAY),
HARD, HARD(new Color(0x9d9d9dff)),
HARDER, HARDER(new Color(0xbcae00ff)),
UNBREAKABLE UNBREAKABLE(Color.BLACK)
; ;
private Color color;
BrickType(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
} }

View File

@ -0,0 +1,12 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.entity.Brick;
public interface BrickLayout {
void initialize();
Array<Brick> getBricks();
}

View File

@ -0,0 +1,82 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.entity.BrickShape;
import com.me.brickbuster.entity.BrickType;
import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.state.PlayState;
public class GridLayout implements BrickLayout {
private final PlayState state;
private final int cols;
private final int rows;
private final float powerUpChance;
private final float brick_padding;
private BrickShape shape = null;
private BrickType type = null;
private boolean randomShape = false;
private boolean randomType = false;
private final Array<Brick> bricks;
public GridLayout(PlayState state, int cols, int rows, float powerUpChance) {
this(state, cols, rows, powerUpChance, false, false);
}
public GridLayout(PlayState state, int cols, int rows, float powerUpChance, boolean randomShape, boolean randomType) {
this(state, cols, rows, powerUpChance, BrickShape.RECTANGLE, BrickType.STANDARD_10);
this.randomShape = randomShape;
this.randomType = randomType;
}
public GridLayout(PlayState state, int cols, int rows, float powerUpChance, BrickShape shape, BrickType type) {
this.state = state;
this.cols = cols;
this.rows = rows;
this.brick_padding = (PlayState.BOARD_WIDTH - cols * Brick.BRICK_WIDTH) / (cols + 1);
this.powerUpChance = powerUpChance;
this.shape = shape;
this.type = type;
this.bricks = new Array<Brick>(cols * rows);
}
@Override
public void initialize() {
for (int col = 0; col < cols; col++) {
for (int row = 0; row < rows; row++) {
bricks.add(getBrickForCell(row, col));
}
}
}
protected Brick getBrickForCell(int row, int col) {
final float x = brick_padding + Brick.BRICK_WIDTH/2 + (col * (Brick.BRICK_WIDTH + brick_padding));
final float y = brick_padding + Brick.BRICK_HEIGHT/2 + (row * (Brick.BRICK_HEIGHT + brick_padding));
PowerUpType powerUpType = null;
if (MathUtils.randomBoolean(powerUpChance)) {
powerUpType = PowerUpType.getWeightedRandom();
}
if (randomType) {
type = BrickType.values()[MathUtils.random(BrickType.values().length-1)];
}
if (randomShape) {
shape = BrickShape.values()[MathUtils.random(BrickShape.values().length-1)];
}
return new Brick(state, type, shape, powerUpType, x, PlayState.BOARD_HEIGHT - y);
}
@Override
public Array<Brick> getBricks() {
return bricks;
}
}

View File

@ -2,14 +2,14 @@ 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.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*; import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.entity.*; import com.me.brickbuster.entity.*;
import com.me.brickbuster.entity.powerup.PowerUp; import com.me.brickbuster.entity.powerup.PowerUp;
import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.layout.BrickLayout;
import com.me.brickbuster.layout.GridLayout;
import com.me.brickbuster.physics.Box2dContactListener; import com.me.brickbuster.physics.Box2dContactListener;
import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.EntityType;
@ -22,11 +22,10 @@ public class PlayState extends State {
public static final float BOARD_HEIGHT = 96f; public static final float BOARD_HEIGHT = 96f;
public static final float EDGE_PADDING = .125f; public static final float EDGE_PADDING = .125f;
public static final int COLUMNS = 10;
public static final int ROWS = 20;
public static final float POWERUP_CHANCE = 0.15f; public static final float POWERUP_CHANCE = 0.15f;
public static final int COLUMNS = 9;
public static final int ROWS = 8;
public static final Vector2 lowerLeftCorner = public static final Vector2 lowerLeftCorner =
new Vector2(EDGE_PADDING,EDGE_PADDING); new Vector2(EDGE_PADDING,EDGE_PADDING);
public static final Vector2 lowerRightCorner = public static final Vector2 lowerRightCorner =
@ -64,13 +63,12 @@ public class PlayState extends State {
bodies = new Array<Body>(); bodies = new Array<Body>();
// define a playArea body with position set to 0 // Set up the borders
BodyDef playAreaDef = new BodyDef(); BodyDef playAreaDef = new BodyDef();
playAreaDef.type = BodyDef.BodyType.StaticBody; playAreaDef.type = BodyDef.BodyType.StaticBody;
playAreaDef.position.set(new Vector2()); playAreaDef.position.set(new Vector2());
EdgeShape screenEdge = new EdgeShape(); EdgeShape screenEdge = new EdgeShape();
FixtureDef playAreaFixture = new FixtureDef(); FixtureDef playAreaFixture = new FixtureDef();
playAreaFixture.shape = screenEdge; playAreaFixture.shape = screenEdge;
@ -78,38 +76,21 @@ public class PlayState extends State {
playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK; playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK;
playArea = world.createBody(playAreaDef); playArea = world.createBody(playAreaDef);
// Right edge
screenEdge.set(lowerRightCorner, upperRightCorner); screenEdge.set(lowerRightCorner, upperRightCorner);
playArea.createFixture(playAreaFixture); playArea.createFixture(playAreaFixture);
// Top edge
screenEdge.set(upperRightCorner, upperLeftCorner); screenEdge.set(upperRightCorner, upperLeftCorner);
playArea.createFixture(playAreaFixture); playArea.createFixture(playAreaFixture);
// Left edge
screenEdge.set(upperLeftCorner, lowerLeftCorner); screenEdge.set(upperLeftCorner, lowerLeftCorner);
playArea.createFixture(playAreaFixture); playArea.createFixture(playAreaFixture);
// Bottom edge
//screenEdge.set(lowerLeftCorner, lowerRightCorner);
//playArea.createFixture(playAreaFixture);
screenEdge.dispose(); screenEdge.dispose();
powerUps = new Array<PowerUp>(); powerUps = new Array<PowerUp>();
paddle = new Paddle(this); paddle = new Paddle(this);
float brick_padding = (BOARD_WIDTH - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1); BrickLayout layout = new GridLayout(this, COLUMNS, ROWS, POWERUP_CHANCE);
bricks = new Array<Brick>(); layout.initialize();
for (int col = 0; col < COLUMNS; col++) {
for (int row = ROWS-1; row >= 0; row--) {
float x = brick_padding + Brick.BRICK_WIDTH/2 + (col * (Brick.BRICK_WIDTH + brick_padding));
float y = brick_padding + Brick.BRICK_HEIGHT/2 + (row * (Brick.BRICK_HEIGHT + brick_padding));
PowerUpType powerUpType = null; bricks = layout.getBricks();
if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
powerUpType = PowerUpType.getWeightedRandom();
}
bricks.add(new Brick(this, powerUpType, x, BOARD_HEIGHT - y));
}
}
balls = new Array<Ball>(); balls = new Array<Ball>();
balls.add(new Ball(this)); balls.add(new Ball(this));
@ -192,6 +173,7 @@ public class PlayState extends State {
brick.update(dt); brick.update(dt);
if (brick.isDeleted()) { if (brick.isDeleted()) {
it.remove(); it.remove();
brick.dispose();
world.destroyBody(brick.getBody()); world.destroyBody(brick.getBody());
} }
} }