diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 3b64d7e..6321ca1 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -22,7 +22,6 @@ import com.me.brickbuster.state.PlayState; 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_HEIGHT = 2.5f; @@ -33,7 +32,6 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { private BrickShape shape; private PowerUpType powerUpType; - private Color color; private Pixmap pm; private Texture solid; private TextureRegion region; @@ -41,26 +39,13 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { private Body body; private boolean hitByBall = false; - public Brick(PlayState state, 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) { + public Brick(PlayState state, BrickType type, BrickShape shape, PowerUpType powerUpType, float x, float y) { super(state, x, y); this.type = type; this.shape = shape; this.powerUpType = powerUpType; - this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color; this.pm = new Pixmap(1,1, Pixmap.Format.RGBA8888); - pm.setColor(color); + pm.setColor(type.getColor()); pm.fill(); this.solid = new Texture(pm); this.region = new TextureRegion(solid); diff --git a/core/src/com/me/brickbuster/entity/BrickType.java b/core/src/com/me/brickbuster/entity/BrickType.java index e430aa3..9515731 100644 --- a/core/src/com/me/brickbuster/entity/BrickType.java +++ b/core/src/com/me/brickbuster/entity/BrickType.java @@ -1,19 +1,31 @@ package com.me.brickbuster.entity; +import com.badlogic.gdx.graphics.Color; + public enum BrickType { - STANDARD_10, - STANDARD_20, - STANDARD_30, - STANDARD_40, - STANDARD_50, - STANDARD_60, - STANDARD_70, - STANDARD_80, - EXPLOSIVE, - HARD, - HARDER, - UNBREAKABLE + STANDARD_10(new Color(0xf1f1f1ff)), + STANDARD_20(new Color(0xff8f00ff)), + STANDARD_30(Color.CYAN), + STANDARD_40(Color.GREEN), + STANDARD_50(Color.RED), + STANDARD_60(new Color(0x0070ffff)), + STANDARD_70(new Color(0xff00ffff)), + STANDARD_80(new Color(0xffff00ff)), + EXPLOSIVE(Color.GRAY), + HARD(new Color(0x9d9d9dff)), + HARDER(new Color(0xbcae00ff)), + UNBREAKABLE(Color.BLACK) ; + private Color color; + + BrickType(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } + } diff --git a/core/src/com/me/brickbuster/layout/BrickLayout.java b/core/src/com/me/brickbuster/layout/BrickLayout.java new file mode 100644 index 0000000..4a1126f --- /dev/null +++ b/core/src/com/me/brickbuster/layout/BrickLayout.java @@ -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 getBricks(); + +} diff --git a/core/src/com/me/brickbuster/layout/GridLayout.java b/core/src/com/me/brickbuster/layout/GridLayout.java new file mode 100644 index 0000000..69324e6 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/GridLayout.java @@ -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 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(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 getBricks() { + return bricks; + } + +} diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 7738d40..c39c7c2 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -2,14 +2,14 @@ package com.me.brickbuster.state; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; -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.entity.*; 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.EntityType; @@ -22,11 +22,10 @@ public class PlayState extends State { public static final float BOARD_HEIGHT = 96f; 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 int COLUMNS = 9; - public static final int ROWS = 8; - + public static final Vector2 lowerLeftCorner = new Vector2(EDGE_PADDING,EDGE_PADDING); public static final Vector2 lowerRightCorner = @@ -64,13 +63,12 @@ public class PlayState extends State { bodies = new Array(); - // define a playArea body with position set to 0 + // Set up the borders BodyDef playAreaDef = new BodyDef(); playAreaDef.type = BodyDef.BodyType.StaticBody; playAreaDef.position.set(new Vector2()); EdgeShape screenEdge = new EdgeShape(); - FixtureDef playAreaFixture = new FixtureDef(); playAreaFixture.shape = screenEdge; @@ -78,38 +76,21 @@ public class PlayState extends State { playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK; playArea = world.createBody(playAreaDef); - // Right edge screenEdge.set(lowerRightCorner, upperRightCorner); playArea.createFixture(playAreaFixture); - // Top edge screenEdge.set(upperRightCorner, upperLeftCorner); playArea.createFixture(playAreaFixture); - // Left edge screenEdge.set(upperLeftCorner, lowerLeftCorner); playArea.createFixture(playAreaFixture); - // Bottom edge - //screenEdge.set(lowerLeftCorner, lowerRightCorner); - //playArea.createFixture(playAreaFixture); screenEdge.dispose(); powerUps = new Array(); paddle = new Paddle(this); - float brick_padding = (BOARD_WIDTH - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1); - bricks = new Array(); - 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)); + BrickLayout layout = new GridLayout(this, COLUMNS, ROWS, POWERUP_CHANCE); + layout.initialize(); - PowerUpType powerUpType = null; - if (MathUtils.randomBoolean(POWERUP_CHANCE)) { - powerUpType = PowerUpType.getWeightedRandom(); - } - - bricks.add(new Brick(this, powerUpType, x, BOARD_HEIGHT - y)); - } - } + bricks = layout.getBricks(); balls = new Array(); balls.add(new Ball(this)); @@ -192,6 +173,7 @@ public class PlayState extends State { brick.update(dt); if (brick.isDeleted()) { it.remove(); + brick.dispose(); world.destroyBody(brick.getBody()); } }