Browse Source

Merge branch 'ghosts'

remotes/origin/ghostsbehaviour
fdai7753 11 months ago
parent
commit
3e58cc98d3
  1. 21
      src/main/java/pacmanGame/GameManager.java
  2. 22
      src/main/java/pacmanGame/Ghost.java
  3. 36
      src/main/java/pacmanGame/Map.java
  4. 11
      src/test/java/pacmanTests/MapTest.java

21
src/main/java/pacmanGame/GameManager.java

@ -15,18 +15,15 @@ public class GameManager {
} }
public void setupGame(){ 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);
}
isPaused = false;
}
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, i);
}
}
public void Update() { public void Update() {
visualizer.Update(); visualizer.Update();
if(time%5 == 0) { if(time%5 == 0) {

22
src/main/java/pacmanGame/Ghost.java

@ -1,12 +1,22 @@
package pacmanGame; package pacmanGame;
public class Ghost { public class Ghost {
public Vector2 position;
public final GameManager gameManager;
public Vector2 position;
public final GameManager gameManager;
public final int ghostNumber;
public Ghost(GameManager gameManager) {
this.gameManager = gameManager;
this.position = new Vector2(-1, -1);
}
public Ghost(GameManager gameManager, int ghostNumber) {
this.gameManager = gameManager;
this.ghostNumber = ghostNumber;
this.position = new Vector2(-1, -1);
}
public void setPosition(Vector2 newPosition) {
this.position = newPosition;
}
public void move(Vector2 direction) {
this.position = this.position.Add(direction);
}
} }

36
src/main/java/pacmanGame/Map.java

@ -21,8 +21,8 @@ public class Map {
"wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww",
"eeeeeee.eeeweeeeeeweee.eeeeeee",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"eeeeeee.eeewe0ee2eweee.eeeeeee",
"wwwwwww.wwewe1ee3eweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww",
@ -41,13 +41,22 @@ public class Map {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww" "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
}; };
public Vector2[] ghostSpawns;
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");
this.put("s", "empty"); this.put("s", "empty");
this.put(".", "dot"); this.put(".", "dot");
this.put("w", "wall"); this.put("w", "wall");
for(int i = 0; i < 10; i++) {
this.put(i+"", "empty");
}
}}; }};
public Cell[][] cells; public Cell[][] cells;
public Vector2 size; public Vector2 size;
@ -78,6 +87,8 @@ public class Map {
cells[x][y] = new Cell(cellPos, cellType, this); cells[x][y] = new Cell(cellPos, cellType, this);
//for(int i = 0; i < gameManager.ghosts.length; i++)
if(cellChar == playerSpawnChar) { if(cellChar == playerSpawnChar) {
playerSpawn = cellPos.Clone(); playerSpawn = cellPos.Clone();
} }
@ -96,4 +107,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);
}
}
}
}
} }

11
src/test/java/pacmanTests/MapTest.java

@ -21,14 +21,13 @@ class MapTest {
String expectedTopLeft = "wall"; String expectedTopLeft = "wall";
String expectedMiddle = "dot"; String expectedMiddle = "dot";
String expectedBottomRight = "dot"; String expectedBottomRight = "dot";
// act
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
// act
gameManager.map = new Map(mapTest, gameManager); gameManager.map = new Map(mapTest, gameManager);
Map testMap = gameManager.map;
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);
@ -45,7 +44,7 @@ class MapTest {
}; };
GameManager gameManager = new GameManager(); GameManager gameManager = new GameManager();
gameManager.map = new Map(mapTest, gameManager); gameManager.map = new Map(mapTest, gameManager);
Map testMap = gameManager.map;
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];

Loading…
Cancel
Save