diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 4ffa477..d04c4c6 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -9,6 +9,7 @@ public class GameManager { public int score = 0; public boolean ghostIsEdible = false; public boolean isPaused; + public final int ghostCount = 10; public int livesRemaining; public int failedAttempts; @@ -19,18 +20,23 @@ public class GameManager { } public void setupGame(){ - map = new Map(Map.mapClassic,this); - visualizer = new VisualizerPlainText(this); - player = new Player(this); - player.Spawn(); - ghosts = new Ghost[4]; - for(int i = 0; i < ghosts.length; i++) { - ghosts[i] = new Ghost(this, 1); - } - isPaused = false; - } + ghosts = new Ghost[ghostCount]; + for (int i = 0; i < ghosts.length; i++) { + ghosts[i] = new Ghost(this, i); + } + map = new Map(Map.mapClassic, this); + visualizer = new VisualizerPlainText(this); + player = new Player(this); + player.Spawn(); + for (int i = 0; i < ghosts.length; i++) { + ghosts[i].spawn(); + } + isPaused = false; + } + public void handleGhostCollision() { livesRemaining--; // Reduziere verbleibende leben um 1 + failedAttempts++; // Erhöhe die Anzahl der Fehlversuche if(livesRemaining <= 0) { gameOver(); //wenn Keine Leben mehr übrig sind. @@ -76,7 +82,7 @@ public class GameManager { } } - public boolean GhostPlayerColisionTest() { + public boolean GhostPlayerCollisionTest() { for(int i = 0; i < ghosts.length; i++) { diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index 26e9845..a7acce0 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -19,4 +19,8 @@ public class Ghost { public void move(Vector2 direction) { this.position = this.position.Add(direction); } + + public void spawn() { + this.position = gameManager.map.ghostSpawns[ghostNumber].Clone(); + } } diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 21051b1..e88dc7e 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -68,10 +68,11 @@ public class Map { public Vector2 playerSpawn = new Vector2(2,2); public char playerSpawnChar = 's'; + public Map(String[] mapData, GameManager gameManager) { - GenerateMap(mapData); this.gameManager = gameManager; + GenerateMap(mapData); } public void GenerateMap(String[] mapData) { @@ -82,6 +83,8 @@ public class Map { cells = new Cell[size.x][size.y]; + ghostSpawns = new Vector2[10]; + for(int x = 0; x < size.x; x++) { for(int y = 0; y < size.y; y++) { Vector2 cellPos = new Vector2(x,y); @@ -92,6 +95,12 @@ public class Map { cells[x][y] = new Cell(cellPos, cellType, this); + for(int i = 0; i < gameManager.ghosts.length; i++) { + if(cellChar == ghostSpawnChars.charAt(i)) { + ghostSpawns[i] = cellPos.Clone(); + } + } + if(cellChar == playerSpawnChar) { playerSpawn = cellPos.Clone(); } @@ -110,28 +119,7 @@ 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); - } - } - } - } - + public String FindPath(Vector2 from, Vector2 to) { char[][] tempMap = new char[size.x][size.y]; diff --git a/src/main/java/pacmanGame/VisualizerPlainText.java b/src/main/java/pacmanGame/VisualizerPlainText.java index 9dccc96..d9c4595 100644 --- a/src/main/java/pacmanGame/VisualizerPlainText.java +++ b/src/main/java/pacmanGame/VisualizerPlainText.java @@ -15,6 +15,15 @@ public class VisualizerPlainText implements Visualizer { }}; public final String ghostSprite = "AA"; + public final String ghostSprite1 = "BB"; + public final String ghostSprite2 = "CC"; + public final String ghostSprite3 = "DD"; + public final String ghostSprite4 = "EE"; + public final String ghostSprite5 = "FF"; + public final String ghostSprite6 = "GG"; + public final String ghostSprite7 = "HH"; + public final String ghostSprite8 = "II"; + public final String ghostSprite9 = "JJ"; public final String playerSprite = "()"; public VisualizerPlainText(GameManager gameManager) { diff --git a/src/test/java/pacmanTests/GameManagerTest.java b/src/test/java/pacmanTests/GameManagerTest.java index 2bbea12..183a89f 100644 --- a/src/test/java/pacmanTests/GameManagerTest.java +++ b/src/test/java/pacmanTests/GameManagerTest.java @@ -54,14 +54,14 @@ class GameManagerTest { GameManager gameManager = new GameManager(); gameManager.player.position = gameManager.ghosts[0].position.Clone(); //act - boolean colision = gameManager.GhostPlayerColisionTest(); + boolean collision = gameManager.GhostPlayerCollisionTest(); boolean expected = true; // assert - assertThat(colision).isEqualTo(expected); + assertThat(collision).isEqualTo(expected); } @Test - void GameManager_GostPlayerColisionTest_DoesntDetectColosion() { + void GameManager_GostPlayerCollisionTest_DoesntDetectColliosion() { // arrange @@ -69,11 +69,24 @@ class GameManagerTest { gameManager.player.position = new Vector2(2,3); gameManager.ghosts[0].position = new Vector2(4,5); //act - boolean colision = gameManager.GhostPlayerColisionTest(); + boolean collision = gameManager.GhostPlayerCollisionTest(); boolean expected = false; // assert - assertThat(colision).isEqualTo(expected); + assertThat(collision).isEqualTo(expected); + } + @Test + void GameManager_setupgame_spawnsghosts() { + + // arrange + GameManager gameManager = new GameManager(); + gameManager.map.ghostSpawns[0] = new Vector2(1, 1); + Vector2 expectedPosition = new Vector2(1, 1); + // act + gameManager.ghosts[0].spawn(); + Vector2 ghostPosition = gameManager.ghosts[0].position; + // assert + assertThat(ghostPosition).isEqualTo(expectedPosition); } @Test diff --git a/src/test/java/pacmanTests/GhostTest.java b/src/test/java/pacmanTests/GhostTest.java new file mode 100644 index 0000000..75344db --- /dev/null +++ b/src/test/java/pacmanTests/GhostTest.java @@ -0,0 +1,11 @@ +package pacmanTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + + +class GhostTest { + +} \ No newline at end of file