Compare commits

..

No commits in common. "b3cbe0cda515ea6c50d0c58c3c7506608ded5015" and "0abae880d3d987e12bd22c4cd9ebf6d4d63931da" have entirely different histories.

11 changed files with 62 additions and 107 deletions

View File

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

View File

@ -9,10 +9,6 @@ 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,7 +12,6 @@ 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;
@ -29,7 +28,7 @@ public class Paddle extends Entity {
@Override
public void render(ShapeRenderer sr) {
sr.begin(ShapeType.Filled);
sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR);
sr.setColor(sticky? Color.GRAY : 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.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.PlayState;
public class GluePowerUp extends PowerUp {
public GluePowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
public GluePowerUp(PlayState state, Brick brick) {
super(state, brick, Color.WHITE);
}
@Override

View File

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

View File

@ -4,14 +4,15 @@ 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, Vector2 brick, Color color) {
super(state, brick, color);
public MultiBallPowerUp(PlayState state, Brick brick) {
super(state, brick, Color.ROYAL);
this.pos = getPos().cpy();
}

View File

@ -5,6 +5,7 @@ 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;
@ -17,8 +18,8 @@ public abstract class PowerUp extends Entity {
private Color color;
private boolean isCaught;
public PowerUp(PlayState state, Vector2 pos, Color color) {
super(state, pos);
public PowerUp(PlayState state, Brick brick, Color color) {
super(state, brick.getX() + Brick.BRICK_WIDTH/2, brick.getY() + Brick.BRICK_HEIGHT/2);
this.color = color;
}

View File

@ -1,68 +0,0 @@
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.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.state.PlayState;
public class ShieldPowerUp extends PowerUp {
public ShieldPowerUp(PlayState state, Vector2 brick, Color color) {
super(state, brick, color);
public ShieldPowerUp(PlayState state, Brick brick) {
super(state, brick, Color.SALMON);
}
@Override

View File

@ -20,12 +20,16 @@ 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) {
@ -44,9 +48,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));
PowerUpType powerUpType = null;
Class<? extends PowerUp> powerUpType = null;
if (MathUtils.randomBoolean(POWERUP_CHANCE)) {
powerUpType = PowerUpType.getWeightedRandom();
powerUpType = getWeightedPowerUp();
}
bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT - y));
@ -163,4 +167,32 @@ 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,8 +10,6 @@ public class DesktopLauncher {
config.width = 450;
config.height = 800;
config.title = BrickBuster.TITLE;
//config.vSyncEnabled = false;
config.foregroundFPS = 0;
new LwjglApplication(new BrickBuster(), config);
}