Map Engine auf ClassLoader geaendert.

This commit is contained in:
toksikk
2014-11-24 21:40:07 +01:00
parent 72e02c3c9a
commit 2d0c6ba611
2 changed files with 20 additions and 17 deletions
+12 -8
View File
@@ -11,6 +11,7 @@ import java.util.Scanner;
import javax.swing.JPanel;
import de.heatup.cfg.Config;
import de.heatup.highscore.HighscoreHandler;
import de.heatup.logging.Logger;
import de.heatup.objects.*;
/**
@@ -31,6 +32,10 @@ public class MapEngine {
private final int gamePanelWidth = Config.gamePanelWidth;
private static String mapString = "";
private final HighscoreHandler mapHighscore;
HashMap<Character, ArrayList<Point>> allMapTiles = new HashMap<Character, ArrayList<Point>>();
ArrayList<Character> availableHardwareTileTypes = new ArrayList<Character>();
/**
@@ -39,28 +44,26 @@ public class MapEngine {
*/
public MapEngine(int mapNumber) {
Scanner input = null;
try {
input = new Scanner(MapLoader.loadMap(mapNumber));
} catch (FileNotFoundException e) {
Logger.write("Could not find map file");
System.exit(-1);
e.printStackTrace();
}
MapLoader ml = new MapLoader();
input = new Scanner(ml.loadMap(mapNumber));
this.createTiles(input);
mapHighscore = new HighscoreHandler(mapString);
}
/**
* Öffnet einen File Chooser um eine Custom Map auszuwählen, welche anschließend geladen wird.
*/
public MapEngine() {
Scanner input = null;
MapLoader ml = new MapLoader();
try {
input = new Scanner(MapLoader.chooseMap());
input = new Scanner(ml.chooseMap());
} catch (FileNotFoundException e) {
Logger.write("Map file not found");
e.printStackTrace();
}
this.createTiles(input);
mapHighscore = new HighscoreHandler(mapString);
}
/**
* Erstellt aus einem gegebenen Scanner, welche eine Datei einliest,
@@ -73,6 +76,7 @@ public class MapEngine {
int y = 0;
while (input.hasNextLine() && lineCount <= mapHeight) {
String currentLine = input.nextLine();
mapString = mapString+currentLine;
for (int i = 0; i<mapWidth; i++) {
/* MapGrid implementation */
if (checkIfPath(currentLine.charAt(i))) {
@@ -2,6 +2,7 @@ package de.heatup.mapengine;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;
@@ -9,10 +10,11 @@ import de.heatup.cfg.Config;
import de.heatup.logging.Logger;
public class MapLoader {
protected static File chooseMap() {
protected File chooseMap() {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File mapFile = null;
try {
Config.mapFilePath = chooser.getSelectedFile().getCanonicalPath().toString();
mapFile = chooser.getSelectedFile();
@@ -23,13 +25,10 @@ public class MapLoader {
}
return mapFile;
}
protected static File loadMap(int mapNumber) {
File mapFile = null;
String mapPath = "src/de/heatup/resources/maps/map"+mapNumber+".txt";
mapFile = new File(mapPath);
Logger.write("Map \""+ mapPath + "\" loaded");
return mapFile;
protected InputStream loadMap(int mapNumber) {
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("de/heatup/resources/maps/map"+mapNumber+".txt");
Logger.write("Map de/heatup/resources/maps/map"+mapNumber+".txt loaded");
return is;
}
}