Add FileLevelLoader and FileLayout to load levels from json files

Add two levels to assets
This commit is contained in:
Matt Low 2018-11-22 17:30:48 +04:00
parent 90b92b1026
commit 36ae71afeb
5 changed files with 1024 additions and 2 deletions

View File

@ -0,0 +1,358 @@
{
bricks: [
{
row: 15
type: HARD
shape: RECTANGLE
}
{
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 1
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 1
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 1
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 1
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 1
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 1
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 2
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 2
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 2
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 2
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 2
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 2
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 3
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 3
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 3
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 3
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 3
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 3
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 4
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 4
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 4
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 4
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 4
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 4
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 5
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 5
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 5
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 5
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 5
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 5
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 6
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 6
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 6
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 6
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 6
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 6
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 7
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 7
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 7
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 7
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 7
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 7
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 8
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 8
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 8
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 8
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 8
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 8
row: 20
type: STANDARD_40
shape: RECTANGLE
}
{
col: 9
row: 15
type: HARD
shape: RECTANGLE
}
{
col: 9
row: 16
type: STANDARD_50
shape: RECTANGLE
}
{
col: 9
row: 17
type: STANDARD_80
shape: RECTANGLE
}
{
col: 9
row: 18
type: STANDARD_60
shape: RECTANGLE
}
{
col: 9
row: 19
type: STANDARD_70
shape: RECTANGLE
}
{
col: 9
row: 20
type: STANDARD_40
shape: RECTANGLE
}
]
}

View File

@ -0,0 +1,598 @@
{
bricks: [
{
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
row: 6
type: STANDARD_40
shape: DIAMOND
}
{
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
row: 9
type: STANDARD_40
shape: FULL_LEFT_UP_RIGHT_TRIANGLE
}
{
row: 10
type: STANDARD_40
shape: RIGHT_UP_RIGHT_TRIANGLE
}
{
col: 1
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 1
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 1
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
col: 1
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 1
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 1
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 1
row: 11
type: STANDARD_40
shape: FULL_LEFT_UP_RIGHT_TRIANGLE
}
{
col: 1
row: 12
type: STANDARD_40
shape: RIGHT_UP_RIGHT_TRIANGLE
}
{
col: 2
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 2
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 2
row: 7
type: STANDARD_50
shape: DIAMOND
}
{
col: 2
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 2
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 2
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 2
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 2
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 2
row: 13
type: STANDARD_40
shape: FULL_LEFT_UP_RIGHT_TRIANGLE
}
{
col: 2
row: 14
type: STANDARD_40
shape: RIGHT_UP_RIGHT_TRIANGLE
}
{
col: 3
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 3
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 3
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
col: 3
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 3
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 3
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 3
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 3
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 3
row: 13
type: STANDARD_80
shape: RECTANGLE
}
{
col: 3
row: 14
type: STANDARD_80
shape: RECTANGLE
}
{
col: 3
row: 15
type: STANDARD_40
shape: FULL_LEFT_UP_RIGHT_TRIANGLE
}
{
col: 3
row: 16
type: STANDARD_40
shape: RIGHT_UP_RIGHT_TRIANGLE
}
{
col: 4
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 4
row: 6
type: STANDARD_40
shape: DIAMOND
}
{
col: 4
row: 7
type: STANDARD_50
shape: DIAMOND
}
{
col: 4
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 4
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 4
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 4
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 4
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 4
row: 13
type: STANDARD_80
shape: RECTANGLE
}
{
col: 4
row: 14
type: STANDARD_80
shape: RECTANGLE
}
{
col: 4
row: 15
type: STANDARD_70
shape: RECTANGLE
}
{
col: 4
row: 16
type: STANDARD_60
shape: RECTANGLE
}
{
col: 4
row: 17
type: STANDARD_40
shape: FULL_LEFT_UP_RIGHT_TRIANGLE
}
{
col: 4
row: 18
type: STANDARD_40
shape: RIGHT_UP_RIGHT_TRIANGLE
}
{
col: 5
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 5
row: 6
type: STANDARD_40
shape: DIAMOND
}
{
col: 5
row: 7
type: STANDARD_50
shape: DIAMOND
}
{
col: 5
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 5
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 5
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 5
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 5
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 5
row: 13
type: STANDARD_80
shape: RECTANGLE
}
{
col: 5
row: 14
type: STANDARD_80
shape: RECTANGLE
}
{
col: 5
row: 15
type: STANDARD_70
shape: RECTANGLE
}
{
col: 5
row: 16
type: STANDARD_60
shape: RECTANGLE
}
{
col: 5
row: 17
type: STANDARD_40
shape: FULL_UP_LEFT_TRIANGLE
}
{
col: 5
row: 18
type: STANDARD_40
shape: LEFT_UP_LEFT_TRIANGLE
}
{
col: 6
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 6
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 6
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
col: 6
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 6
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 6
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 6
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 6
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 6
row: 13
type: STANDARD_80
shape: RECTANGLE
}
{
col: 6
row: 14
type: STANDARD_80
shape: RECTANGLE
}
{
col: 6
row: 15
type: STANDARD_40
shape: FULL_UP_LEFT_TRIANGLE
}
{
col: 6
row: 16
type: STANDARD_40
shape: LEFT_UP_LEFT_TRIANGLE
}
{
col: 7
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 7
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 7
row: 7
type: STANDARD_50
shape: DIAMOND
}
{
col: 7
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 7
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 7
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 7
row: 11
type: STANDARD_50
shape: RECTANGLE
}
{
col: 7
row: 12
type: STANDARD_50
shape: RECTANGLE
}
{
col: 7
row: 13
type: STANDARD_40
shape: FULL_UP_LEFT_TRIANGLE
}
{
col: 7
row: 14
type: STANDARD_40
shape: LEFT_UP_LEFT_TRIANGLE
}
{
col: 8
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 8
row: 6
type: STANDARD_60
shape: DIAMOND
}
{
col: 8
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
col: 8
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 8
row: 9
type: STANDARD_50
shape: RECTANGLE
}
{
col: 8
row: 10
type: STANDARD_50
shape: RECTANGLE
}
{
col: 8
row: 11
type: STANDARD_40
shape: FULL_UP_LEFT_TRIANGLE
}
{
col: 8
row: 12
type: STANDARD_40
shape: LEFT_UP_LEFT_TRIANGLE
}
{
col: 9
row: 5
type: STANDARD_40
shape: HALF_LOWER_RECTANGLE
}
{
col: 9
row: 6
type: STANDARD_40
shape: DIAMOND
}
{
col: 9
row: 7
type: STANDARD_40
shape: DIAMOND
}
{
col: 9
row: 8
type: STANDARD_40
shape: RECTANGLE
}
{
col: 9
row: 9
type: STANDARD_40
shape: FULL_UP_LEFT_TRIANGLE
}
{
col: 9
row: 10
type: STANDARD_40
shape: LEFT_UP_LEFT_TRIANGLE
}
]
}

