From 7a1f9b5fbe334836732a79a3ca1205293b220dad Mon Sep 17 00:00:00 2001 From: mithe24 Date: Wed, 14 May 2025 18:30:01 +0200 Subject: 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 --- .../com/gr15/pacman/controller/GameController.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pacman/controller/src/main/java/com/gr15/pacman/controller/GameController.java (limited to 'pacman/controller/src/main/java/com/gr15') 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(); + } +} -- cgit v1.2.3-70-g09d2