From 36ae71afebce00897129c86ec0b4e7daf2ad35c1 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 22 Nov 2018 17:30:48 +0400 Subject: [PATCH] Add FileLevelLoader and FileLayout to load levels from json files Add two levels to assets --- core/assets/levels/level_1.json | 358 +++++++++++ core/assets/levels/level_2.json | 598 ++++++++++++++++++ .../com/me/brickbuster/layout/FileLayout.java | 41 ++ .../brickbuster/layout/FileLevelLoader.java | 26 + .../com/me/brickbuster/state/PlayState.java | 3 +- 5 files changed, 1024 insertions(+), 2 deletions(-) create mode 100644 core/assets/levels/level_1.json create mode 100644 core/assets/levels/level_2.json create mode 100644 core/src/com/me/brickbuster/layout/FileLayout.java create mode 100644 core/src/com/me/brickbuster/layout/FileLevelLoader.java diff --git a/core/assets/levels/level_1.json b/core/assets/levels/level_1.json new file mode 100644 index 0000000..aa91465 --- /dev/null +++ b/core/assets/levels/level_1.json @@ -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 + } +] +} \ No newline at end of file diff --git a/core/assets/levels/level_2.json b/core/assets/levels/level_2.json new file mode 100644 index 0000000..0f5049a --- /dev/null +++ b/core/assets/levels/level_2.json @@ -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 + } +] +} \ No newline at end of file diff --git a/core/src/com/me/brickbuster/layout/FileLayout.java b/core/src/com/me/brickbuster/layout/FileLayout.java new file mode 100644 index 0000000..3b3e693 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/FileLayout.java @@ -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 bricks; + private FileHandle file; + + public FileLayout(PlayState state, FileHandle file) { + this.state = state; + this.file = file; + } + + @Override + public void initialize() { + bricks = new Array(); + + 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 getBricks() { + return bricks; + } + + +} diff --git a/core/src/com/me/brickbuster/layout/FileLevelLoader.java b/core/src/com/me/brickbuster/layout/FileLevelLoader.java new file mode 100644 index 0000000..2163d23 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/FileLevelLoader.java @@ -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; + } + +} diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 0012fec..9bfa66d 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -62,8 +62,7 @@ public class PlayState extends FieldState { world.setContactListener(new Box2dContactListener()); bodies = new Array(); - //levelLoader = new FileLevelLoader(this); - levelLoader = new GridLevelLoader(this); + levelLoader = new FileLevelLoader(this); currentLevel = levelLoader.getNextLevel(); initializeLevel();