Refactor to use libGdx Array instead of java ArrayList

Render bricks with their box2d angle
Add ball density = 1f
This commit is contained in:
Matt Low 2018-11-15 13:43:48 +04:00
parent b44b48097c
commit 74aa10de04
3 changed files with 22 additions and 20 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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<Body> bodies;
public List<PowerUp> powerUps;
public Array<PowerUp> powerUps;
public Paddle paddle;
public List<Ball> balls;
public List<Brick> bricks;
public List<Shield> shields;
public Array<Ball> balls;
public Array<Brick> bricks;
public Array<Shield> shields;
private float updateTime = 0f;
@ -85,11 +83,11 @@ public class PlayState extends State {
//playArea.createFixture(screenEdge, 0f);
screenEdge.dispose();
powerUps = new ArrayList<PowerUp>();
powerUps = new Array<PowerUp>();
paddle = new Paddle(this);
float brick_padding = ((BrickBuster.BOARD_WIDTH/PIXEL_PER_METER) - COLUMNS * Brick.BRICK_WIDTH) / (COLUMNS + 1);
bricks = new ArrayList<Brick>();
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));
@ -104,10 +102,10 @@ public class PlayState extends State {
}
}
balls = new ArrayList<Ball>();
balls = new Array<Ball>();
balls.add(new Ball(this));
shields = new ArrayList<Shield>();
shields = new Array<Shield>();
}
@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() {