diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index b50d3a5..78e31b2 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -13,8 +13,7 @@ import com.me.brickbuster.entity.powerup.LongerPaddlePowerUp; import com.me.brickbuster.entity.powerup.MultiBallPowerUp; import com.me.brickbuster.entity.powerup.PowerUp; -import java.util.ArrayList; -import java.util.Iterator; +import java.util.*; public class BrickBuster extends ApplicationAdapter { @@ -25,6 +24,9 @@ public class BrickBuster extends ApplicationAdapter { public static final Vector2 HORIZONTAL_EDGE = new Vector2(1, 0); public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1); + private static final Map, Integer> powerUpWeights; + private static final int weightSum; + private BitmapFont font; private SpriteBatch batch; @@ -48,14 +50,8 @@ public class BrickBuster extends ApplicationAdapter { int x = 15 + (col * (Brick.BLOCK_WIDTH + 10)); int y = 15 + Brick.BLOCK_HEIGHT + (row * (Brick.BLOCK_HEIGHT + 10)); Class powerUpType = null; - if (MathUtils.randomBoolean(0.08f)) { - powerUpType = GluePowerUp.class; - } - else if (MathUtils.randomBoolean(0.08f)) { - powerUpType = LongerPaddlePowerUp.class; - } - else if (MathUtils.randomBoolean(0.04f)) { - powerUpType = MultiBallPowerUp.class; + if (MathUtils.randomBoolean(0.15f)) { + powerUpType = getWeightedPowerUp(); } bricks.add(new Brick(this, powerUpType, x, HEIGHT - y)); } @@ -136,6 +132,17 @@ public class BrickBuster extends ApplicationAdapter { public void dispose () { } + private static final Class getWeightedPowerUp() { + int remaining = MathUtils.random(weightSum); + for (Map.Entry, Integer> entry : powerUpWeights.entrySet()) { + remaining -= entry.getValue(); + if (remaining < 0) { + return entry.getKey(); + } + } + return null; + } + public ArrayList getBalls() { return balls; } @@ -152,4 +159,19 @@ public class BrickBuster extends ApplicationAdapter { this.powerUps.add(powerUp); } + static { + Map, Integer> tmp = new HashMap, Integer>(); + + // Assign PowerUp weights here + tmp.put(GluePowerUp.class, 30); + tmp.put(LongerPaddlePowerUp.class, 40); + tmp.put(MultiBallPowerUp.class, 20); + powerUpWeights = Collections.unmodifiableMap(tmp); + + int sum = 0; + for (int x : powerUpWeights.values()) { + sum += x; + } + weightSum = sum; + } } \ No newline at end of file