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.
This commit is contained in:
parent
958232b2ff
commit
f90f5d5452
@ -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));
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ public class GluePowerUp extends PowerUp {
|
||||
|
||||
@Override
|
||||
public void activate() {
|
||||
state.paddle.setSticky(true);
|
||||
((PlayState) state).paddle.setSticky(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class ShieldPowerUp extends PowerUp {
|
||||
|
||||
@Override
|
||||
public void activate() {
|
||||
state.addShield();
|
||||
((PlayState) state).addShield();
|
||||
}
|
||||
|
||||
}
|
||||
|
14
core/src/com/me/brickbuster/state/FieldState.java
Normal file
14
core/src/com/me/brickbuster/state/FieldState.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Body> bodies;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user