From 989bb46d64c7f7585b4335e74c565d929b507660 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 20 Nov 2018 12:17:42 +0400 Subject: [PATCH] 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 --- .../brickbuster/layout/GridLevelLoader.java | 27 ++++++ core/src/com/me/brickbuster/layout/Level.java | 33 +++++++ .../me/brickbuster/layout/LevelLoader.java | 7 ++ .../com/me/brickbuster/state/PlayState.java | 95 +++++++++++-------- 4 files changed, 121 insertions(+), 41 deletions(-) create mode 100644 core/src/com/me/brickbuster/layout/GridLevelLoader.java create mode 100644 core/src/com/me/brickbuster/layout/Level.java create mode 100644 core/src/com/me/brickbuster/layout/LevelLoader.java diff --git a/core/src/com/me/brickbuster/layout/GridLevelLoader.java b/core/src/com/me/brickbuster/layout/GridLevelLoader.java new file mode 100644 index 0000000..80249f0 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/GridLevelLoader.java @@ -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; + } + +} diff --git a/core/src/com/me/brickbuster/layout/Level.java b/core/src/com/me/brickbuster/layout/Level.java new file mode 100644 index 0000000..a586c41 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/Level.java @@ -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; + } + +} diff --git a/core/src/com/me/brickbuster/layout/LevelLoader.java b/core/src/com/me/brickbuster/layout/LevelLoader.java new file mode 100644 index 0000000..8db5ae7 --- /dev/null +++ b/core/src/com/me/brickbuster/layout/LevelLoader.java @@ -0,0 +1,7 @@ +package com.me.brickbuster.layout; + +public interface LevelLoader { + + Level getNextLevel(); + +} diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index c39c7c2..73f5c86 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -8,8 +8,7 @@ import com.badlogic.gdx.utils.Array; import com.me.brickbuster.BrickBuster; import com.me.brickbuster.entity.*; import com.me.brickbuster.entity.powerup.PowerUp; -import com.me.brickbuster.layout.BrickLayout; -import com.me.brickbuster.layout.GridLayout; +import com.me.brickbuster.layout.*; import com.me.brickbuster.physics.Box2dContactListener; 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 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 = new Vector2(EDGE_PADDING,EDGE_PADDING); public static final Vector2 lowerRightCorner = @@ -40,6 +35,9 @@ public class PlayState extends State { public Body playArea; public Array bodies; + public LevelLoader levelLoader; + public Level currentLevel; + public Array powerUps; public Paddle paddle; public Array balls; @@ -62,40 +60,10 @@ public class PlayState extends State { world.setContactListener(new Box2dContactListener()); bodies = new Array(); + levelLoader = new GridLevelLoader(this); + currentLevel = levelLoader.getNextLevel(); - // 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(); - paddle = new Paddle(this); - - BrickLayout layout = new GridLayout(this, COLUMNS, ROWS, POWERUP_CHANCE); - layout.initialize(); - - bricks = layout.getBricks(); - - balls = new Array(); - balls.add(new Ball(this)); - - shields = new Array(); + initializeLevel(); } @Override @@ -178,8 +146,13 @@ public class PlayState extends State { } } if (bricks.size == 0) { - game.setScreen(new MenuState(game)); - dispose(); + currentLevel = levelLoader.getNextLevel(); + if (currentLevel == null) { + game.setScreen(new MenuState(game)); + dispose(); + } else { + initializeLevel(); + } return; } @@ -203,6 +176,46 @@ public class PlayState extends State { 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(); + paddle = new Paddle(this); + + BrickLayout layout = currentLevel.getLayout(); + layout.initialize(); + bricks = layout.getBricks(); + + balls = new Array(); + balls.add(new Ball(this)); + + shields = new Array(); + } + public int getShieldCount() { return shields.size; }