Compare commits

...

3 Commits

Author SHA1 Message Date
b3cbe0cda5 Remove magic value 2018-11-13 19:01:14 +04:00
e2c0bad9e0 Altered powerup weights (decreased glue, increased multi ball)
Allow bricks to have their colour changed.
2018-11-13 18:58:55 +04:00
ea6240bd2c Add PowerUpType enum
Handles weighted random seletion, color and instantiation, cleaning up
other sections of Brick and PlayState code.
2018-11-13 18:42:27 +04:00
11 changed files with 107 additions and 62 deletions

View File

@ -4,23 +4,32 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.powerup.PowerUp;
import com.me.brickbuster.entity.powerup.PowerUpType;
import com.me.brickbuster.state.PlayState;
public class Brick extends Entity {
public static final Color BLOCK_COLOR = Color.FOREST;
public static final Color DEFAULT_COLOR = Color.FOREST;
public static final int BRICK_WIDTH = 200;
public static final int BRICK_HEIGHT = 100;
private Class<? extends PowerUp> powerUpType;
private PowerUpType powerUpType;
private Color color;
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) {
this(state, powerUpType, true, x, y);
}
public Brick(PlayState state, PowerUpType powerUpType, boolean hidePowerup, int x, int y) {
this(state, powerUpType, DEFAULT_COLOR, hidePowerup, x, y);
}
public Brick(PlayState state, PowerUpType powerUpType, Color color, boolean hidePowerUp, int x, int y) {
super(state, x, y);
this.powerUpType = powerUpType;
this.color = powerUpType != null && !hidePowerUp? powerUpType.getColor() : color;
this.vertices = new Vector2[] {
new Vector2(x, y),
new Vector2(x + BRICK_WIDTH, y),
@ -32,7 +41,7 @@ public class Brick extends Entity {
@Override
public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled);
sr.setColor(BLOCK_COLOR);
sr.setColor(color);
sr.rect(getX(), getY(), BRICK_WIDTH, BRICK_HEIGHT);
sr.end();
}
@ -49,12 +58,7 @@ public class Brick extends Entity {
}
if (powerUpType != null) {
try {
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());
}
state.powerUps.add(powerUpType.createInstance(state, this));
}
return true;
}

View File

@ -9,6 +9,10 @@ public abstract class Entity {
protected PlayState state;
protected Vector2 pos;
public Entity(PlayState state, Vector2 pos) {
this(state, pos.x, pos.y);
}
public Entity(PlayState state, float x, float y) {
this.state = state;
this.pos = new Vector2(x, y);

View File

@ -12,6 +12,7 @@ import net.dermetfan.utils.Pair;
public class Paddle extends Entity {
public static final Color STICKY_COLOR = Color.GRAY;
public static final Color PADDLE_COLOR = Color.BLACK;
public static final int DEFAULT_WIDTH = 300;
public static final int PADDLE_HEIGHT = 30;
@ -28,7 +29,7 @@ public class Paddle extends Entity {
@Override
public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled);
sr.setColor(sticky? Color.GRAY : PADDLE_COLOR);
sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR);
sr.rect(getX() - width/2, getY(), width, PADDLE_HEIGHT);
sr.end();
}

View File

@ -1,13 +1,13 @@
package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.entity.Brick;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.state.PlayState;
public class GluePowerUp extends PowerUp {
public GluePowerUp(PlayState state, Brick brick) {
super(state, brick, Color.WHITE);
public GluePowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
}
@Override

View File

@ -1,14 +1,14 @@
package com.me.brickbuster.entity.powerup;
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.state.PlayState;
public class LongerPaddlePowerUp extends PowerUp {
public LongerPaddlePowerUp(PlayState state, Brick brick) {
super(state, brick, Color.OLIVE);
public LongerPaddlePowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
}
@Override

View File

@ -4,15 +4,14 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.PlayState;
public class MultiBallPowerUp extends PowerUp {
private Vector2 pos;
public MultiBallPowerUp(PlayState state, Brick brick) {
super(state, brick, Color.ROYAL);
public MultiBallPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
this.pos = getPos().cpy();
}

View File

@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.Utils;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.entity.Entity;
import com.me.brickbuster.state.PlayState;
import net.dermetfan.utils.Pair;
@ -18,8 +17,8 @@ public abstract class PowerUp extends Entity {
private Color color;
private boolean isCaught;
public PowerUp(PlayState state, Brick brick, Color color) {
super(state, brick.getX() + Brick.BRICK_WIDTH/2, brick.getY() + Brick.BRICK_HEIGHT/2);
public PowerUp(PlayState state, Vector2 pos, Color color) {
super(state, pos);
this.color = color;
}

View 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, 20),
LONGER_PADDLE(LongerPaddlePowerUp.class, Color.OLIVE, 40),
MULTI_BALL(MultiBallPowerUp.class, Color.ROYAL, 30),
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;
}
}

View File

@ -1,13 +1,13 @@
package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.entity.Brick;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.state.PlayState;
public class ShieldPowerUp extends PowerUp {
public ShieldPowerUp(PlayState state, Brick brick) {
super(state, brick, Color.SALMON);
public ShieldPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
}
@Override

View File

@ -20,16 +20,12 @@ public class PlayState extends State {
public static final int COLUMNS = 9;
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 Paddle paddle;
public List<Ball> balls;
public List<Brick> bricks;
private int shieldCount = 0;
private float updateTime = 0f;
public PlayState(BrickBuster game) {
@ -48,9 +44,9 @@ public class PlayState extends State {
int x = brick_padding + (col * (Brick.BRICK_WIDTH + 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)) {
powerUpType = getWeightedPowerUp();
powerUpType = PowerUpType.getWeightedRandom();
}
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);
}
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;
}
}

View File

@ -10,6 +10,8 @@ public class DesktopLauncher {
config.width = 450;
config.height = 800;
config.title = BrickBuster.TITLE;
//config.vSyncEnabled = false;
config.foregroundFPS = 0;
new LwjglApplication(new BrickBuster(), config);
}