Begin adding support for rendering different shaped bricks

This commit is contained in:
Matt Low 2018-11-15 20:09:48 +04:00
parent 64e00c1a1a
commit f70dc67878
5 changed files with 113 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture; 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.PolygonSpriteBatch;
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.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
@ -25,6 +26,7 @@ public class BrickBuster extends Game {
public BitmapFont font; public BitmapFont font;
public SpriteBatch sb; public SpriteBatch sb;
public ShapeRenderer sr; public ShapeRenderer sr;
public PolygonSpriteBatch pb;
@Override @Override
public void create () { public void create () {
@ -38,6 +40,7 @@ public class BrickBuster extends Game {
sb = new SpriteBatch(); sb = new SpriteBatch();
sr = new ShapeRenderer(); sr = new ShapeRenderer();
pb = new PolygonSpriteBatch();
setScreen(new MenuState(this)); setScreen(new MenuState(this));
} }
@ -56,6 +59,7 @@ public class BrickBuster extends Game {
sb.setProjectionMatrix(cam.combined); sb.setProjectionMatrix(cam.combined);
sr.setProjectionMatrix(cam.combined); sr.setProjectionMatrix(cam.combined);
pb.setProjectionMatrix(cam.combined);
super.resize(width, height); super.resize(width, height);
} }

View File

@ -1,14 +1,19 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.PolygonRegion;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.math.EarClippingTriangulator;
import com.badlogic.gdx.math.MathUtils;
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.Body;
import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.ShortArray;
import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.physics.CollisionListener; import com.me.brickbuster.physics.CollisionListener;
import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.EntityType;
@ -21,38 +26,66 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
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;
private static final EarClippingTriangulator ECT = new EarClippingTriangulator();
private BrickType type;
private BrickShape shape;
private PowerUpType powerUpType; private PowerUpType powerUpType;
private Color color; private Color color;
private Pixmap pm;
private Texture solid;
private TextureRegion region;
private Body body; private Body body;
private boolean hitByBall = false; private boolean hitByBall = false;
private final Vector2 tmp = new Vector2();
public Brick(PlayState state, PowerUpType powerUpType, float x, float y) { public Brick(PlayState state, PowerUpType powerUpType, float x, float y) {
this(state, powerUpType, true, x, y); this(state, powerUpType, true, x, y);
} }
public Brick(PlayState state, PowerUpType powerUpType, boolean hidePowerup, float x, float y) { public Brick(PlayState state, BrickShape shape, PowerUpType powerUpType, float x, float y) {
this(state, powerUpType, DEFAULT_COLOR, hidePowerup, x, y); this(state, BrickType.STANDARD_10, shape, powerUpType, DEFAULT_COLOR, true, x, y);
} }
public Brick(PlayState state, PowerUpType powerUpType, Color color, boolean hidePowerUp, float x, float 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.shape = shape;
this.powerUpType = powerUpType; this.powerUpType = powerUpType;
this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color; this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color;
this.pm = new Pixmap(1,1, Pixmap.Format.RGBA8888);
pm.setColor(color);
pm.fill();
this.solid = new Texture(pm);
this.region = new TextureRegion(solid);
createBody(); createBody();
} }
@Override @Override
public void render(ShapeRenderer sr) { public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled); PolygonShape shape = (PolygonShape) body.getFixtureList().get(0).getShape();
sr.setColor(color); float[] vertices = new float[shape.getVertexCount()*2];
sr.rect((pos.x - BRICK_WIDTH/2) * PlayState.PIXEL_PER_METER, for (int i = 0; i < vertices.length/2; i++) {
(pos.y - BRICK_HEIGHT/2) * PlayState.PIXEL_PER_METER, shape.getVertex(i, tmp);
BRICK_WIDTH/2 * PlayState.PIXEL_PER_METER, BRICK_HEIGHT/2 * PlayState.PIXEL_PER_METER, Vector2 vertex = body.getWorldPoint(tmp);
BRICK_WIDTH * PlayState.PIXEL_PER_METER, BRICK_HEIGHT * PlayState.PIXEL_PER_METER, vertices[i*2] = vertex.x * PlayState.PIXEL_PER_METER;
1f, 1f, vertices[i*2 + 1] = vertex.y * PlayState.PIXEL_PER_METER;
body.getAngle() * MathUtils.radiansToDegrees); }
sr.end(); ShortArray triangleIndices = ECT.computeTriangles(vertices);
PolygonRegion polyRegion = new PolygonRegion(region, vertices, triangleIndices.toArray());
PolygonSpriteBatch pb = state.getGame().pb;
pb.begin();
pb.draw(polyRegion, 0, 0);
pb.end();
} }
@Override @Override
@ -75,15 +108,28 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
brickBody.position.set(pos.cpy()); brickBody.position.set(pos.cpy());
PolygonShape brickShape = new PolygonShape(); PolygonShape brickShape = new PolygonShape();
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2,
Vector2.Zero, 0); 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;
default:
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2, Vector2.Zero, 0f);
}
FixtureDef brickFixture = new FixtureDef(); FixtureDef brickFixture = new FixtureDef();
brickFixture.shape = brickShape; brickFixture.shape = brickShape;
brickFixture.friction = 0f; brickFixture.friction = 0f;
brickFixture.density = 0.5f;
brickFixture.filter.categoryBits = EntityType.BRICK; brickFixture.filter.categoryBits = EntityType.BRICK;
brickFixture.filter.maskBits = EntityType.BALL; brickFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK | EntityType.BOUNDARY;
body = state.world.createBody(brickBody); body = state.world.createBody(brickBody);
body.createFixture(brickFixture); body.createFixture(brickFixture);

View File

@ -0,0 +1,23 @@
package com.me.brickbuster.entity;
public enum BrickShape {
RECTANGLE,
DIAMOND,
DOWN_RIGHT_TRIANGLE,
UP_RIGHT_TRIANGLE,
UP_LEFT_TRIANGLE,
DOWN_LEFT_TRIANGLE,
HALF_LOWER,
HALF_UPPER,
HALF_LEFT,
HALF_RIGHT,
THIRD_LEFT,
THIRD_HMIDDLE,
THIRD_RIGHT,
THIRD_UPPER,
THIRD_VMIDDLE,
THIRD_LOWER,
;
}

View File

@ -0,0 +1,19 @@
package com.me.brickbuster.entity;
public enum BrickType {
STANDARD_10,
STANDARD_20,
STANDARD_30,
STANDARD_40,
STANDARD_50,
STANDARD_60,
STANDARD_70,
STANDARD_80,
EXPLOSIVE,
HARD,
HARDER,
UNBREAKABLE
;
}

View File

@ -26,6 +26,10 @@ public abstract class State extends ScreenAdapter {
this.disposed = true; this.disposed = true;
} }
public BrickBuster getGame() {
return game;
}
public abstract void setup(); public abstract void setup();
public abstract void render(); public abstract void render();