mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 11:29:46 +02:00
erster entwurf der mapengine zum automatischen auswerten einer karte
This commit is contained in:
@@ -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<Integer> tileID;
|
||||||
|
Map<Integer, ArrayList<Objekt>> tilesMap;
|
||||||
|
MapEngine() {
|
||||||
|
tiles = new int[24][32];
|
||||||
|
tileID = new Stack<Integer>();
|
||||||
|
tilesMap = new HashMap<Integer, ArrayList<Objekt>>();
|
||||||
|
ArrayList<Objekt> typeArrayListOne = new ArrayList<Objekt>();
|
||||||
|
ArrayList<Objekt> typeArrayListTwo = new ArrayList<Objekt>();
|
||||||
|
ArrayList<Objekt> typeArrayListThree = new ArrayList<Objekt>();
|
||||||
|
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<Objekt> getAllTilesOfType(int i) {
|
||||||
|
return tilesMap.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user