From 07c75c12458e40c0aa6fcb927ed64323675941c3 Mon Sep 17 00:00:00 2001 From: fdai7012 Date: Thu, 18 Jan 2024 16:09:44 +0100 Subject: [PATCH] Added Move function to Player --- src/main/java/pacmanGame/GameManager.java | 3 ++ src/main/java/pacmanGame/Player.java | 17 ++++++++- src/test/java/pacmanTests/PlayerTest.java | 42 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/test/java/pacmanTests/PlayerTest.java diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 33679d5..40bae37 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -5,10 +5,12 @@ public class GameManager { public Map map; public Visualizer visualizer; + public Player player; public GameManager() { map = new Map(); visualizer = new VisualizerPlainText(this); + player = new Player(this); } public void Update() { @@ -17,6 +19,7 @@ public class GameManager { time++; } + public void ProcessInput(char inputChar) { if(inputChar == 27) { //todo: escape key pauses game diff --git a/src/main/java/pacmanGame/Player.java b/src/main/java/pacmanGame/Player.java index ee10c40..9e7ca7d 100644 --- a/src/main/java/pacmanGame/Player.java +++ b/src/main/java/pacmanGame/Player.java @@ -1,6 +1,21 @@ package pacmanGame; public class Player { - Vector2 position; + public Vector2 position; + public Vector2 direction; + public GameManager gameManager; + public Player(GameManager gameManager) { + this.gameManager = gameManager; + } + + public void Move() { + Vector2 newPosition = position.Add(direction); + + boolean newPosIsWall = gameManager.map.GetCell(newPosition).type.equals("wall"); + + if(!newPosIsWall) { + position = newPosition; + } + } } diff --git a/src/test/java/pacmanTests/PlayerTest.java b/src/test/java/pacmanTests/PlayerTest.java new file mode 100644 index 0000000..3a6c8f9 --- /dev/null +++ b/src/test/java/pacmanTests/PlayerTest.java @@ -0,0 +1,42 @@ +package pacmanTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import pacmanGame.*; + + +class PlayerTest { + + @Test + void Player_moving_changesPosition() { + // arrange + GameManager gameManager = new GameManager(); + Player player = gameManager.player; + player.position = new Vector2(2,2); + player.direction = new Vector2(1,0); + Vector2 expectedPosition = new Vector2(3,2); + // act + player.Move(); + Vector2 position = player.position; + // assert + assertThat(position).isEqualTo(expectedPosition); + } + + @Test + void Player_wall_obstructsMovement() { + // arrange + GameManager gameManager = new GameManager(); + Player player = gameManager.player; + player.position = new Vector2(2,2); + player.direction = new Vector2(-1,0); + Vector2 expectedPosition = new Vector2(2,2); + // act + player.Move(); + Vector2 position = player.position; + // assert + assertThat(position).isEqualTo(expectedPosition); + } +}