From 41abb93e7b59ceab86b063193cce5973f39fb1be Mon Sep 17 00:00:00 2001 From: fdai7753 Date: Wed, 7 Feb 2024 17:23:25 +0100 Subject: [PATCH] refactoring: Making the ghost spawns readable --- src/main/java/pacmanGame/GameManager.java | 8 +++--- src/main/java/pacmanGame/Ghost.java | 2 +- src/main/java/pacmanGame/Map.java | 34 ++++++++--------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/main/java/pacmanGame/GameManager.java b/src/main/java/pacmanGame/GameManager.java index 21ea44a..8d42687 100644 --- a/src/main/java/pacmanGame/GameManager.java +++ b/src/main/java/pacmanGame/GameManager.java @@ -15,14 +15,14 @@ public class GameManager { } public void setupGame(){ + ghosts = new Ghost[4]; + 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(); - ghosts = new Ghost[4]; - for (int i = 0; i < ghosts.length; i++) { - ghosts[i] = new Ghost(this, i); - } } public void Update() { visualizer.Update(); diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index 7868fea..29704a4 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -21,6 +21,6 @@ public class Ghost { } public void spawn() { - + //this.position = gameManager.map; } } 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];