Add PowerUpType enum
Handles weighted random seletion, color and instantiation, cleaning up other sections of Brick and PlayState code.
This commit is contained in:
parent
0abae880d3
commit
ea6240bd2c
@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.entity.powerup.PowerUp;
|
import com.me.brickbuster.entity.powerup.PowerUp;
|
||||||
|
import com.me.brickbuster.entity.powerup.PowerUpType;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class Brick extends Entity {
|
public class Brick extends Entity {
|
||||||
@ -13,11 +14,11 @@ public class Brick extends Entity {
|
|||||||
public static final int BRICK_WIDTH = 200;
|
public static final int BRICK_WIDTH = 200;
|
||||||
public static final int BRICK_HEIGHT = 100;
|
public static final int BRICK_HEIGHT = 100;
|
||||||
|
|
||||||
private Class<? extends PowerUp> powerUpType;
|
private PowerUpType powerUpType;
|
||||||
|
|
||||||
private Vector2[] vertices;
|
private Vector2[] vertices;
|
||||||
|
|
||||||
public Brick(PlayState state, Class<? extends PowerUp> powerUpType, int x, int y) {
|
public Brick(PlayState state, PowerUpType powerUpType, int x, int y) {
|
||||||
super(state, x, y);
|
super(state, x, y);
|
||||||
this.powerUpType = powerUpType;
|
this.powerUpType = powerUpType;
|
||||||
|
|
||||||
@ -49,12 +50,7 @@ public class Brick extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (powerUpType != null) {
|
if (powerUpType != null) {
|
||||||
try {
|
state.powerUps.add(powerUpType.createInstance(state, this));
|
||||||
PowerUp powerUp = powerUpType.getConstructor(PlayState.class, Brick.class).newInstance(state, this);
|
|
||||||
state.powerUps.add(powerUp);
|
|
||||||
} catch(Exception e) {
|
|
||||||
System.out.println("Error spawning powerup: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,10 @@ public abstract class Entity {
|
|||||||
protected PlayState state;
|
protected PlayState state;
|
||||||
protected Vector2 pos;
|
protected Vector2 pos;
|
||||||
|
|
||||||
|
public Entity(PlayState state, Vector2 pos) {
|
||||||
|
this(state, pos.x, pos.y);
|
||||||
|
}
|
||||||
|
|
||||||
public Entity(PlayState state, float x, float y) {
|
public Entity(PlayState state, float x, float y) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.pos = new Vector2(x, y);
|
this.pos = new Vector2(x, y);
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.me.brickbuster.entity.powerup;
|
package com.me.brickbuster.entity.powerup;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.me.brickbuster.entity.Brick;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class GluePowerUp extends PowerUp {
|
public class GluePowerUp extends PowerUp {
|
||||||
|
|
||||||
public GluePowerUp(PlayState state, Brick brick) {
|
public GluePowerUp(PlayState state, Vector2 brick, Color color) {
|
||||||
super(state, brick, Color.WHITE);
|
super(state, brick, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package com.me.brickbuster.entity.powerup;
|
package com.me.brickbuster.entity.powerup;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.me.brickbuster.entity.Brick;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.entity.Paddle;
|
import com.me.brickbuster.entity.Paddle;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class LongerPaddlePowerUp extends PowerUp {
|
public class LongerPaddlePowerUp extends PowerUp {
|
||||||
|
|
||||||
public LongerPaddlePowerUp(PlayState state, Brick brick) {
|
public LongerPaddlePowerUp(PlayState state, Vector2 brick, Color color) {
|
||||||
super(state, brick, Color.OLIVE);
|
super(state, brick, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,15 +4,14 @@ import com.badlogic.gdx.graphics.Color;
|
|||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.entity.Ball;
|
import com.me.brickbuster.entity.Ball;
|
||||||
import com.me.brickbuster.entity.Brick;
|
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class MultiBallPowerUp extends PowerUp {
|
public class MultiBallPowerUp extends PowerUp {
|
||||||
|
|
||||||
private Vector2 pos;
|
private Vector2 pos;
|
||||||
|
|
||||||
public MultiBallPowerUp(PlayState state, Brick brick) {
|
public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) {
|
||||||
super(state, brick, Color.ROYAL);
|
super(state, brick, color);
|
||||||
this.pos = getPos().cpy();
|
this.pos = getPos().cpy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.Utils;
|
import com.me.brickbuster.Utils;
|
||||||
import com.me.brickbuster.entity.Brick;
|
|
||||||
import com.me.brickbuster.entity.Entity;
|
import com.me.brickbuster.entity.Entity;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
import net.dermetfan.utils.Pair;
|
import net.dermetfan.utils.Pair;
|
||||||
@ -18,8 +17,8 @@ public abstract class PowerUp extends Entity {
|
|||||||
private Color color;
|
private Color color;
|
||||||
private boolean isCaught;
|
private boolean isCaught;
|
||||||
|
|
||||||
public PowerUp(PlayState state, Brick brick, Color color) {
|
public PowerUp(PlayState state, Vector2 pos, Color color) {
|
||||||
super(state, brick.getX() + Brick.BRICK_WIDTH/2, brick.getY() + Brick.BRICK_HEIGHT/2);
|
super(state, pos);
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
68
core/src/com/me/brickbuster/entity/powerup/PowerUpType.java
Normal file
68
core/src/com/me/brickbuster/entity/powerup/PowerUpType.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package com.me.brickbuster.entity.powerup;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.Color;
|
||||||
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.me.brickbuster.entity.Brick;
|
||||||
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
|
public enum PowerUpType {
|
||||||
|
|
||||||
|
GLUE(GluePowerUp.class, Color.WHITE, 30),
|
||||||
|
LONGER_PADDLE(LongerPaddlePowerUp.class, Color.OLIVE, 40),
|
||||||
|
MULTI_BALL(MultiBallPowerUp.class, Color.ROYAL, 20),
|
||||||
|
SHIELD(ShieldPowerUp.class, Color.SALMON, 40),
|
||||||
|
;
|
||||||
|
|
||||||
|
private static final int WEIGHT_SUM;
|
||||||
|
|
||||||
|
private Class<? extends PowerUp> cls;
|
||||||
|
private Color color;
|
||||||
|
private int weight;
|
||||||
|
|
||||||
|
PowerUpType(Class<? extends PowerUp> cls, Color color, int weight) {
|
||||||
|
this.cls = cls;
|
||||||
|
this.color = color;
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PowerUpType getWeightedRandom() {
|
||||||
|
int distance = MathUtils.random(WEIGHT_SUM);
|
||||||
|
for (PowerUpType type : values()) {
|
||||||
|
distance -= type.weight;
|
||||||
|
if (distance < 0) {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PowerUp createInstance(PlayState state, Brick brick) {
|
||||||
|
return createInstance(state,
|
||||||
|
new Vector2(brick.getX()+Brick.BRICK_WIDTH/2,
|
||||||
|
brick.getY()+Brick.BRICK_HEIGHT/2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public PowerUp createInstance(PlayState state, Vector2 pos) {
|
||||||
|
try {
|
||||||
|
return cls.getConstructor(PlayState.class, Vector2.class, Color.class)
|
||||||
|
.newInstance(state, pos, color);
|
||||||
|
} catch(Exception e) {
|
||||||
|
System.out.println("Error instantiating PoewrUp: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
int weightSum = 0;
|
||||||
|
for (PowerUpType type : values()) {
|
||||||
|
weightSum += type.weight;
|
||||||
|
}
|
||||||
|
WEIGHT_SUM = weightSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,13 +1,13 @@
|
|||||||
package com.me.brickbuster.entity.powerup;
|
package com.me.brickbuster.entity.powerup;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.me.brickbuster.entity.Brick;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.state.PlayState;
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class ShieldPowerUp extends PowerUp {
|
public class ShieldPowerUp extends PowerUp {
|
||||||
|
|
||||||
public ShieldPowerUp(PlayState state, Brick brick) {
|
public ShieldPowerUp(PlayState state, Vector2 brick, Color color) {
|
||||||
super(state, brick, Color.SALMON);
|
super(state, brick, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -20,16 +20,12 @@ public class PlayState extends State {
|
|||||||
public static final int COLUMNS = 9;
|
public static final int COLUMNS = 9;
|
||||||
public static final int ROWS = 8;
|
public static final int ROWS = 8;
|
||||||
|
|
||||||
public static final Map<Class<? extends PowerUp>, Integer> powerUpWeights;
|
|
||||||
private static final int weightSum;
|
|
||||||
|
|
||||||
public List<PowerUp> powerUps;
|
public List<PowerUp> powerUps;
|
||||||
public Paddle paddle;
|
public Paddle paddle;
|
||||||
public List<Ball> balls;
|
public List<Ball> balls;
|
||||||
public List<Brick> bricks;
|
public List<Brick> bricks;
|
||||||
|
|
||||||
private int shieldCount = 0;
|
private int shieldCount = 0;
|
||||||
|
|
||||||
private float updateTime = 0f;
|
private float updateTime = 0f;
|
||||||
|
|
||||||
public PlayState(BrickBuster game) {
|
public PlayState(BrickBuster game) {
|
||||||
@ -48,9 +44,9 @@ public class PlayState extends State {
|
|||||||
int x = brick_padding + (col * (Brick.BRICK_WIDTH + brick_padding));
|
int x = brick_padding + (col * (Brick.BRICK_WIDTH + brick_padding));
|
||||||
int y = brick_padding + Brick.BRICK_HEIGHT + (row * (Brick.BRICK_HEIGHT + brick_padding));
|
int y = brick_padding + Brick.BRICK_HEIGHT + (row * (Brick.BRICK_HEIGHT + brick_padding));
|
||||||
|
|
||||||
Class<? extends PowerUp> powerUpType = null;
|
PowerUpType powerUpType = null;
|
||||||
if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
|
if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
|
||||||
powerUpType = getWeightedPowerUp();
|
powerUpType = PowerUpType.getWeightedRandom();
|
||||||
}
|
}
|
||||||
|
|
||||||
bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT - y));
|
bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT - y));
|
||||||
@ -167,32 +163,4 @@ public class PlayState extends State {
|
|||||||
paddle.setWidth(Paddle.DEFAULT_WIDTH);
|
paddle.setWidth(Paddle.DEFAULT_WIDTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Class<? extends PowerUp> getWeightedPowerUp() {
|
|
||||||
int remaining = MathUtils.random(weightSum);
|
|
||||||
for (Map.Entry<Class<? extends PowerUp>, Integer> entry : PlayState.powerUpWeights.entrySet()) {
|
|
||||||
remaining -= entry.getValue();
|
|
||||||
if (remaining < 0) {
|
|
||||||
return entry.getKey();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
tmp.put(ShieldPowerUp.class, 40);
|
|
||||||
powerUpWeights = Collections.unmodifiableMap(tmp);
|
|
||||||
|
|
||||||
int sum = 0;
|
|
||||||
for (int x : PlayState.powerUpWeights.values()) {
|
|
||||||
sum += x;
|
|
||||||
}
|
|
||||||
weightSum = sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ public class DesktopLauncher {
|
|||||||
config.width = 450;
|
config.width = 450;
|
||||||
config.height = 800;
|
config.height = 800;
|
||||||
config.title = BrickBuster.TITLE;
|
config.title = BrickBuster.TITLE;
|
||||||
|
//config.vSyncEnabled = false;
|
||||||
|
config.foregroundFPS = 0;
|
||||||
new LwjglApplication(new BrickBuster(), config);
|
new LwjglApplication(new BrickBuster(), config);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user