From 74aa10de04d5d6170fc88e09d3775dc871af4e9c Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 15 Nov 2018 13:43:48 +0400 Subject: [PATCH] Refactor to use libGdx Array instead of java ArrayList Render bricks with their box2d angle Add ball density = 1f --- core/src/com/me/brickbuster/entity/Ball.java | 3 ++- core/src/com/me/brickbuster/entity/Brick.java | 13 ++++++---- .../com/me/brickbuster/state/PlayState.java | 26 +++++++++---------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 8f628e5..a46170e 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -30,7 +30,7 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { public Ball(PlayState state) { super(state, state.paddle.getX(), state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); - this.speed = state.bricks.size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED; + this.speed = state.bricks.size > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED; createBody(); } @@ -83,6 +83,7 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { ballFixture.shape = ballShape; ballFixture.restitution = 1f; ballFixture.friction = 0f; + ballFixture.density = 1f; body = state.world.createBody(ballBody); body.createFixture(ballFixture); diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 74ea235..4dc18c4 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -3,6 +3,8 @@ package com.me.brickbuster.entity; 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; @@ -44,10 +46,11 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { public void render(ShapeRenderer sr) { sr.begin(ShapeType.Filled); sr.setColor(color); - sr.rect(getX() * PlayState.PIXEL_PER_METER, - getY() * PlayState.PIXEL_PER_METER, - BRICK_WIDTH * PlayState.PIXEL_PER_METER, - BRICK_HEIGHT * PlayState.PIXEL_PER_METER); + sr.rect(pos.x * PlayState.PIXEL_PER_METER, pos.y * PlayState.PIXEL_PER_METER, + 0f, 0f, + BRICK_WIDTH * PlayState.PIXEL_PER_METER, BRICK_HEIGHT * PlayState.PIXEL_PER_METER, + 1f, 1f, + body.getAngle() * MathUtils.radiansToDegrees); sr.end(); } @@ -97,7 +100,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { } public boolean hit() { - if (state.bricks.size()-1 <= Ball.BLOCKS_FOR_BOOST) { + if (state.bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) { for (Ball ball : state.balls) { ball.setSpeed(Ball.BOOST_SPEED); } diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 8ce6d62..9fff855 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -12,9 +12,7 @@ import com.me.brickbuster.entity.powerup.PowerUp; import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.physics.Box2dContactListener; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; public class PlayState extends State { @@ -42,11 +40,11 @@ public class PlayState extends State { public Body playArea; public Array bodies; - public List powerUps; + public Array powerUps; public Paddle paddle; - public List balls; - public List bricks; - public List shields; + public Array balls; + public Array bricks; + public Array shields; private float updateTime = 0f; @@ -85,11 +83,11 @@ public class PlayState extends State { //playArea.createFixture(screenEdge, 0f); screenEdge.dispose(); - powerUps = new ArrayList(); + powerUps = new Array(); paddle = new Paddle(this); float brick_padding = ((BrickBuster.BOARD_WIDTH/PIXEL_PER_METER) - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1); - bricks = new ArrayList(); + bricks = new Array(); for (int col = 0; col < COLUMNS; col++) { for (int row = 0; row < ROWS; row++) { float x = brick_padding + (col * (Brick.BRICK_WIDTH + brick_padding)); @@ -104,10 +102,10 @@ public class PlayState extends State { } } - balls = new ArrayList(); + balls = new Array(); balls.add(new Ball(this)); - shields = new ArrayList(); + shields = new Array(); } @Override @@ -116,7 +114,7 @@ public class PlayState extends State { world.getBodies(bodies); for (Body b : bodies) { Entity e = (Entity) b.getUserData(); - if (e instanceof Ball || e instanceof PowerUp) { + if (e instanceof Ball || e instanceof PowerUp || e instanceof Brick) { e.setPos(b.getPosition()); } } @@ -159,7 +157,7 @@ public class PlayState extends State { world.destroyBody(ball.getBody()); } } - if (balls.isEmpty()) { + if (balls.size == 0) { ballReset(); } @@ -189,7 +187,7 @@ public class PlayState extends State { world.destroyBody(brick.getBody()); } } - if (bricks.isEmpty()) { + if (bricks.size == 0) { game.setScreen(new MenuState(game)); dispose(); return; @@ -215,7 +213,7 @@ public class PlayState extends State { } public int getShieldCount() { - return shields.size(); + return shields.size; } public void addShield() {