summaryrefslogtreecommitdiff
path: root/pacman/controller
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pacman/controller/pom.xml2
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java45
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java65
-rw-r--r--pacman/controller/src/main/java/module-info.java10
-rw-r--r--pacman/controller/src/main/java/pacman/controller/GameApp.java21
5 files changed, 115 insertions, 28 deletions
diff --git a/pacman/controller/pom.xml b/pacman/controller/pom.xml
index e3a99f8..6baf25f 100644
--- a/pacman/controller/pom.xml
+++ b/pacman/controller/pom.xml
@@ -34,7 +34,7 @@
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
- <mainClass>pacman.controller.GameApp</mainClass>
+ <mainClass>com.gr15.pacman.controller.GameApp</mainClass>
</configuration>
</plugin>
</plugins>
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
new file mode 100644
index 0000000..1c8bc75
--- /dev/null
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameApp.java
@@ -0,0 +1,45 @@
+package com.gr15.pacman.controller;
+
+import com.gr15.pacman.model.GameState;
+import com.gr15.pacman.model.JsonParser;
+import com.gr15.pacman.view.GameView;
+
+import javafx.application.Application;
+import javafx.stage.Stage;
+
+/**
+ * GameApp
+ */
+public class GameApp
+ extends Application {
+
+ GameController gameController;
+ GameView gameView;
+ GameState gameState;
+
+ @Override
+ public void start(Stage primaryStage) throws Exception {
+ primaryStage.setTitle("Pac-Man");
+ primaryStage.setResizable(false);
+ primaryStage.setFullScreen(true);
+
+ try {
+ gameState = JsonParser.getGameState("test");
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException("Failed to load game state: " + e.getMessage());
+ }
+
+ gameView = new GameView(gameState, 8, 5);
+ primaryStage.setScene(gameView);
+
+ gameController = new GameController(gameState, gameView);
+ gameController.startGameLoop();
+
+ primaryStage.show();
+ }
+
+ public static void main(String[] args) {
+ launch(args);
+ }
+}
diff --git a/pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java
new file mode 100644
index 0000000..b1208be
--- /dev/null
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java
@@ -0,0 +1,65 @@
+package com.gr15.pacman.controller;
+
+import com.gr15.pacman.model.GameState;
+import com.gr15.pacman.model.Board.Direction;
+import com.gr15.pacman.view.GameView;
+
+import javafx.animation.AnimationTimer;
+import javafx.scene.paint.Color;
+
+/**
+ * GameController
+ */
+public class GameController {
+
+ private GameState gameState;
+ private GameView gameView;
+
+ private AnimationTimer gameLoop;
+ private long lastUpdate = 0;
+
+ public GameController(GameState gameState, GameView gameView) {
+ this.gameState = gameState;
+ this.gameView = gameView;
+ gameView.setFill(Color.BLACK);
+
+ setupEventHandlers();
+ gameLoop = new AnimationTimer() {
+
+ @Override
+ public void handle(long now) {
+ if (lastUpdate == 0) {
+ lastUpdate = now;
+ return; /* returning early, since no time have elapsed */
+ }
+
+ double deltaSeconds = (now - lastUpdate) / 1_000_000_000.0;
+ lastUpdate = now;
+
+ gameState.update(deltaSeconds);
+ gameView.renderGame(deltaSeconds);
+ }
+ };
+ }
+
+ private void setupEventHandlers() {
+ gameView.setOnKeyPressed(event -> {
+ switch (event.getCode()) {
+ case UP -> gameState.getPacman().setDirection(Direction.UP);
+ case DOWN -> gameState.getPacman().setDirection(Direction.DOWN);
+ case LEFT -> gameState.getPacman().setDirection(Direction.LEFT);
+ case RIGHT -> gameState.getPacman().setDirection(Direction.RIGHT);
+ default -> {}
+ }
+ });
+ }
+
+ public void startGameLoop() {
+ gameLoop.start();
+ }
+
+ public void stopGameLoop() {
+ lastUpdate = 0;
+ gameLoop.stop();
+ }
+}
diff --git a/pacman/controller/src/main/java/module-info.java b/pacman/controller/src/main/java/module-info.java
index 75c422d..8b4e271 100644
--- a/pacman/controller/src/main/java/module-info.java
+++ b/pacman/controller/src/main/java/module-info.java
@@ -1,9 +1,7 @@
/* module-info.java
* This acts as the manifest for the module.
*/
-module pacman.controller {
- requires javafx.controls; /* Declares a dependency on another module */
- requires pacman.model; /* Should depenend on model, */
- requires pacman.view; /* and view */
- exports pacman.controller; /* exports make specific packages */
-} /* public to other modules */
+module com.gr15.pacman.controller {
+ requires transitive com.gr15.pacman.view;
+ exports com.gr15.pacman.controller;
+}
diff --git a/pacman/controller/src/main/java/pacman/controller/GameApp.java b/pacman/controller/src/main/java/pacman/controller/GameApp.java
deleted file mode 100644
index 648dc15..0000000
--- a/pacman/controller/src/main/java/pacman/controller/GameApp.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package pacman.controller;
-
-import javafx.application.Application;
-import javafx.stage.Stage;
-
-/**
- * GameApp
- */
-public class GameApp
- extends Application {
-
- @Override
- public void start(Stage arg0) throws Exception {
- // TODO Auto-generated method stub
-
- }
-
- public static void main(String[] args) {
- launch(args);
- }
-}