diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 6562b6b..9e2bc9a 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -165,6 +165,11 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { } } + float powerUpChance = ((PlayState) state).currentLevel.getRandomPowerUpChance(); + if (powerUpType == null && powerUpChance > 0f && MathUtils.randomBoolean(powerUpChance)) { + powerUpType = PowerUpType.getWeightedRandom(); + } + if (powerUpType != null) { ((PlayState) state).powerUps.add(powerUpType.createInstance((PlayState) state, this)); } diff --git a/core/src/com/me/brickbuster/layout/FileLayout.java b/core/src/com/me/brickbuster/layout/FileLayout.java index 3b3e693..e834134 100644 --- a/core/src/com/me/brickbuster/layout/FileLayout.java +++ b/core/src/com/me/brickbuster/layout/FileLayout.java @@ -1,30 +1,26 @@ 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.FieldState; import com.me.brickbuster.state.PlayState; public class FileLayout implements BrickLayout { - private static final Json JSON = new Json(); - - private PlayState state; + private FieldState state; + private LevelJsonTemplate template; private Array bricks; - private FileHandle file; - public FileLayout(PlayState state, FileHandle file) { + public FileLayout(FieldState state, LevelJsonTemplate template) { this.state = state; - this.file = file; + this.template = template; } @Override public void initialize() { bricks = new Array(); - LevelJsonTemplate level = JSON.fromJson(LevelJsonTemplate.class, file); - for (LevelJsonTemplate.BrickJsonTemplate brick : level.bricks) { + for (LevelJsonTemplate.BrickJsonTemplate brick : template.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; diff --git a/core/src/com/me/brickbuster/layout/FileLevelLoader.java b/core/src/com/me/brickbuster/layout/FileLevelLoader.java index 2163d23..65de6f5 100644 --- a/core/src/com/me/brickbuster/layout/FileLevelLoader.java +++ b/core/src/com/me/brickbuster/layout/FileLevelLoader.java @@ -2,14 +2,19 @@ package com.me.brickbuster.layout; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.utils.Json; +import com.me.brickbuster.state.FieldState; import com.me.brickbuster.state.PlayState; public class FileLevelLoader implements LevelLoader { - private PlayState state; + private static final Json JSON = new Json(); + + private FieldState state; private int level = 1; - public FileLevelLoader(PlayState state) { + public FileLevelLoader(FieldState state) { this.state = state; } @@ -17,8 +22,15 @@ public class FileLevelLoader implements LevelLoader { public Level getNextLevel() { FileHandle file = Gdx.files.internal("levels/level_" + level + ".json"); if (file.exists()) { + LevelJsonTemplate template = JSON.fromJson(LevelJsonTemplate.class, file); + + Texture background = null; + if (template.backgroundFile != null && !"".equals(template.backgroundFile)) { + background = new Texture(Gdx.files.internal(template.backgroundFile)); + } + level++; - return new Level(new FileLayout(state, file), null, null); + return new Level(new FileLayout(state, template), background, template.randomPowerUpChance); } return null; } diff --git a/core/src/com/me/brickbuster/layout/Level.java b/core/src/com/me/brickbuster/layout/Level.java index a586c41..d38fce2 100644 --- a/core/src/com/me/brickbuster/layout/Level.java +++ b/core/src/com/me/brickbuster/layout/Level.java @@ -6,16 +6,16 @@ public class Level { private final BrickLayout layout; private final Texture background; - private final Texture border; + private final float randomPowerUpChance; public Level(BrickLayout layout) { - this(layout, null, null); + this(layout, null, 0f); } - public Level(BrickLayout layout, Texture background, Texture border) { + public Level(BrickLayout layout, Texture background, float randomPowerUpChance) { this.layout = layout; this.background = background; - this.border = border; + this.randomPowerUpChance = randomPowerUpChance; } public BrickLayout getLayout() { @@ -26,8 +26,8 @@ public class Level { return background; } - public Texture getBorder() { - return border; + public float getRandomPowerUpChance() { + return randomPowerUpChance; } } diff --git a/core/src/com/me/brickbuster/layout/LevelJsonTemplate.java b/core/src/com/me/brickbuster/layout/LevelJsonTemplate.java index 0b12931..6bd6d2a 100644 --- a/core/src/com/me/brickbuster/layout/LevelJsonTemplate.java +++ b/core/src/com/me/brickbuster/layout/LevelJsonTemplate.java @@ -9,7 +9,7 @@ import java.util.List; public class LevelJsonTemplate { public String backgroundFile; - public String borderFile; + public float randomPowerUpChance; public List bricks; public static class BrickJsonTemplate {