diff --git a/src/de/bricked/game/powerups/ExtraLifePowerUp.java b/src/de/bricked/game/powerups/ExtraLifePowerUp.java index 38e76ab303890f42ec1ba3af435eb5d6c748a8c6..55de70cec527e26d8ee25280491af294d8f85bf7 100644 --- a/src/de/bricked/game/powerups/ExtraLifePowerUp.java +++ b/src/de/bricked/game/powerups/ExtraLifePowerUp.java @@ -7,7 +7,7 @@ public class ExtraLifePowerUp extends PowerUp { public ExtraLifePowerUp() { - super(PowerUpType.EXTRA_LIFE.getId(), PowerUpType.EXTRA_LIFE.getDurationInSeconds()); + super(PowerUpType.EXTRA_LIFE.getID(), PowerUpType.EXTRA_LIFE.getDurationInSeconds()); } @Override diff --git a/src/de/bricked/game/powerups/PowerUpType.java b/src/de/bricked/game/powerups/PowerUpType.java index 1dbe7c97db39faf38e0ca802307b67164d2df90a..e360e30acd7be843dca36e0b60e160ba7387193c 100644 --- a/src/de/bricked/game/powerups/PowerUpType.java +++ b/src/de/bricked/game/powerups/PowerUpType.java @@ -1,39 +1,51 @@ package de.bricked.game.powerups; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import de.bricked.game.powerups.ball.ExplodeBallPowerUp; import de.bricked.game.powerups.ball.NoCollisionBallPowerUp; + public enum PowerUpType { - NONE(0, -1), - EXTRA_LIFE(1, -1), - FASTER_BALL(2, 10), - SLOWER_BALL(3, 10), - EXPLODE_BALL(4, 10), - NO_COLLISION_BALL(5, 10); - - private int id; + NONE(0, -1, null), + EXTRA_LIFE(1, -1, null), + FASTER_BALL(2, 10, Arrays.asList(3)), + SLOWER_BALL(3, 10, Arrays.asList(2, 4, 5)), + EXPLODE_BALL(4, 10, Arrays.asList(2, 3, 5)), + NO_COLLISION_BALL(5, 10, Arrays.asList(2, 3, 4)); + + private int ID; private int durationInSeconds; + private List<Integer> deactivatesPowerUpIDs; public static PowerUpType[] types = PowerUpType.values(); - PowerUpType(int id, int durationInSeconds) + PowerUpType(int ID, int durationInSeconds, List<Integer> deactivatesPowerUpdIDs) { - this.id = id; + this.ID = ID; this.durationInSeconds = durationInSeconds; + this.deactivatesPowerUpIDs = deactivatesPowerUpdIDs; } - public int getId() + public int getID() { - return id; + return ID; } public int getDurationInSeconds() { return durationInSeconds; - } + } + + public ArrayList<Integer> getDeactivatesPowerUpIDs() + { + return new ArrayList<>(deactivatesPowerUpIDs); + } - public static PowerUp getInstance(PowerUpType powerUpType) + public static PowerUp getInstance(PowerUpType powerUpType) { switch (powerUpType) { @@ -45,4 +57,16 @@ public enum PowerUpType default: return null; } } + + public static PowerUpType valueOf(int ID) + { + for(PowerUpType currentType : PowerUpType.values()) + { + if(ID == currentType.getID()) + { + return currentType; + } + } + return PowerUpType.NONE; + } } \ No newline at end of file diff --git a/src/de/bricked/game/powerups/ball/ExplodeBallPowerUp.java b/src/de/bricked/game/powerups/ball/ExplodeBallPowerUp.java index e4a7a8a911d2411a89f80639721529b14b4e5b96..bb3ca927a7bfdc681e21a94b0758e986bed02f11 100644 --- a/src/de/bricked/game/powerups/ball/ExplodeBallPowerUp.java +++ b/src/de/bricked/game/powerups/ball/ExplodeBallPowerUp.java @@ -11,7 +11,7 @@ public class ExplodeBallPowerUp extends PowerUp { public ExplodeBallPowerUp() { - super(PowerUpType.EXPLODE_BALL.getId(), PowerUpType.EXPLODE_BALL.getDurationInSeconds()); + super(PowerUpType.EXPLODE_BALL.getID(), PowerUpType.EXPLODE_BALL.getDurationInSeconds()); } @Override diff --git a/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java b/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java index 214dffadc5c2390cc63e32c62a322035fdbea292..c9c4f0e54161c377b4e3e2c7882a4ff9432d03f0 100644 --- a/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java +++ b/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java @@ -11,7 +11,7 @@ public class NoCollisionBallPowerUp extends PowerUp { public NoCollisionBallPowerUp() { - super(PowerUpType.NO_COLLISION_BALL.getId(), PowerUpType.NO_COLLISION_BALL.getDurationInSeconds()); + super(PowerUpType.NO_COLLISION_BALL.getID(), PowerUpType.NO_COLLISION_BALL.getDurationInSeconds()); } @Override diff --git a/src/de/bricked/ui/LevelController.java b/src/de/bricked/ui/LevelController.java index 9c95c138857e815da8b2dbe1c152aa1376c93a5d..f08f68ab934a6b7e5104367243a18b788ee08bda 100644 --- a/src/de/bricked/ui/LevelController.java +++ b/src/de/bricked/ui/LevelController.java @@ -18,6 +18,7 @@ import de.bricked.game.bricks.BrickType; import de.bricked.game.paddle.Paddle; import de.bricked.game.paddle.PaddleSize; import de.bricked.game.powerups.PowerUp; +import de.bricked.game.powerups.PowerUpType; import de.bricked.utils.CountdownTimer; import fontAwesome.FontIcon; import fontAwesome.FontIconType; @@ -919,6 +920,8 @@ public class LevelController if(!alreadyActivated) { + deactivateAllContraryPowerUps(powerUp); + HBox hbox = new HBox(); Label labelIcon = new Label(); labelIcon.setStyle("-fx-background-image: url(\"de/bricked/resources/textures/powerups/" + powerUp.getID() + ".png\");" + "-fx-background-position: center center;" + "-fx-background-repeat: no-repeat;" + "-fx-background-size: contain;"); @@ -939,7 +942,7 @@ public class LevelController timedPowerUps.add(new CountdownTimer(powerUp.getDurationInSeconds(), hbox, this)); } - } + } private void resetPowerUps() { @@ -948,6 +951,27 @@ public class LevelController timedPowerUps = new ArrayList<>(); vboxPowerUps.getChildren().clear(); } + + public void deactivateAllContraryPowerUps(PowerUp powerUp) + { + ArrayList<Integer> deactiveIDs = PowerUpType.valueOf(powerUp.getID()).getDeactivatesPowerUpIDs(); + if(deactiveIDs != null) + { + for(int currentInt : deactiveIDs) + { + for(CountdownTimer currentTimer : timedPowerUps) + { + PowerUp currentPowerUp = (PowerUp)currentTimer.getHBox().getUserData(); + if(currentPowerUp.getID() == currentInt) + { + currentTimer.stop(); + deactivatePowerUp(currentTimer, currentTimer.getHBox()); + break; + } + } + } + } + } public void deactivatePowerUp(CountdownTimer timer, HBox hbox) { diff --git a/src/de/bricked/utils/CountdownTimer.java b/src/de/bricked/utils/CountdownTimer.java index 83d2763c241175c05c3f11bdfce4f68e5bea6dde..84cb3ace09343674e10c5a97874acbadbc11371b 100644 --- a/src/de/bricked/utils/CountdownTimer.java +++ b/src/de/bricked/utils/CountdownTimer.java @@ -12,6 +12,7 @@ public class CountdownTimer { private int count; private HBox hbox; + private Timer timer; public CountdownTimer(int seconds, HBox hbox, LevelController levelController) { @@ -20,7 +21,7 @@ public class CountdownTimer CountdownTimer self = this; - Timer timer = new Timer(); + timer = new Timer(); TimerTask task = new TimerTask() { @Override @@ -63,4 +64,10 @@ public class CountdownTimer { return hbox; } + + public void stop() + { + timer.cancel(); + timer.purge(); + } } \ No newline at end of file