Add LevelLoader interface and Level class.

Add GridLevelLoader implementation, use to load initial level
Add logic to return to main menu when there's no more levels, or load the next
the next level
This commit is contained in:
Matt Low 2018-11-20 12:17:42 +04:00
parent 1aeab73cd0
commit 989bb46d64
4 changed files with 121 additions and 41 deletions

View File

@ -0,0 +1,27 @@
package com.me.brickbuster.layout;
import com.me.brickbuster.state.PlayState;
public class GridLevelLoader implements LevelLoader {
public static final float POWER_UP_CHANCE = 0.15f;
public static final int COLUMNS = 10;
public static final int ROWS = 20;
public static final int ROUNDS = 2;
private PlayState state;
private int playCount = 0;
public GridLevelLoader(PlayState state) {
this.state = state;
}
@Override
public Level getNextLevel() {
if (playCount++ < ROUNDS) {
return new Level(new GridLayout(state, COLUMNS, ROWS, POWER_UP_CHANCE));
}
return null;
}
}

View File

@ -0,0 +1,33 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.graphics.Texture;
public class Level {
private final BrickLayout layout;
private final Texture background;
private final Texture border;
public Level(BrickLayout layout) {
this(layout, null, null);
}
public Level(BrickLayout layout, Texture background, Texture border) {
this.layout = layout;
this.background = background;
this.border = border;
}
public BrickLayout getLayout() {
return layout;
}
public Texture getBackground() {
return background;
}
public Texture getBorder() {
return border;
}
}

View File

@ -0,0 +1,7 @@
package com.me.brickbuster.layout;
public interface LevelLoader {
Level getNextLevel();
}

View File

@ -8,8 +8,7 @@ import com.badlogic.gdx.utils.Array;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.entity.*; import com.me.brickbuster.entity.*;
import com.me.brickbuster.entity.powerup.PowerUp; import com.me.brickbuster.entity.powerup.PowerUp;
import com.me.brickbuster.layout.BrickLayout; import com.me.brickbuster.layout.*;
import com.me.brickbuster.layout.GridLayout;
import com.me.brickbuster.physics.Box2dContactListener; import com.me.brickbuster.physics.Box2dContactListener;
import com.me.brickbuster.physics.EntityType; import com.me.brickbuster.physics.EntityType;
@ -22,10 +21,6 @@ public class PlayState extends State {
public static final float BOARD_HEIGHT = 96f; public static final float BOARD_HEIGHT = 96f;
public static final float EDGE_PADDING = .125f; public static final float EDGE_PADDING = .125f;
public static final int COLUMNS = 10;
public static final int ROWS = 20;
public static final float POWERUP_CHANCE = 0.15f;
public static final Vector2 lowerLeftCorner = public static final Vector2 lowerLeftCorner =
new Vector2(EDGE_PADDING,EDGE_PADDING); new Vector2(EDGE_PADDING,EDGE_PADDING);
public static final Vector2 lowerRightCorner = public static final Vector2 lowerRightCorner =
@ -40,6 +35,9 @@ public class PlayState extends State {
public Body playArea; public Body playArea;
public Array<Body> bodies; public Array<Body> bodies;
public LevelLoader levelLoader;
public Level currentLevel;
public Array<PowerUp> powerUps; public Array<PowerUp> powerUps;
public Paddle paddle; public Paddle paddle;
public Array<Ball> balls; public Array<Ball> balls;
@ -62,40 +60,10 @@ public class PlayState extends State {
world.setContactListener(new Box2dContactListener()); world.setContactListener(new Box2dContactListener());
bodies = new Array<Body>(); bodies = new Array<Body>();
levelLoader = new GridLevelLoader(this);
currentLevel = levelLoader.getNextLevel();
// Set up the borders initializeLevel();
BodyDef playAreaDef = new BodyDef();
playAreaDef.type = BodyDef.BodyType.StaticBody;
playAreaDef.position.set(new Vector2());
EdgeShape screenEdge = new EdgeShape();
FixtureDef playAreaFixture = new FixtureDef();
playAreaFixture.shape = screenEdge;
playAreaFixture.filter.categoryBits = EntityType.BOUNDARY;
playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK;
playArea = world.createBody(playAreaDef);
screenEdge.set(lowerRightCorner, upperRightCorner);
playArea.createFixture(playAreaFixture);
screenEdge.set(upperRightCorner, upperLeftCorner);
playArea.createFixture(playAreaFixture);
screenEdge.set(upperLeftCorner, lowerLeftCorner);
playArea.createFixture(playAreaFixture);
screenEdge.dispose();
powerUps = new Array<PowerUp>();
paddle = new Paddle(this);
BrickLayout layout = new GridLayout(this, COLUMNS, ROWS, POWERUP_CHANCE);
layout.initialize();
bricks = layout.getBricks();
balls = new Array<Ball>();
balls.add(new Ball(this));
shields = new Array<Shield>();
} }
@Override @Override
@ -178,8 +146,13 @@ public class PlayState extends State {
} }
} }
if (bricks.size == 0) { if (bricks.size == 0) {
game.setScreen(new MenuState(game)); currentLevel = levelLoader.getNextLevel();
dispose(); if (currentLevel == null) {
game.setScreen(new MenuState(game));
dispose();
} else {
initializeLevel();
}
return; return;
} }
@ -203,6 +176,46 @@ public class PlayState extends State {
paddle = null; paddle = null;
} }
private void initializeLevel() {
world.getBodies(bodies);
for (Body b : bodies) {
world.destroyBody(b);
}
// Set up the borders
BodyDef playAreaDef = new BodyDef();
playAreaDef.type = BodyDef.BodyType.StaticBody;
playAreaDef.position.set(new Vector2());
EdgeShape screenEdge = new EdgeShape();
FixtureDef playAreaFixture = new FixtureDef();
playAreaFixture.shape = screenEdge;
playAreaFixture.filter.categoryBits = EntityType.BOUNDARY;
playAreaFixture.filter.maskBits = EntityType.BALL | EntityType.BRICK;
playArea = world.createBody(playAreaDef);
screenEdge.set(lowerRightCorner, upperRightCorner);
playArea.createFixture(playAreaFixture);
screenEdge.set(upperRightCorner, upperLeftCorner);
playArea.createFixture(playAreaFixture);
screenEdge.set(upperLeftCorner, lowerLeftCorner);
playArea.createFixture(playAreaFixture);
screenEdge.dispose();
powerUps = new Array<PowerUp>();
paddle = new Paddle(this);
BrickLayout layout = currentLevel.getLayout();
layout.initialize();
bricks = layout.getBricks();
balls = new Array<Ball>();
balls.add(new Ball(this));
shields = new Array<Shield>();
}
public int getShieldCount() { public int getShieldCount() {
return shields.size; return shields.size;
} }