From f90f5d545282c0c90d0a173ece4630663824b11d Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 20 Nov 2018 17:07:29 +0400 Subject: [PATCH] Add new abstract FieldState, which contains the Box2d world instance. PlayState now extends FieldState instead of State. Entity now takes a FieldState instead of a State instance. Casts from State to PlayState where necessary in entities. --- core/src/com/me/brickbuster/entity/Ball.java | 11 ++--- core/src/com/me/brickbuster/entity/Brick.java | 44 +++++++++---------- .../src/com/me/brickbuster/entity/Entity.java | 8 ++-- .../src/com/me/brickbuster/entity/Paddle.java | 4 +- .../src/com/me/brickbuster/entity/Shield.java | 2 +- .../entity/powerup/GluePowerUp.java | 2 +- .../entity/powerup/LongerPaddlePowerUp.java | 4 +- .../entity/powerup/MultiBallPowerUp.java | 4 +- .../entity/powerup/ShieldPowerUp.java | 2 +- .../com/me/brickbuster/state/FieldState.java | 14 ++++++ .../com/me/brickbuster/state/PlayState.java | 4 +- 11 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 core/src/com/me/brickbuster/state/FieldState.java diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index cc9d5e6..0e817f6 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -121,15 +121,16 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { } public Vector2 paddleReflectAngle() { - float rel = (pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2); - float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth())); + Paddle paddle = ((PlayState) state).paddle; + float rel = (pos.x - paddle.getX()) + (paddle.getWidth()/2); + float newAngle = MathUtils.PI - (MathUtils.PI * (rel / paddle.getWidth())); newAngle = MathUtils.clamp(newAngle, MINIMUM_ANGLE, MathUtils.PI-MINIMUM_ANGLE); return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle)); } public void launch() { Vector2 direction; - if (state.paddle.isSticky()) { + if (((PlayState) state).paddle.isSticky()) { direction = paddleReflectAngle(); } else { // launch at random angle between 135 and 45 degrees @@ -141,10 +142,10 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { } public void paddleCollision() { - if (state.paddle.isSticky()) { + if (((PlayState) state).paddle.isSticky()) { isStuck = true; body.setLinearVelocity(new Vector2()); - setY(state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); + setY(((PlayState) state).paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); return; } body.setLinearVelocity(paddleReflectAngle().scl(speed)); diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 5ae98a9..7afad97 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -17,6 +17,7 @@ import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.physics.CollisionListener; import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.PhysicsBody; +import com.me.brickbuster.state.FieldState; import com.me.brickbuster.state.PlayState; public class Brick extends Entity implements PhysicsBody, CollisionListener { @@ -38,7 +39,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { private Body body; private boolean hitByBall = false; - public Brick(PlayState state, BrickType type, BrickShape shape, PowerUpType powerUpType, float x, float y) { + public Brick(FieldState state, BrickType type, BrickShape shape, PowerUpType powerUpType, float x, float y) { super(state, x, y); this.type = type; this.shape = shape; @@ -95,7 +96,6 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { brickBody.position.set(pos.cpy()); PolygonShape brickShape = new PolygonShape(); - switch (shape) { case DIAMOND: brickShape.set(new float[] { @@ -135,41 +135,40 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { 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 + -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 + -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 + -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 + 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; default: brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2, Vector2.Zero, 0f); } - FixtureDef brickFixture = new FixtureDef(); brickFixture.shape = brickShape; brickFixture.friction = 0f; @@ -178,6 +177,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { brickFixture.filter.categoryBits = EntityType.BRICK; brickFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK | EntityType.BOUNDARY; + body = state.world.createBody(brickBody); body.createFixture(brickFixture); body.setUserData(this); @@ -203,14 +203,14 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { } public boolean hit() { - if (state.bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) { - for (Ball ball : state.balls) { + if (((PlayState) state).bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) { + for (Ball ball : ((PlayState) state).balls) { ball.setSpeed(Ball.BOOST_SPEED); } } if (powerUpType != null) { - state.powerUps.add(powerUpType.createInstance(state, this)); + ((PlayState) state).powerUps.add(powerUpType.createInstance((PlayState) state, this)); } deleted = true; diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index a0ee15e..52e297b 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -2,19 +2,19 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; -import com.me.brickbuster.state.PlayState; +import com.me.brickbuster.state.FieldState; public abstract class Entity { - protected PlayState state; + protected FieldState state; protected Vector2 pos; protected boolean deleted = false; - public Entity(PlayState state, Vector2 pos) { + public Entity(FieldState state, Vector2 pos) { this(state, pos.x, pos.y); } - public Entity(PlayState state, float x, float y) { + public Entity(FieldState state, float x, float y) { this.state = state; this.pos = new Vector2(x, y); } diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 8c6aa48..75d24a0 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -68,7 +68,7 @@ public class Paddle extends Entity implements PhysicsBody { } if (!MathUtils.isZero(adjust)) { setX(pos.x + adjust); - for (Ball ball : state.balls) { + for (Ball ball : ((PlayState) state).balls) { if (ball.isStuck()) { ball.setX(ball.getX() + adjust); } @@ -128,7 +128,7 @@ public class Paddle extends Entity implements PhysicsBody { if (this.width == width) { return; } - state.world.destroyBody(body); + ((PlayState) state).world.destroyBody(body); this.width = width; createBody(); } diff --git a/core/src/com/me/brickbuster/entity/Shield.java b/core/src/com/me/brickbuster/entity/Shield.java index c78cf22..753bdd4 100644 --- a/core/src/com/me/brickbuster/entity/Shield.java +++ b/core/src/com/me/brickbuster/entity/Shield.java @@ -36,7 +36,7 @@ public class Shield extends Entity implements PhysicsBody { @Override public void update(float dt) { if (deleted) { - state.removeShield(this); + ((PlayState) state).removeShield(this); } } diff --git a/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java b/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java index a6712c3..fe4fe01 100644 --- a/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java @@ -12,7 +12,7 @@ public class GluePowerUp extends PowerUp { @Override public void activate() { - state.paddle.setSticky(true); + ((PlayState) state).paddle.setSticky(true); } } diff --git a/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java b/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java index cd344a5..5d4a0ba 100644 --- a/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java @@ -13,8 +13,8 @@ public class LongerPaddlePowerUp extends PowerUp { @Override public void activate() { - if (state.paddle.getWidth() < Paddle.DEFAULT_WIDTH*2.5) { - state.paddle.setWidth(state.paddle.getWidth() + Paddle.DEFAULT_WIDTH/2); + if (((PlayState) state).paddle.getWidth() < Paddle.DEFAULT_WIDTH*2.5) { + ((PlayState) state).paddle.setWidth(((PlayState) state).paddle.getWidth() + Paddle.DEFAULT_WIDTH/2); } } } diff --git a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java index f2c4048..9e6bbb9 100644 --- a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java @@ -19,9 +19,9 @@ public class MultiBallPowerUp extends PowerUp { @Override public void activate() { for (int x = 0; x < 2; x++) { - Ball ball = new Ball(state); + Ball ball = new Ball((PlayState) state); ball.launch(); - state.balls.add(ball); + ((PlayState) state).balls.add(ball); } } } diff --git a/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java index c618ba7..219c4d6 100644 --- a/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java @@ -12,7 +12,7 @@ public class ShieldPowerUp extends PowerUp { @Override public void activate() { - state.addShield(); + ((PlayState) state).addShield(); } } diff --git a/core/src/com/me/brickbuster/state/FieldState.java b/core/src/com/me/brickbuster/state/FieldState.java new file mode 100644 index 0000000..d5812cc --- /dev/null +++ b/core/src/com/me/brickbuster/state/FieldState.java @@ -0,0 +1,14 @@ +package com.me.brickbuster.state; + +import com.badlogic.gdx.physics.box2d.World; +import com.me.brickbuster.BrickBuster; + +public abstract class FieldState extends State { + + public World world; + + public FieldState(BrickBuster game) { + super(game); + } + +} diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 73f5c86..75bc2ce 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -14,7 +14,7 @@ import com.me.brickbuster.physics.EntityType; import java.util.Iterator; -public class PlayState extends State { +public class PlayState extends FieldState { public static final float PIXEL_PER_METER = 40f; // Board dimension: 54x96 meters public static final float BOARD_WIDTH = 54f; @@ -30,8 +30,6 @@ public class PlayState extends State { public static final Vector2 upperLeftCorner = new Vector2(EDGE_PADDING, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER-EDGE_PADDING); - public World world; - public Body playArea; public Array bodies;