View File

@ -0,0 +1,41 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.PlayState;
public class FileLayout implements BrickLayout {
private static final Json JSON = new Json();
private PlayState state;
private Array<Brick> bricks;
private FileHandle file;
public FileLayout(PlayState state, FileHandle file) {
this.state = state;
this.file = file;
}
@Override
public void initialize() {
bricks = new Array<Brick>();
LevelJsonTemplate level = JSON.fromJson(LevelJsonTemplate.class, file);
for (LevelJsonTemplate.BrickJsonTemplate brick : level.bricks) {
final float x = PlayState.EDGE_PADDING + Brick.BRICK_WIDTH/2 + brick.col * Brick.BRICK_WIDTH;
final float y = PlayState.EDGE_PADDING + Brick.BRICK_HEIGHT/2 + brick.row * Brick.BRICK_HEIGHT;
bricks.add(new Brick(state, brick.type, brick.shape, brick.powerUp, x, PlayState.BOARD_HEIGHT - y));
}
}
@Override
public Array<Brick> getBricks() {
return bricks;
}
}

View File

@ -0,0 +1,26 @@
package com.me.brickbuster.layout;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.me.brickbuster.state.PlayState;
public class FileLevelLoader implements LevelLoader {
private PlayState state;
private int level = 1;
public FileLevelLoader(PlayState state) {
this.state = state;
}
@Override
public Level getNextLevel() {
FileHandle file = Gdx.files.internal("levels/level_" + level + ".json");
if (file.exists()) {
level++;
return new Level(new FileLayout(state, file), null, null);
}
return null;
}
}

View File

@ -62,8 +62,7 @@ public class PlayState extends FieldState {
world.setContactListener(new Box2dContactListener()); world.setContactListener(new Box2dContactListener());
bodies = new Array<Body>(); bodies = new Array<Body>();
//levelLoader = new FileLevelLoader(this); levelLoader = new FileLevelLoader(this);
levelLoader = new GridLevelLoader(this);
currentLevel = levelLoader.getNextLevel(); currentLevel = levelLoader.getNextLevel();
initializeLevel(); initializeLevel();