Browse Source

added findCellByString method to Map

remotes/origin/player
fdai7910 11 months ago
parent
commit
7380944cfc
  1. 126
      src/main/java/pacmanGame/Map.java

126
src/main/java/pacmanGame/Map.java

@ -3,98 +3,90 @@ package pacmanGame;
import java.util.HashMap; import java.util.HashMap;
public class Map { public class Map {
public final GameManager gameManager; public final GameManager gameManager;
public static String[] mapClassic = {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"ww............ww............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",
"ww.wwww.ww.wwwwwwww.ww.wwww.ww",
"ww......ww....ww....ww......ww",
"wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"eeeeeee.eeeweeeeeeweee.eeeeeee",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"ww............ww............ww",
"ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.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",
"ww.wwwwwwwwww.ww.wwwwwwwwww.ww",
"ww.wwwwwwwwww.ww.wwwwwwwwww.ww",
"ww..........................ww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
};
public final HashMap<String, String> mapTypes = new HashMap<String,String>(){{
this.put("e", "empty");
this.put("s", "empty");
this.put(".", "dot");
this.put("w", "wall");
this.put("p", "pill");
}};
public static String[] mapClassic = { "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"ww............ww............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",
"ww.wwww.ww.wwwwwwww.ww.wwww.ww", "ww......ww....ww....ww......ww", "wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wwwwwewwewwwww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "Teeeeee.eeeweeeeeeweee.eeeeeet", "wwwwwww.wweweeeeeeweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wwewwwwwwwweww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww", "ww............ww............ww", "ww.wwww.wwwww.ww.wwwww.wwww.ww",
"ww.wwww.wwwww.ww.wwwww.wwww.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", "ww.wwwwwwwwww.ww.wwwwwwwwww.ww",
"ww.wwwwwwwwww.ww.wwwwwwwwww.ww", "ww..........................ww", "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwww" };
public final HashMap<String, String> mapTypes = new HashMap<String, String>() {
{
this.put("e", "empty");
this.put("s", "empty");
this.put(".", "dot");
this.put("w", "wall");
this.put("p", "pill");
this.put("T", "tpLeft");
this.put("t", "tpRight");
}
};
public Cell[][] cells; public Cell[][] cells;
public Vector2 size; public Vector2 size;
public Vector2 playerSpawn = new Vector2(2,2);
public Vector2 playerSpawn = new Vector2(2, 2);
public char playerSpawnChar = 's'; public char playerSpawnChar = 's';
public Map(String[] mapData, GameManager gameManager) { public Map(String[] mapData, GameManager gameManager) {
GenerateMap(mapData); GenerateMap(mapData);
this.gameManager = gameManager; this.gameManager = gameManager;
} }
public void GenerateMap(String[] mapData) { public void GenerateMap(String[] mapData) {
int sizeY = mapData.length; int sizeY = mapData.length;
int sizeX = mapData[0].length(); int sizeX = mapData[0].length();
size = new Vector2(sizeX, sizeY); size = new Vector2(sizeX, sizeY);
cells = new Cell[size.x][size.y]; cells = new Cell[size.x][size.y];
for(int x = 0; x < size.x; x++) {
for(int y = 0; y < size.y; y++) {
Vector2 cellPos = new Vector2(x,y);
for (int x = 0; x < size.x; x++) {
for (int y = 0; y < size.y; y++) {
Vector2 cellPos = new Vector2(x, y);
char cellChar = mapData[size.y - 1 - y].charAt(x); char cellChar = mapData[size.y - 1 - y].charAt(x);
String cellType = mapTypes.get(String.valueOf(cellChar)); String cellType = mapTypes.get(String.valueOf(cellChar));
cells[x][y] = new Cell(cellPos, cellType, this); cells[x][y] = new Cell(cellPos, cellType, this);
if(cellChar == playerSpawnChar) {
if (cellChar == playerSpawnChar) {
playerSpawn = cellPos.Clone(); playerSpawn = cellPos.Clone();
} }
}
}
} }
} }
public Cell GetCell(Vector2 pos) { 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 == target) {
return cells[x][y];
}
}
} }
return null;
} }
} }
Loading…
Cancel
Save