diff --git a/src/main/java/pacmanGame/Cell.java b/src/main/java/pacmanGame/Cell.java index a077a59..ce5247e 100644 --- a/src/main/java/pacmanGame/Cell.java +++ b/src/main/java/pacmanGame/Cell.java @@ -19,16 +19,36 @@ public class Cell { public void triggerPill() { this.type = "empty"; - gameManager.score += 100; - int timeStopPillEffect = gameManager.time + 200; - gameManager.ghostIsEdible = true; + map.gameManager.score += 100; + map.gameManager.timeStopPillEffect = map.gameManager.time + 200; + map.gameManager.ghostIsEdible = true; } - public void triggerFruit() { + public void triggerCherry() { this.type = "empty"; map.gameManager.score += 505; } + public void triggerStrawberry() { + this.type = "empty"; + map.gameManager.score += 405; + } + + public void triggerOrange() { + this.type = "empty"; + map.gameManager.score += 305; + } + + public void triggerApple() { + this.type = "empty"; + map.gameManager.score += 205; + } + public void triggerCrystal() { + this.type = "empty"; + map.gameManager.score += 506; + map.gameManager.moveSpeed = 3; + } + public void triggerItem() { if(type.equals("dot")) { triggerDot(); @@ -37,10 +57,30 @@ public class Cell { triggerPill(); } else if(type.equals("cherry")) { - triggerFruit(); + triggerCherry(); + } + else if(type.equals("strawberry")) { + triggerStrawberry(); + } + else if(type.equals("orange")) { + triggerOrange(); + } + else if(type.equals("apple")) { + triggerApple(); } + else if(type.equals("crystal")) { + triggerCrystal(); + } + else if(type.equals("tpLeft")) { + map.gameManager.teleportPlayer("tpRight"); + } + else if(type.equals("tpRight")) { + map.gameManager.teleportPlayer("tpLeft"); + } + else { //System.out.println("cell contains no item!"); } } } + diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 77158d4..c28aa8a 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -12,6 +12,8 @@ public class GameManager { public final int ghostCount = 10; public int livesRemaining; public int failedAttempts; + public int timeStopPillEffect; + public int moveSpeed; public GameManager() { livesRemaining = 3; @@ -20,6 +22,8 @@ public class GameManager { } public void setupGame(){ + time = 0; + moveSpeed = 5; ghosts = new Ghost[ghostCount]; for (int i = 0; i < ghosts.length; i++) { ghosts[i] = new Ghost(this, i); @@ -41,40 +45,53 @@ public class GameManager { if(livesRemaining <= 0) { gameOver(); //wenn Keine Leben mehr �brig sind. } + } public void gameOver() { System.out.println("Das Spiel ist vorbei! Pech!!"); } public void Update() { + + visualizer.Update(); if(!isPaused) { - visualizer.Update(); - if(time%5 == 0) { + if(time%moveSpeed == 0) { player.Move(); } + time++; } if(time == 300) { - spawnCherry(); + spawnFruit(); } if (time == 600) { - destroyCherry(); + destroyFruit(); + } + if(ghostIsEdible) { + if(time == timeStopPillEffect) + ghostIsEdible = false; } - time++; + } - public void spawnCherry() { - map.GetCell(map.playerSpawn).type = "cherry"; + public void spawnFruit() { + map.GetCell(map.playerSpawn).type = randomFruit(); } - public void destroyCherry() { + public void destroyFruit() { map.GetCell(map.playerSpawn).type = "empty"; } + + public String randomFruit() { + String fruitTypes[] = {"cherry","strawberry","orange","apple","crystal"}; + int randomNumber = (int) (Math. random() * (fruitTypes.length)); + return fruitTypes[randomNumber]; + } public void ProcessInput(char inputChar) { if(inputChar == 27) {// 27 = ESC TogglePause(); } - else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a') { + else if (inputChar == 'w' || inputChar == 's' || inputChar == 'd' || inputChar == 'a' || inputChar == 'r') { player.processInput(inputChar); } else { @@ -82,7 +99,13 @@ public class GameManager { } } - public boolean GhostPlayerCollisionTest() { + public void teleportPlayer(String destination) { + + player.position = map.findCellByString(destination).pos; + + } + + public boolean GhostPlayerColisionTest() { for(int i = 0; i < ghosts.length; i++) { diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index e88dc7e..075b30b 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -10,7 +10,7 @@ public class Map { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "ww............ww............ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww", - "ww.wwww.wwwww.ww.wwwww.wwww.ww", + "wwpwwww.wwwww.ww.wwwww.wwwwpww", "ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww..........................ww", "ww.wwww.ww.wwwwwwww.ww.wwww.ww", @@ -21,7 +21,7 @@ public class Map { "wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", - "eeeeeee.eeew804629weee.eeeeeee", + "Teeeeee.eeew804629weee.eeeeeet", "wwwwwww.wwewe1573eweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", @@ -30,7 +30,7 @@ public class Map { "ww............ww............ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww", - "ww...ww.......s........ww...ww", + "wwp..ww.......s........ww..pww", "wwww.ww.ww.wwwwwwww.ww.ww.wwww", "wwww.ww.ww.wwwwwwww.ww.ww.wwww", "ww......ww....ww....ww......ww", @@ -50,6 +50,9 @@ public class Map { this.put("s", "empty"); this.put(".", "dot"); this.put("w", "wall"); + this.put("p", "pill"); + this.put("T", "tpLeft"); + this.put("t", "tpRight"); this.put("0", "empty"); this.put("1", "empty"); this.put("2", "empty"); @@ -62,20 +65,19 @@ public class Map { this.put("9", "empty"); }}; - public Cell[][] cells; public Vector2 size; - - public Vector2 playerSpawn = new Vector2(2,2); + + public Vector2 playerSpawn = new Vector2(2, 2); public char playerSpawnChar = 's'; - public Map(String[] mapData, GameManager gameManager) { this.gameManager = gameManager; GenerateMap(mapData); } public void GenerateMap(String[] mapData) { + int sizeY = mapData.length; int sizeX = mapData[0].length(); @@ -106,18 +108,30 @@ public class Map { } } } + } public Cell GetCell(Vector2 pos) { - if(pos.isWithin(size)) { - - return cells[pos.x][pos.y]; + if (pos.isWithin(size)) { + + return cells[pos.x][pos.y]; + } else { + + return cells[0][0]; } - else { - - return cells[0][0]; + + } + + public Cell findCellByString(String target) { + for (int x = 0; x < size.x; x++) { + for (int y = 0; y < size.y; y++) { + if (cells[x][y].type.equals(target)) { + return cells[x][y]; + } + } } - + System.out.println("couldnt find cell " + target); + return null; } public String FindPath(Vector2 from, Vector2 to) diff --git a/src/main/java/pacmanGame/Player.java b/src/main/java/pacmanGame/Player.java index 0780614..c622521 100644 --- a/src/main/java/pacmanGame/Player.java +++ b/src/main/java/pacmanGame/Player.java @@ -4,8 +4,7 @@ public class Player { public Vector2 position; public Vector2 direction; public GameManager gameManager; - public Vector2 oldDirection; - public Vector2 futureDirection; + public Vector2 savedDirection; public Player(GameManager gameManager) { this.gameManager = gameManager; @@ -16,7 +15,6 @@ public class Player { public void Spawn() { position = gameManager.map.playerSpawn; direction = new Vector2(0, -1); - oldDirection = direction; } public void Move() { @@ -26,32 +24,47 @@ public class Player { if(!newPosIsWall) { position = newPosition; } - else if (oldDirection != direction) { - direction = oldDirection; - Move(); + if(savedDirection != null) { + checkInput(savedDirection); } + gameManager.updatePlayerCell(); } public void processInput(char inputchar) { - oldDirection = direction; - - if(inputchar == 'w') { - direction = new Vector2(0,1); + + if(inputchar == 'w') { + checkInput(new Vector2(0,1)); } else if(inputchar == 's') { - direction = new Vector2(0,-1); + checkInput(new Vector2(0,-1)); } else if(inputchar == 'd') { - direction = new Vector2(1,0); + checkInput(new Vector2(1,0)); } else if(inputchar == 'a') { - direction = new Vector2(-1,0); + checkInput(new Vector2(-1,0)); + } + else if(inputchar == 'r') { + gameManager.setupGame(); } } + public void checkInput(Vector2 input) { + Vector2 newPosition = position.Add(input); + boolean newPosIsWall = gameManager.map.GetCell(newPosition).type.equals("wall"); + if(!newPosIsWall) { + direction = input; + savedDirection = null; + } + else { + savedDirection = input; + } + } + } + diff --git a/src/main/java/pacmanGame/VisualizerPlainText.java b/src/main/java/pacmanGame/VisualizerPlainText.java index d9c4595..8a8eabb 100644 --- a/src/main/java/pacmanGame/VisualizerPlainText.java +++ b/src/main/java/pacmanGame/VisualizerPlainText.java @@ -12,6 +12,13 @@ public class VisualizerPlainText implements Visualizer { this.put("dot", ". "); this.put("wall", "[]"); this.put("cherry", "OO"); + this.put("strawberry", "T7"); + this.put("orange", "CO"); + this.put("apple", "@ "); + this.put("crystal", "<>"); + this.put("pill", "o "); + this.put("tpRight" , " "); + this.put("tpLeft" , " "); }}; public final String ghostSprite = "AA"; @@ -37,6 +44,7 @@ public class VisualizerPlainText implements Visualizer { @Override public void Update() { + output = ""; if(showScore) { output = "score: " + gameManager.score + "\n"; @@ -88,7 +96,4 @@ public class VisualizerPlainText implements Visualizer { output += "\n"; } } - - - } diff --git a/src/main/java/pacmanGame/VisualizerPlainTextUltra.java b/src/main/java/pacmanGame/VisualizerPlainTextUltra.java index bcc3cdc..579ef4f 100644 --- a/src/main/java/pacmanGame/VisualizerPlainTextUltra.java +++ b/src/main/java/pacmanGame/VisualizerPlainTextUltra.java @@ -26,10 +26,38 @@ public class VisualizerPlainTextUltra implements Visualizer { "|''|", "|__|" }); + this.put("pill", new String[] { + " O ", + " " + }); + this.put("tpLeft", new String[] { + " ", + " " + }); + this.put("tpRight", new String[] { + " ", + " " + }); this.put("cherry", new String[] { " /\\ ", " 0O " }); + this.put("strawberry", new String[] { + "( )", + " \\_/" + }); + this.put("orange", new String[] { + " __ ", + "(__)" + }); + this.put("apple", new String[] { + " , ", + " @ " + }); + this.put("crystal", new String[] { + " /\\ ", + " \\/ " + }); }}; public final String[][] ghostSprites = { diff --git a/src/test/java/pacmanTests/CellTest.java b/src/test/java/pacmanTests/CellTest.java new file mode 100644 index 0000000..4d58241 --- /dev/null +++ b/src/test/java/pacmanTests/CellTest.java @@ -0,0 +1,110 @@ +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 CellTest { + + @Test + void cell_triggerItem_505cherryScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "cherry"; + int expectedScore = 505; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_10dotScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "dot"; + int expectedScore = 10; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_100pillScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "pill"; + int expectedScore = 100; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_405strawberryScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "strawberry"; + int expectedScore = 405; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_305orangeScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "orange"; + int expectedScore = 305; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_205appleScore() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "apple"; + int expectedScore = 205; + // act + cell.triggerItem(); + int resultingScore = gameManager.score; + // assert + assertThat(expectedScore).isEqualTo(resultingScore); + } + + @Test + void cell_triggerItem_3crystalMoveSpeed() { + // arrange + GameManager gameManager = new GameManager(); + Cell cell = gameManager.map.GetCell(new Vector2(0,0)); + cell.type = "crystal"; + int expectedMoveSpeed = 3; + // act + cell.triggerItem(); + int resultingMoveSpeed = gameManager.moveSpeed; + // assert + assertThat(expectedMoveSpeed).isEqualTo(resultingMoveSpeed); + } +} diff --git a/src/test/java/pacmanTests/GameManagerTest.java b/src/test/java/pacmanTests/GameManagerTest.java index 31380d4..7b61411 100644 --- a/src/test/java/pacmanTests/GameManagerTest.java +++ b/src/test/java/pacmanTests/GameManagerTest.java @@ -55,7 +55,7 @@ class GameManagerTest { GameManager gameManager = new GameManager(); gameManager.player.position = gameManager.ghosts[0].position.Clone(); //act - boolean collision = gameManager.GhostPlayerCollisionTest(); + boolean collision = gameManager.GhostPlayerColisionTest(); boolean expected = true; // assert @@ -70,7 +70,7 @@ class GameManagerTest { gameManager.player.position = new Vector2(2,3); gameManager.ghosts[0].position = new Vector2(4,5); //act - boolean collision = gameManager.GhostPlayerCollisionTest(); + boolean collision = gameManager.GhostPlayerColisionTest(); boolean expected = false; // assert diff --git a/src/test/java/pacmanTests/MapTest.java b/src/test/java/pacmanTests/MapTest.java index 723fa63..5b12899 100644 --- a/src/test/java/pacmanTests/MapTest.java +++ b/src/test/java/pacmanTests/MapTest.java @@ -111,4 +111,22 @@ class MapTest { // assert assertThat(path).isEqualTo(expectedPath); } + + @Test + void Map_findCellByString_cellT() { + // arrange + String[] mapTest = { + "wew", + "w.t", + "ee." + }; + GameManager gameManager = new GameManager(); + gameManager.map = new Map(mapTest, gameManager); + Map testMap = gameManager.map; + Cell expectedT = testMap.cells[2][1]; + // act + Cell foundT = testMap.findCellByString("tpRight"); + // assert + assertThat(expectedT).isEqualTo(foundT); + } } diff --git a/src/test/java/pacmanTests/VisualizerPlainTextTest.java b/src/test/java/pacmanTests/VisualizerPlainTextTest.java index 7227f86..9306492 100644 --- a/src/test/java/pacmanTests/VisualizerPlainTextTest.java +++ b/src/test/java/pacmanTests/VisualizerPlainTextTest.java @@ -23,6 +23,7 @@ class VisualizerPlainTextTest { gameManager.map = new Map(mapTest, gameManager); VisualizerPlainText vpt = new VisualizerPlainText(gameManager); vpt.showScore = false; + String expected = "" + "[]. []\n" + " . \n"