Treat brick coordinates as their center

This commit is contained in:
Matt Low 2018-11-15 18:19:54 +04:00
parent cb90c949ff
commit 64e00c1a1a
3 changed files with 10 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
@ -47,8 +46,9 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled);
sr.setColor(color);
sr.rect(pos.x * PlayState.PIXEL_PER_METER, pos.y * PlayState.PIXEL_PER_METER,
0f, 0f,
sr.rect((pos.x - BRICK_WIDTH/2) * PlayState.PIXEL_PER_METER,
(pos.y - BRICK_HEIGHT/2) * PlayState.PIXEL_PER_METER,
BRICK_WIDTH/2 * PlayState.PIXEL_PER_METER, BRICK_HEIGHT/2 * PlayState.PIXEL_PER_METER,
BRICK_WIDTH * PlayState.PIXEL_PER_METER, BRICK_HEIGHT * PlayState.PIXEL_PER_METER,
1f, 1f,
body.getAngle() * MathUtils.radiansToDegrees);
@ -76,7 +76,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
PolygonShape brickShape = new PolygonShape();
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2,
new Vector2(BRICK_WIDTH/2,BRICK_HEIGHT/2), 0);
Vector2.Zero, 0);
FixtureDef brickFixture = new FixtureDef();
brickFixture.shape = brickShape;

View File

@ -42,9 +42,7 @@ public enum PowerUpType {
}
public PowerUp createInstance(PlayState state, Brick brick) {
return createInstance(state,
new Vector2(brick.getX()+Brick.BRICK_WIDTH/2,
brick.getY()+Brick.BRICK_HEIGHT/2));
return createInstance(state, new Vector2(brick.getX(), brick.getY()));
}
public PowerUp createInstance(PlayState state, Vector2 pos) {

View File

@ -95,19 +95,19 @@ public class PlayState extends State {
powerUps = new Array<PowerUp>();
paddle = new Paddle(this);
float brick_padding = ((BrickBuster.BOARD_WIDTH/PIXEL_PER_METER) - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1);
float brick_padding = (BOARD_WIDTH - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1);
bricks = new Array<Brick>();
for (int col = 0; col < COLUMNS; col++) {
for (int row = 0; row < ROWS; row++) {
float x = brick_padding + (col * (Brick.BRICK_WIDTH + brick_padding));
float y = brick_padding + Brick.BRICK_HEIGHT + (row * (Brick.BRICK_HEIGHT + brick_padding));
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;
if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
powerUpType = PowerUpType.getWeightedRandom();
}
bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER - y));
bricks.add(new Brick(this, powerUpType, x, BOARD_HEIGHT - y));
}
}