Use bit-or comparisons to create brick vertices, makes for shorter code

Added new brick shapes
This commit is contained in:
Matt Low 2018-11-20 22:15:47 +04:00
parent ef698a38c1
commit 2a40bcabdf
3 changed files with 103 additions and 154 deletions

View File

@ -29,7 +29,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
private static final Vector2 tmp = new Vector2();
private BrickType type;
private BrickShape shape;
private BrickShape.Shape shape;
private PowerUpType powerUpType;
private Pixmap pm;
@ -39,7 +39,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
private Body body;
private boolean hitByBall = false;
public Brick(FieldState state, BrickType type, BrickShape shape, PowerUpType powerUpType, float x, float y) {
public Brick(FieldState state, BrickType type, BrickShape.Shape shape, PowerUpType powerUpType, float x, float y) {
super(state, x, y);
this.type = type;
this.shape = shape;
@ -95,135 +95,40 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
brickBody.type = BodyDef.BodyType.StaticBody;
brickBody.position.set(pos.cpy());
PolygonShape brickShape = new PolygonShape();
switch (shape) {
case DIAMOND:
brickShape.set(new float[] {
-BRICK_WIDTH/2, 0, // Left
0, BRICK_HEIGHT/2, // Up
BRICK_WIDTH/2, 0, // Right
0, -BRICK_HEIGHT/2 // Down
});
break;
case DOWN_RIGHT_TRIANGLE:
brickShape.set(new float[] {
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case UP_RIGHT_TRIANGLE:
brickShape.set(new float[] {
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
});
break;
case UP_LEFT_TRIANGLE:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case DOWN_LEFT_TRIANGLE:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case HALF_LOWER:
brickShape.set(new float[] {
-BRICK_WIDTH/2, 0, // Top left
BRICK_WIDTH/2, 0, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case HALF_UPPER:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, 0, // Bottom right
-BRICK_WIDTH/2, 0, // Bottom left
});
break;
case HALF_LEFT:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
0, BRICK_HEIGHT/2, // Top right
0, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case HALF_RIGHT:
brickShape.set(new float[] {
0, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
0, -BRICK_HEIGHT/2, // Bottom left
});
break;
case RIGHT_DOWN_RIGHT_TRIANGLE:
brickShape.set(new float[] {
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
0, -BRICK_HEIGHT/2, // Bottom left
});
break;
case RIGHT_UP_RIGHT_TRIANGLE:
brickShape.set(new float[] {
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
0, BRICK_HEIGHT/2, // Top left
});
break;
case RIGHT_UP_LEFT_TRIANGLE:
brickShape.set(new float[] {
0, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top right
0, -BRICK_HEIGHT/2, // Bottom left
});
break;
case RIGHT_DOWN_LEFT_TRIANGLE:
brickShape.set(new float[] {
0, BRICK_HEIGHT/2, // Top left
BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom right
0, -BRICK_HEIGHT/2, // Bottom left
});
break;
case LEFT_DOWN_RIGHT_TRIANGLE:
brickShape.set(new float[] {
0, BRICK_HEIGHT/2, // Top right
0, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case LEFT_UP_RIGHT_TRIANGLE:
brickShape.set(new float[] {
0, BRICK_HEIGHT/2, // Top right
0, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
});
break;
case LEFT_UP_LEFT_TRIANGLE:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
0, BRICK_HEIGHT/2, // Top right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
case LEFT_DOWN_LEFT_TRIANGLE:
brickShape.set(new float[] {
-BRICK_WIDTH/2, BRICK_HEIGHT/2, // Top left
0, -BRICK_HEIGHT/2, // Bottom right
-BRICK_WIDTH/2, -BRICK_HEIGHT/2, // Bottom left
});
break;
default:
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2, Vector2.Zero, 0f);
int shapeMask = shape.getMask();
Vector2[] vertices = new Vector2[Integer.bitCount(shapeMask)];
int v = 0;
if ((shapeMask & BrickShape.BOTTOM_LEFT) != 0) {
vertices[v++] = new Vector2(-BRICK_WIDTH/2, -BRICK_HEIGHT/2);
}
if ((shapeMask & BrickShape.MIDDLE_LEFT) != 0) {
vertices[v++] = new Vector2(-BRICK_WIDTH/2, 0);
}
if ((shapeMask & BrickShape.TOP_LEFT) != 0) {
vertices[v++] = new Vector2(-BRICK_WIDTH/2, BRICK_HEIGHT/2);
}
if ((shapeMask & BrickShape.TOP_MIDDLE) != 0) {
vertices[v++] = new Vector2(0, BRICK_HEIGHT/2);
}
if ((shapeMask & BrickShape.CENTER) != 0) {
vertices[v++] = new Vector2(0, 0);
}
if ((shapeMask & BrickShape.TOP_RIGHT) != 0) {
vertices[v++] = new Vector2(BRICK_WIDTH/2, BRICK_HEIGHT/2);
}
if ((shapeMask & BrickShape.MIDDLE_RIGHT) != 0) {
vertices[v++] = new Vector2(BRICK_WIDTH/2, 0);
}
if ((shapeMask & BrickShape.BOTTOM_RIGHT) != 0) {
vertices[v++] = new Vector2(BRICK_WIDTH/2, -BRICK_HEIGHT/2);
}
if ((shapeMask & BrickShape.BOTTOM_MIDDLE) != 0) {
vertices[v++] = new Vector2(0, -BRICK_HEIGHT/2);
}
PolygonShape brickShape = new PolygonShape();
brickShape.set(vertices);
FixtureDef brickFixture = new FixtureDef();
brickFixture.shape = brickShape;
@ -270,6 +175,18 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
pm.dispose();
}
public BrickType getType() {
return type;
}
public BrickShape.Shape getShape() {
return shape;
}
public PowerUpType getPowerUpType() {
return powerUpType;
}
public boolean hit() {
if (((PlayState) state).bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) {
for (Ball ball : ((PlayState) state).balls) {

View File

@ -1,25 +1,57 @@
package com.me.brickbuster.entity;
public enum BrickShape {
public class BrickShape {
RECTANGLE,
DIAMOND,
DOWN_RIGHT_TRIANGLE,
UP_RIGHT_TRIANGLE,
UP_LEFT_TRIANGLE,
DOWN_LEFT_TRIANGLE,
HALF_UPPER,
HALF_LOWER,
HALF_LEFT,
HALF_RIGHT,
RIGHT_DOWN_RIGHT_TRIANGLE,
RIGHT_UP_RIGHT_TRIANGLE,
RIGHT_UP_LEFT_TRIANGLE,
RIGHT_DOWN_LEFT_TRIANGLE,
LEFT_DOWN_RIGHT_TRIANGLE,
LEFT_UP_RIGHT_TRIANGLE,
LEFT_UP_LEFT_TRIANGLE,
LEFT_DOWN_LEFT_TRIANGLE,
public static final int BOTTOM_LEFT = 0x1;
public static final int MIDDLE_LEFT = 0x1 << 1;
public static final int TOP_LEFT = 0x1 << 2;
public static final int TOP_MIDDLE = 0x1 << 3;
public static final int CENTER = 0x1 << 4;
public static final int TOP_RIGHT = 0x1 << 5;
public static final int MIDDLE_RIGHT = 0x1 << 6;
public static final int BOTTOM_RIGHT = 0x1 << 7;
public static final int BOTTOM_MIDDLE = 0x1 << 8;
public enum Shape {
RECTANGLE(BOTTOM_LEFT | TOP_LEFT | TOP_RIGHT | BOTTOM_RIGHT),
DIAMOND(MIDDLE_LEFT | TOP_MIDDLE | MIDDLE_RIGHT | BOTTOM_MIDDLE),
UP(BOTTOM_LEFT | TOP_MIDDLE | BOTTOM_RIGHT),
HALF_UP(BOTTOM_LEFT | CENTER | BOTTOM_RIGHT),
DOWN(TOP_LEFT | TOP_RIGHT | BOTTOM_MIDDLE),
HALF_DOWN(TOP_LEFT | TOP_RIGHT | CENTER),
LEFT(MIDDLE_LEFT | TOP_RIGHT | BOTTOM_RIGHT),
HALF_LEFT(CENTER | TOP_RIGHT | BOTTOM_RIGHT),
RIGHT(BOTTOM_LEFT | TOP_LEFT | MIDDLE_RIGHT),
HALF_RIGHT(BOTTOM_LEFT | TOP_LEFT | CENTER),
DOWN_RIGHT_TRIANGLE(BOTTOM_LEFT | TOP_RIGHT | BOTTOM_RIGHT),
UP_RIGHT_TRIANGLE(TOP_LEFT | TOP_RIGHT | BOTTOM_RIGHT),
UP_LEFT_TRIANGLE(BOTTOM_LEFT | TOP_LEFT | TOP_RIGHT),
DOWN_LEFT_TRIANGLE(BOTTOM_LEFT | TOP_LEFT | BOTTOM_RIGHT),
HALF_UPPER(MIDDLE_LEFT | TOP_LEFT | TOP_RIGHT | MIDDLE_RIGHT),
HALF_LOWER(BOTTOM_LEFT | MIDDLE_LEFT | MIDDLE_RIGHT | BOTTOM_RIGHT),
HALF_LEFT_SQUARE(BOTTOM_LEFT | TOP_LEFT | TOP_MIDDLE | BOTTOM_MIDDLE),
HALF_RIGHT_SQUARE(BOTTOM_MIDDLE | TOP_MIDDLE | TOP_RIGHT | BOTTOM_RIGHT),
LEFT_DOWN_RIGHT_TRIANGLE(BOTTOM_LEFT | TOP_MIDDLE | BOTTOM_MIDDLE),
LEFT_UP_RIGHT_TRIANGLE(TOP_LEFT | TOP_MIDDLE | BOTTOM_MIDDLE),
LEFT_UP_LEFT_TRIANGLE(BOTTOM_LEFT | TOP_LEFT | TOP_MIDDLE),
LEFT_DOWN_LEFT_TRIANGLE(BOTTOM_LEFT | TOP_LEFT | BOTTOM_MIDDLE),
RIGHT_DOWN_RIGHT_TRIANGLE(BOTTOM_MIDDLE | TOP_RIGHT | BOTTOM_RIGHT),
RIGHT_UP_RIGHT_TRIANGLE(TOP_MIDDLE | TOP_RIGHT | BOTTOM_RIGHT),
RIGHT_UP_LEFT_TRIANGLE(BOTTOM_MIDDLE | TOP_MIDDLE | TOP_RIGHT),
RIGHT_DOWN_LEFT_TRIANGLE(BOTTOM_MIDDLE | TOP_MIDDLE | BOTTOM_RIGHT),
;
private int mask;
Shape(int mask) {
this.mask = mask;
}
public int getMask() {
return mask;
}
}
}

View File

@ -16,7 +16,7 @@ public class GridLayout implements BrickLayout {
private final float powerUpChance;
private final float brick_padding;
private BrickShape shape = null;
private BrickShape.Shape shape = null;
private BrickType type = null;
private boolean randomShape = false;
@ -29,12 +29,12 @@ public class GridLayout implements BrickLayout {
}
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(state, cols, rows, powerUpChance, BrickShape.Shape.RECTANGLE, BrickType.STANDARD_10);
this.randomShape = randomShape;
this.randomType = randomType;
}
public GridLayout(PlayState state, int cols, int rows, float powerUpChance, BrickShape shape, BrickType type) {
public GridLayout(PlayState state, int cols, int rows, float powerUpChance, BrickShape.Shape shape, BrickType type) {
this.state = state;
this.cols = cols;
this.rows = rows;
@ -68,7 +68,7 @@ public class GridLayout implements BrickLayout {
}
if (randomShape) {
shape = BrickShape.values()[MathUtils.random(BrickShape.values().length-1)];
shape = BrickShape.Shape.values()[MathUtils.random(BrickShape.Shape.values().length-1)];
}
return new Brick(state, type, shape, powerUpType, x, PlayState.BOARD_HEIGHT - y);