diff --git a/bin/de/bricked/ui/LevelGUI.fxml b/bin/de/bricked/ui/LevelGUI.fxml index 716598d820007443d25ad648e97223005676b1ab..dabc3a8a3ca5e10c4660fe58c2d4cdb16f18abda 100644 --- a/bin/de/bricked/ui/LevelGUI.fxml +++ b/bin/de/bricked/ui/LevelGUI.fxml @@ -12,7 +12,7 @@ <children> <AnchorPane fx:id="anchorPaneGame" layoutX="22.0" layoutY="125.0" minHeight="650.0" minWidth="700.0" prefHeight="650.0" prefWidth="700.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="78.0" AnchorPane.topAnchor="125.0"> <children> - <Label fx:id="labelMultiplicator" alignment="CENTER" contentDisplay="CENTER" layoutX="301.0" layoutY="502.0" prefHeight="45.0" prefWidth="99.0" textAlignment="CENTER"> + <Label fx:id="labelMultiplicator" alignment="CENTER" contentDisplay="CENTER" layoutX="301.0" layoutY="502.0" prefHeight="45.0" prefWidth="100.0" textAlignment="CENTER"> <font> <Font name="System Bold" size="30.0" /> </font> diff --git a/src/de/bricked/game/GameState.java b/src/de/bricked/game/GameState.java index 7609145d80c8353326c7e8b22a9bc58cfa7e8728..8641d6f7fe390fd66e42955542d8eeb639a60240 100644 --- a/src/de/bricked/game/GameState.java +++ b/src/de/bricked/game/GameState.java @@ -2,5 +2,5 @@ package de.bricked.game; public enum GameState { - WAITING, RUNNING, STOPPED + WAITING, RUNNING, PAUSED, STOPPED } \ No newline at end of file diff --git a/src/de/bricked/ui/LevelController.java b/src/de/bricked/ui/LevelController.java index 73c9ada7210ba085ed73c742d7d306a22904a14f..bc5bc940062fb0fa38f61f15f136c90426ff35d6 100644 --- a/src/de/bricked/ui/LevelController.java +++ b/src/de/bricked/ui/LevelController.java @@ -101,6 +101,8 @@ public class LevelController private static ArrayList<Label> brickLabels; private ArrayList<Label> movingPowerUps; private ArrayList<Label> timedPowerUps; + private long previousTime = 0; + private float secondsElapsedSinceLastFpsUpdate = 0f; private void startGame() { @@ -140,7 +142,7 @@ public class LevelController game.setBoard(new Board(game)); game.setLevelController(this); - anchorPane.setOnMouseClicked(new EventHandler<MouseEvent>() + anchorPaneGame.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) @@ -184,6 +186,26 @@ public class LevelController back(); event.consume(); } + + //pause + if(event.getCode().toString().equals("P")) + { + if(gameState.equals(GameState.RUNNING)) + { + gameState = GameState.PAUSED; + pause(); + event.consume(); + return; + } + + if(gameState.equals(GameState.PAUSED)) + { + gameState = GameState.RUNNING; + restart(); + event.consume(); + return; + } + } } }); @@ -354,10 +376,7 @@ public class LevelController private void initTimer() { timer = new AnimationTimer() - { - private long previousTime = 0; - private float secondsElapsedSinceLastFpsUpdate = 0f; - + { @Override public void handle(long currentTime) { @@ -401,11 +420,11 @@ public class LevelController game.setLivesRemaining(game.getLivesRemaining() - 1); Logger.log(LogLevel.DEBUG, "Life lost (" + game.getLivesRemaining() + " lives remaining)"); refreshLiveCounter(); + resetPowerUps(); if(game.getLivesRemaining() <= 0) { // game over - - resetPowerUps(); + gameState = GameState.STOPPED; timer.stop(); @@ -516,6 +535,34 @@ public class LevelController } }; } + + private void pause() + { + timer.stop(); + anchorPaneGame.setOpacity(0.5); + + Text t = new Text("PAUSED"); + t.setFont(new Font(40)); + new Scene(new Group(t)); + t.applyCss(); + + Label labelPause = new Label("PAUSED"); + labelPause.setStyle("-fx-font-weight: bold; -fx-font-size: 40;"); + labelPause.setTranslateX(((gamePaneWidth - t.getLayoutBounds().getWidth() + 10) / 2) + 22); + labelPause.setTranslateY(((gamePaneHeight - t.getLayoutBounds().getHeight()) / 2) + 125); + + anchorPane.getChildren().add(labelPause); + } + + private void restart() + { + previousTime = 0; + secondsElapsedSinceLastFpsUpdate = 0f; + fps = 0; + anchorPaneGame.setOpacity(1.0); + anchorPane.getChildren().remove(anchorPane.getChildren().size() - 1); + timer.start(); + } public void correctBallPosition(HitLocation hitLocation, Point2D ballPosition, Point2D brickPosition, double brickWidth, double brickHeight) { @@ -755,9 +802,7 @@ public class LevelController labelNotification.setTranslateX(xPosition); labelNotification.setTranslateY(yPosition); labelNotification.setStyle("-fx-font-weight: bold; -fx-font-size: " + fontSize + "; "); - labelNotification.setAlignment(Pos.CENTER); - - + labelNotification.setAlignment(Pos.CENTER); labelNotification.setPrefWidth(t.getLayoutBounds().getWidth() + 10); labelNotification.setPrefHeight(gamePaneHeight / Board.HEIGHT); @@ -840,6 +885,17 @@ public class LevelController } } } + + private void clearMovingPowerUps() + { + if(movingPowerUps != null) + { + for(Label currentLabel : movingPowerUps) + { + anchorPaneGame.getChildren().remove(currentLabel); + } + } + } private void addTimedPowerUp(PowerUp powerUp) { @@ -861,6 +917,7 @@ public class LevelController private void resetPowerUps() { + clearMovingPowerUps(); movingPowerUps = new ArrayList<>(); timedPowerUps = new ArrayList<>(); vboxPowerUps.getChildren().clear();