erweitertes tile handling in der map engine

This commit is contained in:
toksikk
2014-10-31 15:25:46 +01:00
parent 2f432346ce
commit 98b3908b77
+44 -25
View File
@@ -2,9 +2,12 @@ package de.heatup.mapengine;
import java.awt.Point;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner; import java.util.Scanner;
import javax.swing.JPanel; import javax.swing.JPanel;
@@ -13,12 +16,15 @@ import de.heatup.logging.Logger;
import de.heatup.objects.*; import de.heatup.objects.*;
public class MapEngine { public class MapEngine {
final int mapHeight = 24;
final int mapWidth = 32;
HashMap<Character, ArrayList<Point>> allMapTiles = new HashMap<Character, ArrayList<Point>>();
char[][] tiles; char[][] tiles;
MapEngine() { MapEngine(int mapNumber) {
tiles = new char[24][32]; tiles = new char[mapHeight][mapWidth];
Scanner input = null; Scanner input = null;
try { try {
input = new Scanner(new File("src/de/heatup/resources/maps/map1.txt")); input = new Scanner(new File("src/de/heatup/resources/maps/map"+mapNumber+".txt"));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Logger.write("Konnte Map Datei nicht finden"); Logger.write("Konnte Map Datei nicht finden");
System.exit(-1); System.exit(-1);
@@ -28,35 +34,41 @@ public class MapEngine {
} }
private void createTiles(Scanner input) { private void createTiles(Scanner input) {
int lineCount = 0; int lineCount = 0;
while (input.hasNextLine()) { int x = 0;
int y = 0;
while (input.hasNextLine() && lineCount <= mapHeight) {
String currentLine = input.nextLine(); String currentLine = input.nextLine();
for (int i = 0; i<32; i++) { for (int i = 0; i<mapWidth; i++) {
tiles[lineCount][i] = currentLine.charAt(i); tiles[lineCount][i] = currentLine.charAt(i); // ich glaube die zeile ist schon quasi-obsolet
/*
* setze die koordinaten (value) des tiles nach typ (key) in eine hashmap
*/
if (!allMapTiles.containsKey(currentLine.charAt(i))) {
ArrayList<Point> newTileType = new ArrayList<Point>();
newTileType.add(new Point(x,y));
allMapTiles.put(currentLine.charAt(i), newTileType);
} else {
allMapTiles.get(currentLine.charAt(i)).add(new Point(x,y));
}
/*
* setze x und y auf die koordinaten für das objekt im NÄCHSTEN durchlauf
*/
if (x+25 >= 800) {
x = 0;
y += 25;
} else {
x += 25;
}
} }
lineCount++; lineCount++;
} }
} }
// private int defineTileType(char charAt) { public void addAllGameTilesToGamePanel(JPanel gamePanel) throws IOException {
// for (char i = '0'; i<'9';i++) {
// if (charAt == i) {
// tileID.push(i-'0');
// return 2;
// }
// }
// switch (charAt) {
// case '#':
// return 1;
// case '-':
// return 3;
// default:
// return 0;
// }
// }
public void addAllGameTilesToPanel(JPanel gamePanel) throws IOException {
int x = 0; int x = 0;
int y = 0; int y = 0;
for (int i = 0; i<24; i++) { for (int i = 0; i<mapHeight; i++) {
for (int j = 0; j < 32; j++) { for (int j = 0; j < mapWidth; j++) {
Objekt newGameTile = new Objekt(getTiles()[i][j]); Objekt newGameTile = new Objekt(getTiles()[i][j]);
newGameTile.setPosition(x, y); newGameTile.setPosition(x, y);
if (x+25 >= 800) { if (x+25 >= 800) {
@@ -66,6 +78,10 @@ public class MapEngine {
x += 25; x += 25;
} }
gamePanel.add(newGameTile.getLabel()); gamePanel.add(newGameTile.getLabel());
/*
* natürlich performanter, wenn es außerhalb der schleifen steht, aber so
* sieht es cooler aus :)
*/
gamePanel.repaint(); gamePanel.repaint();
} }
@@ -78,4 +94,7 @@ public class MapEngine {
private char[][] getTiles() { private char[][] getTiles() {
return tiles; return tiles;
} }
public HashMap<Character, ArrayList<Point>> getAllMapTiles() {
return allMapTiles;
}
} }