Browse Source

Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7012/pacmayham

remotes/origin/visualizer
fdai7753 11 months ago
parent
commit
ca1b669f7c
  1. 33
      src/main/java/pacmanGame/Cell.java
  2. 15
      src/main/java/pacmanGame/GameManager.java
  3. 13
      src/main/java/pacmanGame/Map.java
  4. 4
      src/test/java/pacmanTests/GameManagerTest.java
  5. 8
      src/test/java/pacmanTests/MapTest.java
  6. 4
      src/test/java/pacmanTests/VisualizerPlainTextTest.java

33
src/main/java/pacmanGame/Cell.java

@ -3,9 +3,40 @@ package pacmanGame;
public class Cell { public class Cell {
public Vector2 pos; public Vector2 pos;
public String type; public String type;
public final Map map;
public Cell(Vector2 pos, String type) {
public Cell(Vector2 pos, String type, Map map) {
this.pos = pos; this.pos = pos;
this.type = type; this.type = type;
this.map = map;
} }
public void triggerDot() {
this.type = "empty";
map.gameManager.score += 10;
}
public void triggerPill() {
//todo: code
}
public void triggerFruit() {
//todo: code
}
public void triggerItem() {
if(type.equals("dot")) {
triggerDot();
}
else if(type.equals("pill")) {
triggerPill();
}
else if(type.equals("cherry")) {
triggerFruit();
}
else {
System.out.println("cell contains no item!");
}
}
} }

15
src/main/java/pacmanGame/GameManager.java

@ -6,13 +6,14 @@ public class GameManager {
public Map map; public Map map;
public Visualizer visualizer; public Visualizer visualizer;
public Player player; public Player player;
public int score = 0;
public GameManager() { public GameManager() {
setupGame(); setupGame();
} }
public void setupGame(){ public void setupGame(){
map = new Map();
map = new Map(Map.mapClassic,this);
visualizer = new VisualizerPlainText(this); visualizer = new VisualizerPlainText(this);
player = new Player(this); player = new Player(this);
player.Spawn(); player.Spawn();
@ -20,8 +21,8 @@ public class GameManager {
for(int i = 0; i < ghosts.length; i++) { for(int i = 0; i < ghosts.length; i++) {
ghosts[i] = new Ghost(this); ghosts[i] = new Ghost(this);
} }
} }
public void Update() { public void Update() {
visualizer.Update(); visualizer.Update();
if(time%5 == 0) { if(time%5 == 0) {
@ -30,23 +31,19 @@ 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
} }
else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a') { else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a') {
player.processInput(inputChar); player.processInput(inputChar);
} }
else { else {
System.out.println("Unprocessed Input: " + inputChar + " (" + (int)inputChar + ")"); System.out.println("Unprocessed Input: " + inputChar + " (" + (int)inputChar + ")");
} }
} }
public boolean GostPlayerColisionTest()
{
public boolean GhostPlayerColisionTest() {
for(int i = 0; i < ghosts.length; i++) { for(int i = 0; i < ghosts.length; i++) {
@ -56,9 +53,7 @@ public class GameManager {
{ {
return true; return true;
} }
} }
} }
} }

13
src/main/java/pacmanGame/Map.java

@ -3,7 +3,9 @@ package pacmanGame;
import java.util.HashMap; import java.util.HashMap;
public class Map { public class Map {
public final String[] mapClassic = {
public final GameManager gameManager;
public static String[] mapClassic = {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"ww............ww............ww", "ww............ww............ww",
@ -52,12 +54,9 @@ public class Map {
public Vector2 playerSpawn = new Vector2(2,2); public Vector2 playerSpawn = new Vector2(2,2);
public char playerSpawnChar = 's'; public char playerSpawnChar = 's';
public Map() {
GenerateMap(mapClassic);
}
public Map(String[] mapData) {
public Map(String[] mapData, GameManager gameManager) {
GenerateMap(mapData); GenerateMap(mapData);
this.gameManager = gameManager;
} }
public void GenerateMap(String[] mapData) { public void GenerateMap(String[] mapData) {
@ -77,7 +76,7 @@ public class Map {
String cellType = mapTypes.get(String.valueOf(cellChar)); String cellType = mapTypes.get(String.valueOf(cellChar));
cells[x][y] = new Cell(cellPos, cellType);
cells[x][y] = new Cell(cellPos, cellType, this);
if(cellChar == playerSpawnChar) { if(cellChar == playerSpawnChar) {
playerSpawn = cellPos.Clone(); playerSpawn = cellPos.Clone();

4
src/test/java/pacmanTests/GameManagerTest.java

@ -54,7 +54,7 @@ class GameManagerTest {
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.player.position = gameManager.ghosts[0].position.Clone(); gameManager.player.position = gameManager.ghosts[0].position.Clone();
//act //act
boolean colision = gameManager.GostPlayerColisionTest();
boolean colision = gameManager.GhostPlayerColisionTest();
boolean expected = true; boolean expected = true;
// assert // assert
@ -69,7 +69,7 @@ class GameManagerTest {
gameManager.player.position = new Vector2(2,3); gameManager.player.position = new Vector2(2,3);
gameManager.ghosts[0].position = new Vector2(4,5); gameManager.ghosts[0].position = new Vector2(4,5);
//act //act
boolean colision = gameManager.GostPlayerColisionTest();
boolean colision = gameManager.GhostPlayerColisionTest();
boolean expected = false; boolean expected = false;
// assert // assert

8
src/test/java/pacmanTests/MapTest.java

@ -22,7 +22,9 @@ class MapTest {
String expectedMiddle = "dot"; String expectedMiddle = "dot";
String expectedBottomRight = "dot"; String expectedBottomRight = "dot";
// act // act
Map testMap = new Map(mapTest);
GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest, gameManager);
Map testMap = gameManager.map;
String topLeft = testMap.cells[0][2].type; String topLeft = testMap.cells[0][2].type;
String middle = testMap.cells[1][1].type; String middle = testMap.cells[1][1].type;
@ -41,7 +43,9 @@ class MapTest {
"w..", "w..",
"ee." "ee."
}; };
Map testMap = new Map(mapTest);
GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest, gameManager);
Map testMap = gameManager.map;
Cell expectedTopLeft = testMap.cells[0][2]; Cell expectedTopLeft = testMap.cells[0][2];
Cell expectedMiddle = testMap.cells[1][1]; Cell expectedMiddle = testMap.cells[1][1];
Cell expectedBottomRight = testMap.cells[2][0]; Cell expectedBottomRight = testMap.cells[2][0];

4
src/test/java/pacmanTests/VisualizerPlainTextTest.java

@ -20,7 +20,7 @@ class VisualizerPlainTextTest {
}; };
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest);
gameManager.map = new Map(mapTest, gameManager);
VisualizerPlainText vpt = new VisualizerPlainText(gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager);
String expected = "" String expected = ""
@ -44,7 +44,7 @@ class VisualizerPlainTextTest {
}; };
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest);
gameManager.map = new Map(mapTest, gameManager);
VisualizerPlainText vpt = new VisualizerPlainText(gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager);
gameManager.ghosts[0].position = new Vector2(1, 1); gameManager.ghosts[0].position = new Vector2(1, 1);

Loading…
Cancel
Save