Use a weighted random choice for powerup selection, 15% of power up

This commit is contained in:
Matt Low 2018-11-12 20:39:59 +04:00
parent 4694947a08
commit 393b2fae58

View File

@ -13,8 +13,7 @@ import com.me.brickbuster.entity.powerup.LongerPaddlePowerUp;
import com.me.brickbuster.entity.powerup.MultiBallPowerUp; import com.me.brickbuster.entity.powerup.MultiBallPowerUp;
import com.me.brickbuster.entity.powerup.PowerUp; import com.me.brickbuster.entity.powerup.PowerUp;
import java.util.ArrayList; import java.util.*;
import java.util.Iterator;
public class BrickBuster extends ApplicationAdapter { 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 HORIZONTAL_EDGE = new Vector2(1, 0);
public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1); public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1);
private static final Map<Class<? extends PowerUp>, Integer> powerUpWeights;
private static final int weightSum;
private BitmapFont font; private BitmapFont font;
private SpriteBatch batch; private SpriteBatch batch;
@ -48,14 +50,8 @@ public class BrickBuster extends ApplicationAdapter {
int x = 15 + (col * (Brick.BLOCK_WIDTH + 10)); int x = 15 + (col * (Brick.BLOCK_WIDTH + 10));
int y = 15 + Brick.BLOCK_HEIGHT + (row * (Brick.BLOCK_HEIGHT + 10)); int y = 15 + Brick.BLOCK_HEIGHT + (row * (Brick.BLOCK_HEIGHT + 10));
Class<? extends PowerUp> powerUpType = null; Class<? extends PowerUp> powerUpType = null;
if (MathUtils.randomBoolean(0.08f)) { if (MathUtils.randomBoolean(0.15f)) {
powerUpType = GluePowerUp.class; powerUpType = getWeightedPowerUp();
}
else if (MathUtils.randomBoolean(0.08f)) {
powerUpType = LongerPaddlePowerUp.class;
}
else if (MathUtils.randomBoolean(0.04f)) {
powerUpType = MultiBallPowerUp.class;
} }
bricks.add(new Brick(this, powerUpType, x, HEIGHT - y)); bricks.add(new Brick(this, powerUpType, x, HEIGHT - y));
} }
@ -136,6 +132,17 @@ public class BrickBuster extends ApplicationAdapter {
public void dispose () { public void dispose () {
} }
private static final Class<? extends PowerUp> getWeightedPowerUp() {
int remaining = MathUtils.random(weightSum);
for (Map.Entry<Class<? extends PowerUp>, Integer> entry : powerUpWeights.entrySet()) {
remaining -= entry.getValue();
if (remaining < 0) {
return entry.getKey();
}
}
return null;
}
public ArrayList<Ball> getBalls() { public ArrayList<Ball> getBalls() {
return balls; return balls;
} }
@ -152,4 +159,19 @@ public class BrickBuster extends ApplicationAdapter {
this.powerUps.add(powerUp); this.powerUps.add(powerUp);
} }
static {
Map<Class<? extends PowerUp>, Integer> tmp = new HashMap<Class<? extends PowerUp>, 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;
}
} }