summaryrefslogtreecommitdiff
path: root/pacman/controller/src/main
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-05-14 18:30:01 +0200
committerGitHub <noreply@github.com>2025-05-14 18:30:01 +0200
commit7a1f9b5fbe334836732a79a3ca1205293b220dad (patch)
treea426a845f8bf2242c51e278aa6e614556efc77f2 /pacman/controller/src/main
parentbf766db31174316cac83856ee86f76efd55178c9 (diff)
downloadpacman-7a1f9b5fbe334836732a79a3ca1205293b220dad.tar.gz
pacman-7a1f9b5fbe334836732a79a3ca1205293b220dad.zip
docs/model-module (#13)
* docs(model/entities): Documentation for entities and items Adds documentation to the following entity and item classes - Entity - EntityUtils - Items Did not add documentation for Ghost entity and Pacman entity, since the class is not done yet. * fix/small-fixes (#14) * fix(model/entities): entites start in center of tile * style(pacman): It should be width and height, not height and width. * refactor(view): Changing setting of background color
Diffstat (limited to 'pacman/controller/src/main')
-rw-r--r--pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java63
1 files changed, 63 insertions, 0 deletions
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..a334576
--- /dev/null
+++ b/pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java
@@ -0,0 +1,63 @@
+package com.gr15.pacman.controller;
+
+import com.gr15.pacman.model.GameState;
+import com.gr15.pacman.model.entities.Entity.Direction;
+import com.gr15.pacman.view.GameView;
+
+import javafx.animation.AnimationTimer;
+
+/**
+ * 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;
+
+ 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();
+ }
+}