Browse Source

Fixed ghosts branch crashes

remotes/origin/ghosts
fdai7753 11 months ago
parent
commit
c25a45f196
  1. 2
      src/main/java/pacmanGame/GameManager.java
  2. 1
      src/main/java/pacmanGame/Ghost.java
  3. 34
      src/main/java/pacmanGame/Map.java
  4. 14
      src/test/java/pacmanTests/MapTest.java
  5. 4
      src/test/java/pacmanTests/VisualizerPlainTextTest.java

2
src/main/java/pacmanGame/GameManager.java

@ -12,7 +12,7 @@ public class GameManager {
} }
public void setupGame(){ public void setupGame(){
map = new Map();
map = new Map(this);
visualizer = new VisualizerPlainText(this); visualizer = new VisualizerPlainText(this);
player = new Player(this); player = new Player(this);
player.Spawn(); player.Spawn();

1
src/main/java/pacmanGame/Ghost.java

@ -4,6 +4,7 @@ public class Ghost {
public Vector2 position; public Vector2 position;
public final GameManager gameManager; public final GameManager gameManager;
public final int ghostNumber; public final int ghostNumber;
public Ghost(GameManager gameManager, int ghostNumber) { public Ghost(GameManager gameManager, int ghostNumber) {
this.gameManager = gameManager; this.gameManager = gameManager;

34
src/main/java/pacmanGame/Map.java

@ -41,7 +41,7 @@ public class Map {
public Vector2[] ghostSpawns; public Vector2[] ghostSpawns;
public String ghostSpawnChars = "0123456789"; public String ghostSpawnChars = "0123456789";
public final HashMap<String, String> mapTypes = new HashMap<String,String>(){{ public final HashMap<String, String> mapTypes = new HashMap<String,String>(){{
this.put("e", "empty"); this.put("e", "empty");
@ -53,6 +53,7 @@ public class Map {
this.put(i+"", "empty"); this.put(i+"", "empty");
} }
}}; }};
public GameManager gameManager;
public Cell[][] cells; public Cell[][] cells;
public Vector2 size; public Vector2 size;
@ -60,12 +61,14 @@ 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() {
public Map(GameManager gameManager) {
initializeGhostSpawns();
GenerateMap(mapClassic); GenerateMap(mapClassic);
this.gameManager = gameManager;
} }
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) {
@ -87,6 +90,8 @@ public class Map {
cells[x][y] = new Cell(cellPos, cellType); cells[x][y] = new Cell(cellPos, cellType);
//for(int i = 0; i < gameManager.ghosts.length; i++)
if(cellChar == playerSpawnChar) { if(cellChar == playerSpawnChar) {
playerSpawn = cellPos.Clone(); playerSpawn = cellPos.Clone();
} }
@ -105,4 +110,25 @@ public class Map {
} }
} }
private void initializeGhostSpawns() {
int count = 0;
for (int y = 0; y < mapClassic.length; y++) {
for (int x = 0; x < mapClassic[y].length(); x++) {
char cellChar = mapClassic[y].charAt(x);
if (ghostSpawnChars.indexOf(cellChar) != -1) {
count++;
}
}
}
ghostSpawns = new Vector2[count];
count = 0;
for (int y = 0; y < mapClassic.length; y++) {
for (int x = 0; x < mapClassic[y].length(); x++) {
char cellChar = mapClassic[y].charAt(x);
if (ghostSpawnChars.indexOf(cellChar) != -1) {
ghostSpawns[count++] = new Vector2(x, mapClassic.length - 1 - y);
}
}
}
}
} }

14
src/test/java/pacmanTests/MapTest.java

@ -21,12 +21,13 @@ class MapTest {
String expectedTopLeft = "wall"; String expectedTopLeft = "wall";
String expectedMiddle = "dot"; String expectedMiddle = "dot";
String expectedBottomRight = "dot"; String expectedBottomRight = "dot";
GameManager gameManager = new GameManager();
// act // act
Map testMap = new Map(mapTest);
gameManager.map = new Map(mapTest, gameManager);
String topLeft = testMap.cells[0][2].type;
String middle = testMap.cells[1][1].type;
String bottomRight = testMap.cells[2][0].type;
String topLeft = gameManager.map.cells[0][2].type;
String middle = gameManager.map.cells[1][1].type;
String bottomRight = gameManager.map.cells[2][0].type;
// assert // assert
assertThat(expectedTopLeft).isEqualTo(topLeft); assertThat(expectedTopLeft).isEqualTo(topLeft);
assertThat(expectedMiddle).isEqualTo(middle); assertThat(expectedMiddle).isEqualTo(middle);
@ -41,7 +42,10 @@ class MapTest {
"w..", "w..",
"ee." "ee."
}; };
Map testMap = new Map(mapTest);
GameManager gameManager = new GameManager();
// act
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