Browse Source

Added Move function to Player

remotes/origin/menu
fdai7012 12 months ago
committed by Julian Wenzel
parent
commit
07c75c1245
  1. 3
      src/main/java/pacmanGame/GameManager.java
  2. 17
      src/main/java/pacmanGame/Player.java
  3. 42
      src/test/java/pacmanTests/PlayerTest.java

3
src/main/java/pacmanGame/GameManager.java

@ -5,10 +5,12 @@ public class GameManager {
public Map map; public Map map;
public Visualizer visualizer; public Visualizer visualizer;
public Player player;
public GameManager() { public GameManager() {
map = new Map(); map = new Map();
visualizer = new VisualizerPlainText(this); visualizer = new VisualizerPlainText(this);
player = new Player(this);
} }
public void Update() { public void Update() {
@ -17,6 +19,7 @@ public class GameManager {
time++; time++;
} }
public void ProcessInput(char inputChar) { public void ProcessInput(char inputChar) {
if(inputChar == 27) { if(inputChar == 27) {
//todo: escape key pauses game //todo: escape key pauses game

17
src/main/java/pacmanGame/Player.java

@ -1,6 +1,21 @@
package pacmanGame; package pacmanGame;
public class Player { 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;
}
}
} }

42
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);
}
}
Loading…
Cancel
Save