diff --git a/HeatUp/de/heatup/MapEngine/MapEngine.java b/HeatUp/de/heatup/MapEngine/MapEngine.java new file mode 100644 index 0000000..60a48f6 --- /dev/null +++ b/HeatUp/de/heatup/MapEngine/MapEngine.java @@ -0,0 +1,94 @@ +package mapEngine; + +import java.awt.Color; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; +import java.util.Stack; + +import javax.swing.JPanel; + +import mapEngine2.Objekt; + +public class MapEngine { + int[][] tiles; + Stack tileID; + Map> tilesMap; + MapEngine() { + tiles = new int[24][32]; + tileID = new Stack(); + tilesMap = new HashMap>(); + ArrayList typeArrayListOne = new ArrayList(); + ArrayList typeArrayListTwo = new ArrayList(); + ArrayList typeArrayListThree = new ArrayList(); + tilesMap.put(1, typeArrayListOne); + tilesMap.put(2, typeArrayListTwo); + tilesMap.put(3, typeArrayListThree); + Scanner input = null; + try { + input = new Scanner(new File("prp/spielfeld.txt")); + } catch (FileNotFoundException e) { + System.out.println("Konnte Map Datei nicht finden"); + System.exit(-1); + e.printStackTrace(); + } + this.createTiles(input); + } + private void createTiles(Scanner input) { + int lineCount = 0; + while (input.hasNextLine()) { + String currentLine = input.nextLine(); + for (int i = 0; i<32; i++) { + tiles[lineCount][i] = defineTileType(currentLine.charAt(i)); + } + lineCount++; + } + } + private int defineTileType(char charAt) { + 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 y = 0; + gamePanel.setBackground(new Color(500)); // hintergrund gamepanel auf blau setzen, bitte entfernen, ist unnötig + for (int i = 0; i<24; i++) { + for (int j = 0; j < 32; j++) { + Objekt newGameTile = new Objekt("Images/leiterbahn_lr_25.png"); + System.out.println(tiles[i][j]); + newGameTile.setObjectType(tiles[i][j]); + newGameTile.setPosition(x, y); + if (x+25 >= 800) { + x = 0; + y += 25; + } else { + x += 25; + } + gamePanel.add(newGameTile.getLabel()); + tilesMap.get(tiles[i][j]).add(newGameTile); + + } + + } + gamePanel.repaint(); + } + public ArrayList getAllTilesOfType(int i) { + return tilesMap.get(i); + } +}