From ea6240bd2c842b0d62ef47858b17195216d7d7ba Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 13 Nov 2018 18:42:27 +0400 Subject: [PATCH] Add PowerUpType enum Handles weighted random seletion, color and instantiation, cleaning up other sections of Brick and PlayState code. --- core/src/com/me/brickbuster/entity/Brick.java | 12 ++-- .../src/com/me/brickbuster/entity/Entity.java | 4 ++ .../entity/powerup/GluePowerUp.java | 6 +- .../entity/powerup/LongerPaddlePowerUp.java | 6 +- .../entity/powerup/MultiBallPowerUp.java | 5 +- .../brickbuster/entity/powerup/PowerUp.java | 5 +- .../entity/powerup/PowerUpType.java | 68 +++++++++++++++++++ .../entity/powerup/ShieldPowerUp.java | 6 +- .../com/me/brickbuster/state/PlayState.java | 36 +--------- .../brickbuster/desktop/DesktopLauncher.java | 2 + 10 files changed, 93 insertions(+), 57 deletions(-) create mode 100644 core/src/com/me/brickbuster/entity/powerup/PowerUpType.java diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index d48ffb0..8092e84 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -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.entity.powerup.PowerUp; +import com.me.brickbuster.entity.powerup.PowerUpType; import com.me.brickbuster.state.PlayState; 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_HEIGHT = 100; - private Class powerUpType; + private PowerUpType powerUpType; private Vector2[] vertices; - public Brick(PlayState state, Class powerUpType, int x, int y) { + public Brick(PlayState state, PowerUpType powerUpType, int x, int y) { super(state, x, y); this.powerUpType = powerUpType; @@ -49,12 +50,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; } diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 2332e19..cb29e92 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -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); diff --git a/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java b/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java index 1ed22b2..a6712c3 100644 --- a/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/GluePowerUp.java @@ -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 diff --git a/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java b/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java index fbe53bf..cd344a5 100644 --- a/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/LongerPaddlePowerUp.java @@ -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 diff --git a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java index 85891ae..16d894c 100644 --- a/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/MultiBallPowerUp.java @@ -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(); } diff --git a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java index baa0d17..ac1aaba 100644 --- a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java @@ -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; } diff --git a/core/src/com/me/brickbuster/entity/powerup/PowerUpType.java b/core/src/com/me/brickbuster/entity/powerup/PowerUpType.java new file mode 100644 index 0000000..284ff40 --- /dev/null +++ b/core/src/com/me/brickbuster/entity/powerup/PowerUpType.java @@ -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 cls; + private Color color; + private int weight; + + PowerUpType(Class 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; + } + +} diff --git a/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java index 6a8cade..c618ba7 100644 --- a/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java @@ -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 diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 787ef93..ba58657 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -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, Integer> powerUpWeights; - private static final int weightSum; - public List powerUps; public Paddle paddle; public List balls; public List 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 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 getWeightedPowerUp() { - int remaining = MathUtils.random(weightSum); - for (Map.Entry, Integer> entry : PlayState.powerUpWeights.entrySet()) { - remaining -= entry.getValue(); - if (remaining < 0) { - return entry.getKey(); - } - } - return null; - } - - 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); - tmp.put(ShieldPowerUp.class, 40); - powerUpWeights = Collections.unmodifiableMap(tmp); - - int sum = 0; - for (int x : PlayState.powerUpWeights.values()) { - sum += x; - } - weightSum = sum; - } - } diff --git a/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java b/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java index 767e6ae..97849d3 100644 --- a/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java +++ b/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java @@ -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); }