diff --git a/src/de/bricked/game/powerups/ExtraLifePowerUp.java b/src/de/bricked/game/powerups/ExtraLifePowerUp.java index 37b7d5edcdac1e2cadd6b025bae1858a37c0c0a8..38e76ab303890f42ec1ba3af435eb5d6c748a8c6 100644 --- a/src/de/bricked/game/powerups/ExtraLifePowerUp.java +++ b/src/de/bricked/game/powerups/ExtraLifePowerUp.java @@ -7,13 +7,17 @@ public class ExtraLifePowerUp extends PowerUp { public ExtraLifePowerUp() { - super(PowerUpType.valueOf("EXTRA_LIFE").getId(), 10); + super(PowerUpType.EXTRA_LIFE.getId(), PowerUpType.EXTRA_LIFE.getDurationInSeconds()); } @Override public void activate(LevelController levelController, Game game) - { - //TODO + { + if(game.getLivesRemaining() - 1 < levelController.MAX_LIVES) + { + game.setLivesRemaining(game.getLivesRemaining() + 1); + levelController.refreshLiveCounter(); + } } @Override diff --git a/src/de/bricked/game/powerups/PowerUpType.java b/src/de/bricked/game/powerups/PowerUpType.java index 6bc52e10ebd8a9ff010692b8ac2172e5796939f9..bddabe0a35e208df6982c664803583ec22a6677d 100644 --- a/src/de/bricked/game/powerups/PowerUpType.java +++ b/src/de/bricked/game/powerups/PowerUpType.java @@ -3,21 +3,28 @@ package de.bricked.game.powerups; public enum PowerUpType { - EXTRA_LIFE(1), - FASTER_BALL(2), - SLOWER_BALL(3), - EXPLODE_BALL(4), - NO_COLLISION_BALL(5); + EXTRA_LIFE(1, -1), + FASTER_BALL(2, 10), + SLOWER_BALL(3, 10), + EXPLODE_BALL(4, 10), + NO_COLLISION_BALL(5, 10); private int id; + private int durationInSeconds; - PowerUpType(int id) + PowerUpType(int id, int durationInSeconds) { this.id = id; + this.durationInSeconds = durationInSeconds; } public int getId() { return id; } -} + + public int getDurationInSeconds() + { + return durationInSeconds; + } +} \ 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 a7eb86e7e7de233799df0593c942bbdf7269bff0..e4a7a8a911d2411a89f80639721529b14b4e5b96 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.valueOf("EXPLODE_BALL").getId(), -1); + super(PowerUpType.EXPLODE_BALL.getId(), PowerUpType.EXPLODE_BALL.getDurationInSeconds()); } @Override @@ -23,5 +23,6 @@ public class ExplodeBallPowerUp extends PowerUp @Override public void deactivate(LevelController levelController, Game game) { + levelController.changeBall(new Ball(BallType.NORMAL)); } } \ No newline at end of file diff --git a/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java b/src/de/bricked/game/powerups/ball/NoCollisionBallPowerUp.java index 099006d8e6703b87ea37b7dd7b45092d1700e4a4..214dffadc5c2390cc63e32c62a322035fdbea292 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.valueOf("NO_COLLISION_BALL").getId(), -1); + super(PowerUpType.NO_COLLISION_BALL.getId(), PowerUpType.NO_COLLISION_BALL.getDurationInSeconds()); } @Override @@ -22,6 +22,7 @@ public class NoCollisionBallPowerUp extends PowerUp @Override public void deactivate(LevelController levelController, Game game) - { + { + levelController.changeBall(new Ball(BallType.NORMAL)); } } \ No newline at end of file diff --git a/src/de/bricked/ui/LevelController.java b/src/de/bricked/ui/LevelController.java index 6aa4b4890c3c2046a9837086883f3b181fdc4ae6..90055ef6a1f2c9a02029b75e30ed8df89c81bcd6 100644 --- a/src/de/bricked/ui/LevelController.java +++ b/src/de/bricked/ui/LevelController.java @@ -81,7 +81,7 @@ public class LevelController private AnimationTimer timer; private double gamePaneWidth; private double gamePaneHeight; - private final int MAX_LIVES = 7; + public final int MAX_LIVES = 7; private final int TARGET_FPS = 60; private final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; private final static double BRICK_FADE_DURATION = 300.0; @@ -134,7 +134,10 @@ public class LevelController @Override public void handle(MouseEvent event) { - startGame(); + if(gameState.equals(GameState.WAITING)) + { + startGame(); + } event.consume(); anchorPaneGame.requestFocus(); } @@ -498,9 +501,7 @@ public class LevelController } // move powerups - movePowerUps(); - // check timed powerups - checkPowerUps(); + movePowerUps(); } }; } @@ -584,7 +585,7 @@ public class LevelController } } - private void refreshLiveCounter() + public void refreshLiveCounter() { vboxLives.getChildren().clear(); @@ -788,8 +789,7 @@ public class LevelController HitLocation hitLocation = game.collides(labelPosition, paddlePosition, paddle.getWidth(), paddle.getHeight(), true); if(hitLocation != null && (hitLocation.equals(HitLocation.PADDLE) || hitLocation.equals(HitLocation.CORNER))) - { - // TODO activate method + { Logger.log(LogLevel.DEBUG, "Collected PowerUp with ID = " + currentPowerUp.getID()); if(!currentPowerUp.isPermanent()) { @@ -809,13 +809,12 @@ public class LevelController } } } - - // TODO if there is already one item of this type? + private void addTimedPowerUp(PowerUp powerUp) { Label labelPowerUp = new Label(String.valueOf(powerUp.getDurationInSeconds())); labelPowerUp.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;" + "-fx-font-size: 16;" + "-fx-font-weight: bold;" - + "-fx-tect-fill: #cc0000;"); + + "-fx-text-fill: #000000;"); labelPowerUp.setAlignment(Pos.CENTER); labelPowerUp.setUserData(powerUp); @@ -826,20 +825,7 @@ public class LevelController timedPowerUps.add(labelPowerUp); - new CountdownTimer(powerUp.getDurationInSeconds(), labelPowerUp); - } - - private void checkPowerUps() - { - for(Iterator<Label> iterator = timedPowerUps.iterator(); iterator.hasNext();) - { - Label currentLabel = iterator.next(); - if(currentLabel.getText().equals("0")) - { - vboxPowerUps.getChildren().remove(currentLabel); - iterator.remove(); - } - } + new CountdownTimer(powerUp.getDurationInSeconds(), labelPowerUp, this); } private void resetPowerUps() @@ -847,19 +833,28 @@ public class LevelController movingPowerUps = new ArrayList<>(); timedPowerUps = new ArrayList<>(); vboxPowerUps.getChildren().clear(); - } + } - public void changeBall(Ball newBall) + public void deactivatePowerUp(Label label) { - double translateX = stackPaneBall.getTranslateX(); - double translateY = stackPaneBall.getTranslateY(); - Point2D direction = game.getBall().getDirection(); - game.setBall(newBall); - - initBall(game.getBall().getType()); - stackPaneBall.setTranslateX(translateX); - stackPaneBall.setTranslateY(translateY); - game.getBall().setDirection(game.getNewSpeedDirection(direction, newBall.getType().getSpeedFactor())); + int counter = 0; + PowerUp powerUp = (PowerUp)label.getUserData(); + for(Label currentLabel : timedPowerUps) + { + PowerUp currentPowerUp = (PowerUp)currentLabel.getUserData(); + if(currentPowerUp.getID() == powerUp.getID()) + { + counter++; + } + } + + //if no other power ups of same kind are active + if(counter <= 1) + { + vboxPowerUps.getChildren().remove(label); + timedPowerUps.remove(label); + powerUp.deactivate(this, game); + } } public void showLabelFPS(boolean value) @@ -898,4 +893,20 @@ public class LevelController anchorPaneGame.requestFocus(); } + + /* + * PowerUP-Functions + */ + public void changeBall(Ball newBall) + { + double translateX = stackPaneBall.getTranslateX(); + double translateY = stackPaneBall.getTranslateY(); + Point2D direction = game.getBall().getDirection(); + game.setBall(newBall); + + initBall(game.getBall().getType()); + stackPaneBall.setTranslateX(translateX); + stackPaneBall.setTranslateY(translateY); + game.getBall().setDirection(game.getNewSpeedDirection(direction, newBall.getType().getSpeedFactor())); + } } \ No newline at end of file diff --git a/src/de/bricked/utils/CountdownTimer.java b/src/de/bricked/utils/CountdownTimer.java index e6e30c9773cb33cf2ca2fc479e088dd4245c10ba..f5a807828a9f2d5f02a8c8ffb54c8987b252bcee 100644 --- a/src/de/bricked/utils/CountdownTimer.java +++ b/src/de/bricked/utils/CountdownTimer.java @@ -3,6 +3,7 @@ package de.bricked.utils; import java.util.Timer; import java.util.TimerTask; +import de.bricked.ui.LevelController; import javafx.application.Platform; import javafx.scene.control.Label; @@ -10,7 +11,7 @@ public class CountdownTimer { private int count; - public CountdownTimer(int seconds, Label label) + public CountdownTimer(int seconds, Label label, LevelController levelController) { this.count = seconds; @@ -31,10 +32,17 @@ public class CountdownTimer } }); if(count > 0) + { count--; + } if(count == 0) + { + Platform.runLater(()->{ + levelController.deactivatePowerUp(label); + }); timer.cancel(); + } } }; timer.schedule(task, 0, 1000);