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() {
|
public Vector2 paddleReflectAngle() {
|
||||||
float rel = (pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2);
|
Paddle paddle = ((PlayState) state).paddle;
|
||||||
float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth()));
|
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);
|
newAngle = MathUtils.clamp(newAngle, MINIMUM_ANGLE, MathUtils.PI-MINIMUM_ANGLE);
|
||||||
return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
|
return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void launch() {
|
public void launch() {
|
||||||
Vector2 direction;
|
Vector2 direction;
|
||||||
if (state.paddle.isSticky()) {
|
if (((PlayState) state).paddle.isSticky()) {
|
||||||
direction = paddleReflectAngle();
|
direction = paddleReflectAngle();
|
||||||
} else {
|
} else {
|
||||||
// launch at random angle between 135 and 45 degrees
|
// launch at random angle between 135 and 45 degrees
|
||||||
@ -141,10 +142,10 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void paddleCollision() {
|
public void paddleCollision() {
|
||||||
if (state.paddle.isSticky()) {
|
if (((PlayState) state).paddle.isSticky()) {
|
||||||
isStuck = true;
|
isStuck = true;
|
||||||
body.setLinearVelocity(new Vector2());
|
body.setLinearVelocity(new Vector2());
|
||||||
setY(state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
|
setY(((PlayState) state).paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
body.setLinearVelocity(paddleReflectAngle().scl(speed));
|
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.CollisionListener;
|
||||||
import com.me.brickbuster.physics.EntityType;
|
import com.me.brickbuster.physics.EntityType;
|
||||||
import com.me.brickbuster.physics.PhysicsBody;
|
import com.me.brickbuster.physics.PhysicsBody;
|
||||||
|
import com.me.brickbuster.state.FieldState;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
||||||
@ -38,7 +39,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
private Body body;
|
private Body body;
|
||||||
private boolean hitByBall = false;
|
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);
|
super(state, x, y);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.shape = shape;
|
this.shape = shape;
|
||||||
@ -95,7 +96,6 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
brickBody.position.set(pos.cpy());
|
brickBody.position.set(pos.cpy());
|
||||||
|
|
||||||
PolygonShape brickShape = new PolygonShape();
|
PolygonShape brickShape = new PolygonShape();
|
||||||
|
|
||||||
switch (shape) {
|
switch (shape) {
|
||||||
case DIAMOND:
|
case DIAMOND:
|
||||||
brickShape.set(new float[] {
|
brickShape.set(new float[] {
|
||||||
@ -169,7 +169,6 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2, Vector2.Zero, 0f);
|
brickShape.setAsBox(BRICK_WIDTH/2, BRICK_HEIGHT/2, Vector2.Zero, 0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FixtureDef brickFixture = new FixtureDef();
|
FixtureDef brickFixture = new FixtureDef();
|
||||||
brickFixture.shape = brickShape;
|
brickFixture.shape = brickShape;
|
||||||
brickFixture.friction = 0f;
|
brickFixture.friction = 0f;
|
||||||
@ -178,6 +177,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
brickFixture.filter.categoryBits = EntityType.BRICK;
|
brickFixture.filter.categoryBits = EntityType.BRICK;
|
||||||
brickFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK | EntityType.BOUNDARY;
|
brickFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK | EntityType.BOUNDARY;
|
||||||
|
|
||||||
|
|
||||||
body = state.world.createBody(brickBody);
|
body = state.world.createBody(brickBody);
|
||||||
body.createFixture(brickFixture);
|
body.createFixture(brickFixture);
|
||||||
body.setUserData(this);
|
body.setUserData(this);
|
||||||
@ -203,14 +203,14 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hit() {
|
public boolean hit() {
|
||||||
if (state.bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) {
|
if (((PlayState) state).bricks.size-1 <= Ball.BLOCKS_FOR_BOOST) {
|
||||||
for (Ball ball : state.balls) {
|
for (Ball ball : ((PlayState) state).balls) {
|
||||||
ball.setSpeed(Ball.BOOST_SPEED);
|
ball.setSpeed(Ball.BOOST_SPEED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (powerUpType != null) {
|
if (powerUpType != null) {
|
||||||
state.powerUps.add(powerUpType.createInstance(state, this));
|
((PlayState) state).powerUps.add(powerUpType.createInstance((PlayState) state, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
deleted = true;
|
deleted = true;
|
||||||
|
@ -2,19 +2,19 @@ package com.me.brickbuster.entity;
|
|||||||
|
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.FieldState;
|
||||||
|
|
||||||
public abstract class Entity {
|
public abstract class Entity {
|
||||||
|
|
||||||
protected PlayState state;
|
protected FieldState state;
|
||||||
protected Vector2 pos;
|
protected Vector2 pos;
|
||||||
protected boolean deleted = false;
|
protected boolean deleted = false;
|
||||||
|
|
||||||
public Entity(PlayState state, Vector2 pos) {
|
public Entity(FieldState state, Vector2 pos) {
|
||||||
this(state, pos.x, pos.y);
|
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.state = state;
|
||||||
this.pos = new Vector2(x, y);
|
this.pos = new Vector2(x, y);
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ public class Paddle extends Entity implements PhysicsBody {
|
|||||||
}
|
}
|
||||||
if (!MathUtils.isZero(adjust)) {
|
if (!MathUtils.isZero(adjust)) {
|
||||||
setX(pos.x + adjust);
|
setX(pos.x + adjust);
|
||||||
for (Ball ball : state.balls) {
|
for (Ball ball : ((PlayState) state).balls) {
|
||||||
if (ball.isStuck()) {
|
if (ball.isStuck()) {
|
||||||
ball.setX(ball.getX() + adjust);
|
ball.setX(ball.getX() + adjust);
|
||||||
}
|
}
|
||||||
@ -128,7 +128,7 @@ public class Paddle extends Entity implements PhysicsBody {
|
|||||||
if (this.width == width) {
|
if (this.width == width) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
state.world.destroyBody(body);
|
((PlayState) state).world.destroyBody(body);
|
||||||
this.width = width;
|
this.width = width;
|
||||||
createBody();
|
createBody();
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ public class Shield extends Entity implements PhysicsBody {
|
|||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
if (deleted) {
|
if (deleted) {
|
||||||
state.removeShield(this);
|
((PlayState) state).removeShield(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ public class GluePowerUp extends PowerUp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void activate() {
|
public void activate() {
|
||||||
state.paddle.setSticky(true);
|
((PlayState) state).paddle.setSticky(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ public class LongerPaddlePowerUp extends PowerUp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void activate() {
|
public void activate() {
|
||||||
if (state.paddle.getWidth() < Paddle.DEFAULT_WIDTH*2.5) {
|
if (((PlayState) state).paddle.getWidth() < Paddle.DEFAULT_WIDTH*2.5) {
|
||||||
state.paddle.setWidth(state.paddle.getWidth() + Paddle.DEFAULT_WIDTH/2);
|
((PlayState) state).paddle.setWidth(((PlayState) state).paddle.getWidth() + Paddle.DEFAULT_WIDTH/2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,9 @@ public class MultiBallPowerUp extends PowerUp {
|
|||||||
@Override
|
@Override
|
||||||
public void activate() {
|
public void activate() {
|
||||||
for (int x = 0; x < 2; x++) {
|
for (int x = 0; x < 2; x++) {
|
||||||
Ball ball = new Ball(state);
|
Ball ball = new Ball((PlayState) state);
|
||||||
ball.launch();
|
ball.launch();
|
||||||
state.balls.add(ball);
|
((PlayState) state).balls.add(ball);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ public class ShieldPowerUp extends PowerUp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void activate() {
|
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;
|
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 PIXEL_PER_METER = 40f; // Board dimension: 54x96 meters
|
||||||
public static final float BOARD_WIDTH = 54f;
|
public static final float BOARD_WIDTH = 54f;
|
||||||
@ -30,8 +30,6 @@ public class PlayState extends State {
|
|||||||
public static final Vector2 upperLeftCorner =
|
public static final Vector2 upperLeftCorner =
|
||||||
new Vector2(EDGE_PADDING, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER-EDGE_PADDING);
|
new Vector2(EDGE_PADDING, BrickBuster.BOARD_HEIGHT/PIXEL_PER_METER-EDGE_PADDING);
|
||||||
|
|
||||||
public World world;
|
|
||||||
|
|
||||||
public Body playArea;
|
public Body playArea;
|
||||||
public Array<Body> bodies;
|
public Array<Body> bodies;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user