Pac-Man/src/Map.java

93 lines
3.2 KiB
Java

import java.io.BufferedReader;
import java.io.FileReader;
// Diese Klasse ist zuständig für das Laden der Karte
// In der Textdatei map.txt enthalten im "assets" - Ordner kann man anhand von '#' Positionen von Bricks festlegen. Dort wo sich kein '#' befindet, ist also eine Lücke.
public class Map {
public static Brick bricks[][] = new Brick[35][35]; // Zweidimensionales Array für alle Bricks
public static Point points[][] = new Point[35][35];
public static BigPoint bigpoints[][] = new BigPoint[35][35];
// Zweidimensionale Arrays siehe
// https://de.wikipedia.org/wiki/Feld_(Datentyp)#Mehrdimensional_/_in-sich-mehrdimensional
BufferedReader reader; // reader zum Einlesen der Text Datei
Player player;
Ghost ghosts[];
public static int ghost_posX[] = new int[4];
public static int ghost_posY[] = new int[4];
public static int maxScore;
public static int pac_posX;
public static int pac_posY;
String line; // String in dem eingelsene Zeilen der Datei gespeichert werden
public Map(Player player, Ghost ghosts[]) { // Erstellen des Konstruktors
this.ghosts = ghosts;
this.player = player;
}
public void mapping() {
try {
reader = new BufferedReader(new FileReader("assets/map.txt")); // Einlesen der .txt Datei
for (int i = 0; i < 35; i++) { // für die ersten 35 Zeilen der Datei:
String line = reader.readLine(); // Einlesen der jeweiligen Zeile
for (int j = 0; j < 35; j++) { // für die ersten 35 Zeichen der jeweiligen Zeile
try {
if (line.charAt(j) == '#') { // Erkennen ob sich dort ein '#' befindet
bricks[i][j] = new Brick(10 + 20 * j, 10 + 20 * i); // wenn ja, soll an der Stelle ein neuer
// Brick erstellt werden
} else if (line.charAt(j) == 'P') {
player.setPos('x', 10 + 20 * j);
player.setPos('y', 10 + 20 * i);
pac_posX = 10 + 20 * j;
pac_posY = 10 + 20 * i;
} else if (line.charAt(j) == '0') {
ghosts[0].setPos('x', 10 + 20 * j);
ghosts[0].setPos('y', 10 + 20 * i);
ghost_posX[0] = 10 + 20 * j;
ghost_posY[0] = 10 + 20 * i;
} else if (line.charAt(j) == '1') {
ghosts[1].setPos('x', 10 + 20 * j);
ghosts[1].setPos('y', 10 + 20 * i);
ghost_posX[1] = 10 + 20 * j;
ghost_posY[1] = 10 + 20 * i;
} else if (line.charAt(j) == '2') {
ghosts[2].setPos('x', 10 + 20 * j);
ghosts[2].setPos('y', 10 + 20 * i);
ghost_posX[2] = 10 + 20 * j;
ghost_posY[2] = 10 + 20 * i;
} else if (line.charAt(j) == '3') {
ghosts[3].setPos('x', 10 + 20 * j);
ghosts[3].setPos('y', 10 + 20 * i);
ghost_posX[3] = 10 + 20 * j;
ghost_posY[3] = 10 + 20 * i;
} else if (line.charAt(j) == ' ') {
points[i][j] = new Point(10 + 20 * j, 10 + 20 * i);
maxScore++;
} else if (line.charAt(j) == '.') {
bigpoints[i][j] = new BigPoint(10 + 20 * j, 10 + 20 * i);
}
} catch (Exception e) {
}
}
}
} catch (Exception e) { // Falls etwas mit dem Einlesen der Datei schiefläuft:
System.out.println(
"There seems to be an error with the file of the map. Pls use hashes to indicate positions of Walls. ERROR: "
+ e);
}
}
}