summaryrefslogtreecommitdiff
path: root/pacman/controller/src/main/java/com
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-05-23 19:24:52 +0200
committerGitHub <noreply@github.com>2025-05-23 19:24:52 +0200
commit6a01dc315b38a047d202b3924be5a84321db8226 (patch)
tree68536b8d2829facd45a62eda1f099018b6c35781 /pacman/controller/src/main/java/com
parent8c8a4023c98e076ae6c635b36734dbe28782ed69 (diff)
downloadpacman-6a01dc315b38a047d202b3924be5a84321db8226.tar.gz
pacman-6a01dc315b38a047d202b3924be5a84321db8226.zip
Last commits. I won't be contributing anymore. (#27)
* im done * feat(GameConfig): Adds immutability for GameConfig record * accidentally removed main menu button * throwing exceptions * Please stop using protected
Diffstat (limited to 'pacman/controller/src/main/java/com')
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java9
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameController.java35
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameOverController.java33
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/screen/MainMenuController.java56
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/screen/PauseController.java18
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/screen/YouWonController.java34
6 files changed, 129 insertions, 56 deletions
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java
index 9c07798..2c76d88 100644
--- a/pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java
@@ -42,15 +42,14 @@ public class GameApp
public void start(Stage primaryStage) throws Exception {
/* window properties */
primaryStage.setTitle("Pac-Man");
- primaryStage.setResizable(false);
- primaryStage.setFullScreen(true);
/* Adding main menu, and instantiate controller */
- viewManager.addView(ViewKeys.MAIN_MENU, mainMenuView);
- viewManager.showView(ViewKeys.MAIN_MENU);
+ viewManager.addView(ViewKeys.MAIN_MENU_VIEW, mainMenuView);
+ viewManager.showView(ViewKeys.MAIN_MENU_VIEW);
new MainMenuController(viewManager, mainMenuView);
- Scene scene = new Scene(viewManager.getRoot(), 1920, 1080);
+ Scene scene = new Scene(viewManager.getRoot(), 1000, 1000);
+
primaryStage.setScene(scene);
primaryStage.show();
}
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameController.java
index 585247f..82c8cda 100644
--- a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameController.java
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameController.java
@@ -3,7 +3,9 @@ package com.gr15.pacman.controller.screen;
import com.gr15.pacman.model.GameState;
import com.gr15.pacman.model.entities.Entity.Direction;
import com.gr15.pacman.view.ViewManager;
+import com.gr15.pacman.view.screen.GameOverView;
import com.gr15.pacman.view.screen.GameView;
+import com.gr15.pacman.view.screen.YouWonView;
import com.gr15.pacman.view.ViewManager.ViewKeys;
import javafx.animation.AnimationTimer;
@@ -41,14 +43,27 @@ public class GameController {
* @param gameState the state of the game
* @param gameView the view that renders the game
* @param viewManager the manager responsible for switching views
+ * @throws IllegalArgumentException if GameState, gameView or viewManager is {@code null}
*/
public GameController(GameState gameState, GameView gameView,
ViewManager viewManager) {
-
+ if (gameState == null) {
+ throw new IllegalArgumentException("gameState must not be null");
+ }
+ if (gameView == null) {
+ throw new IllegalArgumentException("gameView must not be null");
+ }
+ if (viewManager == null) {
+ throw new IllegalArgumentException("viewManager must not be null");
+ }
this.viewManager = viewManager;
this.gameState = gameState;
this.gameView = gameView;
+ /* Removing potential unrelated views */
+ viewManager.removeView(ViewKeys.GAME_OVER_VIEW);
+ viewManager.removeView(ViewKeys.YOU_WON_VIEW);
+
gameView.setOnKeyPressed(this::handleKeyEvent);
gameLoop = new AnimationTimer() {
@@ -59,7 +74,21 @@ public class GameController {
lastUpdate = now;
return; /* returning early, since no time have elapsed */
}
-
+
+ if (gameState.isWon()) {
+ YouWonView youWonView = new YouWonView(gameState.getScore());
+ viewManager.addView(ViewKeys.YOU_WON_VIEW, youWonView);
+ viewManager.showView(ViewKeys.YOU_WON_VIEW);
+ new YouWonController(youWonView, viewManager);
+ stopGameLoop();
+ } else if(gameState.GameOver()) {
+ GameOverView gameOverView = new GameOverView(gameState.getScore());
+ viewManager.addView(ViewKeys.GAME_OVER_VIEW, gameOverView);
+ viewManager.showView(ViewKeys.GAME_OVER_VIEW);
+ new GameOverController(gameOverView, viewManager);
+ stopGameLoop();
+ }
+
double deltaSeconds = (now - lastUpdate) / 1_000_000_000.0;
lastUpdate = now;
@@ -84,7 +113,7 @@ public class GameController {
case PAGE_UP -> gameView.changeZoom(0.1);
case PAGE_DOWN -> gameView.changeZoom(-0.1);
case ESCAPE -> {
- viewManager.showView(ViewKeys.PAUSE);
+ viewManager.showView(ViewKeys.PAUSE_VIEW);
stopGameLoop();
}
default -> {}
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameOverController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameOverController.java
new file mode 100644
index 0000000..6938b2e
--- /dev/null
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/GameOverController.java
@@ -0,0 +1,33 @@
+package com.gr15.pacman.controller.screen;
+
+import javafx.event.ActionEvent;
+
+import com.gr15.pacman.view.ViewManager;
+import com.gr15.pacman.view.ViewManager.ViewKeys;
+import com.gr15.pacman.view.screen.GameOverView;
+
+/**
+ * GameOverController
+ */
+public class GameOverController {
+
+ private GameOverView gameOverView;
+ private ViewManager viewManager;
+
+ public GameOverController(GameOverView gameOverView, ViewManager viewManager) {
+ if (gameOverView == null) {
+ throw new IllegalArgumentException("gameOverView must not be null");
+ }
+ if (viewManager == null) {
+ throw new IllegalArgumentException("viewManager must not be null");
+ }
+ this.gameOverView = gameOverView;
+ this.viewManager = viewManager;
+
+ gameOverView.getMainMenuButton().setOnAction(this::mainMenu);
+ }
+
+ private void mainMenu(ActionEvent event) {
+ viewManager.showView(ViewKeys.MAIN_MENU_VIEW);
+ }
+}
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/MainMenuController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/MainMenuController.java
index 58f4d60..a0d1a59 100644
--- a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/MainMenuController.java
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/MainMenuController.java
@@ -1,7 +1,7 @@
package com.gr15.pacman.controller.screen;
+import com.gr15.pacman.model.ConfigBuilder;
import com.gr15.pacman.model.GameState;
-import com.gr15.pacman.model.GameStateBuilder;
import com.gr15.pacman.view.ViewManager;
import com.gr15.pacman.view.screen.GameView;
import com.gr15.pacman.view.screen.MainMenuView;
@@ -13,11 +13,6 @@ import com.gr15.pacman.view.ViewManager.ViewKeys;
/**
* The {@code MainMenuController} handles interactions on the main menu screen.
- *
- * <p>This includes starting a new game, resuming an existing game,
- * or exiting the application.
- * It also manages setting up new
- * game views and controllers when a new game is launched.</p>
*/
public class MainMenuController {
@@ -27,20 +22,21 @@ public class MainMenuController {
/** The main menu view UI. */
private final MainMenuView mainMenuView;
- /** The active game view instance, created when starting or resuming a game. */
- private GameView gameView;
-
- /** The active game controller instance, used to control the game loop. */
- GameController gameController;
-
/**
* Constructs a new {@code MainMenuController} with the given
* view manager and main menu view.
*
* @param viewManager the manager responsible for view navigation
* @param mainMenuView the view representing the main menu UI
+ * @throws IllegalArgumentException if viewManager or mainMenuView is {@code null}
*/
public MainMenuController(ViewManager viewManager, MainMenuView mainMenuView) {
+ if (viewManager == null) {
+ throw new IllegalArgumentException("viewManager must not be null");
+ }
+ if (mainMenuView == null) {
+ throw new IllegalArgumentException("mainMenuView must not be null");
+ }
this.viewManager = viewManager;
this.mainMenuView = mainMenuView;
initializeButtons();
@@ -50,30 +46,11 @@ public class MainMenuController {
* Initializes event handlers for the main menu buttons.
*
* <p>This includes setting actions for
- * starting a new game, resuming a game, and exiting.</p>
+ * starting a new game, and exiting.</p>
*/
private void initializeButtons() {
- mainMenuView.getResumeButton().setDisable(true);
mainMenuView.getNewGameButton().setOnAction(this::startNewGame);
mainMenuView.getExitButton().setOnAction(this::exitGame);
- mainMenuView.getResumeButton().setOnAction(this::resumeGame);
- }
-
- /**
- * Handles the event when the "Resume" button is clicked.
- *
- * <p>This resumes the game by switching back to the game view
- * and starting the game loop.</p>
- *
- * @param event the action event triggered by the button
- */
- private void resumeGame(ActionEvent event) {
- if (!viewManager.hasView(ViewKeys.GAME_VIEW) || gameController == null) {
- startNewGame(event);
- }
-
- viewManager.showView(ViewKeys.GAME_VIEW);
- gameController.startGameLoop();
}
/**
@@ -85,30 +62,27 @@ public class MainMenuController {
* @param event the action event triggered by the button
*/
private void startNewGame(ActionEvent event) {
- GameState gameState = GameStateBuilder.fromJson(
- this.getClass().getResourceAsStream("/testGameState.json"));
+ GameState gameState = new GameState(ConfigBuilder.fromJson(
+ this.getClass().getResourceAsStream("/testConfig.json")));
/* Creating new views */
- gameView = new GameView(gameState);
+ GameView gameView = new GameView(gameState);
/* Creaing new controllers */
- gameController = new GameController(gameState,
+ GameController gameController = new GameController(gameState,
gameView, viewManager);
/* Removing potentiel old views */
viewManager.removeView(ViewKeys.GAME_VIEW);
- viewManager.removeView(ViewKeys.PAUSE);
+ viewManager.removeView(ViewKeys.PAUSE_VIEW);
/* Adding new views */
viewManager.addView(ViewKeys.GAME_VIEW, gameView);
- /* Enabling resume button */
- mainMenuView.getResumeButton().setDisable(false);
-
/* Adding pause menu */
PauseView pauseView = new PauseView();
new PauseController(pauseView, gameController, viewManager);
- viewManager.addView(ViewKeys.PAUSE, pauseView);
+ viewManager.addView(ViewKeys.PAUSE_VIEW, pauseView);
/* Showing game view and starting game */
viewManager.showView(ViewKeys.GAME_VIEW);
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/PauseController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/PauseController.java
index 553455a..c9e1530 100644
--- a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/PauseController.java
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/PauseController.java
@@ -33,19 +33,23 @@ public class PauseController {
* @param pauseView the pause screen UI
* @param gameController the game controller to resume or control the game state
* @param viewManager the view manager for switching between views
+ * @throws IllegalArgumentException if pauseView, gameController or viewManager is {@code null}
*/
public PauseController(PauseView pauseView, GameController gameController,
ViewManager viewManager) {
+ if (pauseView == null) {
+ throw new IllegalArgumentException("pauseView must not be null");
+ }
+ if (gameController == null) {
+ throw new IllegalArgumentException("gameController must not be null");
+ }
+ if (viewManager == null) {
+ throw new IllegalArgumentException("viewManager must not be null");
+ }
this.gameController = gameController;
this.pauseView = pauseView;
this.viewManager = viewManager;
- setupEventHandlers();
- }
- /**
- * Initializes event handlers for pause screen buttons and keyboard input.
- */
- private void setupEventHandlers() {
pauseView.getQuitButton().setOnAction(this::exitGame);
pauseView.getMainMenuButton().setOnAction(this::quitToMainMenu);
pauseView.getResumeButton().setOnAction(this::resumeGame);
@@ -75,7 +79,7 @@ public class PauseController {
* @param event the action event triggered by the button
*/
private void quitToMainMenu(ActionEvent event) {
- viewManager.showView(ViewKeys.MAIN_MENU);
+ viewManager.showView(ViewKeys.MAIN_MENU_VIEW);
}
/**
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/YouWonController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/YouWonController.java
new file mode 100644
index 0000000..973584f
--- /dev/null
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/screen/YouWonController.java
@@ -0,0 +1,34 @@
+package com.gr15.pacman.controller.screen;
+
+import com.gr15.pacman.view.ViewManager;
+import com.gr15.pacman.view.ViewManager.ViewKeys;
+import com.gr15.pacman.view.screen.YouWonView;
+
+import javafx.event.ActionEvent;
+
+/**
+ * YouWonController
+ */
+public class YouWonController {
+
+ private YouWonView youWonView;
+ private ViewManager viewManager;
+
+ public YouWonController(YouWonView youWonView, ViewManager viewManager) {
+ if (youWonView == null) {
+ throw new IllegalArgumentException("youWonView must not be null");
+ }
+ if (viewManager == null) {
+ throw new IllegalArgumentException("viewManager must not be null");
+ }
+
+ this.youWonView = youWonView;
+ this.viewManager = viewManager;
+
+ youWonView.getMainMenuButton().setOnAction(this::mainMenu);
+ }
+
+ private void mainMenu(ActionEvent event) {
+ viewManager.showView(ViewKeys.MAIN_MENU_VIEW);
+ }
+}