From e1d24d05dd61d1637feea85c43b04eb87a79c340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 23 Apr 2016 15:07:48 +0200 Subject: [PATCH] started implementing map parser in Dungeon.buildDungeon() also added an example map file: exampledungeon.dungeon. work to be done: implement rest of the parser, so it sets up cross references between Tiles correctly. --- cicaprojekt/Dungeon.java | 80 ++++++++++++++++++++++++++++++++++++++-- exampledungeon.dungeon | 7 ++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 exampledungeon.dungeon diff --git a/cicaprojekt/Dungeon.java b/cicaprojekt/Dungeon.java index 7f471dd..bf2a67e 100644 --- a/cicaprojekt/Dungeon.java +++ b/cicaprojekt/Dungeon.java @@ -1,12 +1,86 @@ package cicaprojekt; -import java.io.File; +import java.io.*; public class Dungeon { - cicaprojekt.Tile buildDungeon(File input){ + cicaprojekt.Tile buildDungeon(File input) throws IOException + { Menu.addTab(); System.out.println(">" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon(" + input + ")"); + + try(BufferedReader reader = new BufferedReader(new FileReader(input))) + { + String[] sizedata = reader.readLine().split("x"); // read size data at beginning of file + reader.readLine(); // throw empty line away + int width = Integer.parseInt(sizedata[0]); + int height = Integer.parseInt(sizedata[1]); + + Tile[][] dungeon = new Tile[width][height]; + + String line = null; + Tile oneilllocation = null; + Tile jaffalocation = null; + Gate gate = new Gate(); + Gate lastgate = gate; + for (int y = 0; y < height; ++y) + { + line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces + for (int x = 0; x < width; ++x) + { + switch (line.charAt(x)) // set the dungeon up + { + case 'W': + dungeon[y][x] = new Wall(); + break; + + case 'F': + dungeon[y][x] = new Field(); + break; + + case 'Z': + Field zpmfield = new Field(); + zpmfield.setItemOnTile(new ZPM()); + dungeon[y][x] = zpmfield; + break; + + case 'B': + Field boxfield = new Field(); + boxfield.setItemOnTile(new Box()); + dungeon[y][x] = boxfield; + break; + + case 'O': + Field oneillfield = new Field(); + dungeon[y][x] = oneillfield; + oneilllocation = oneillfield; + break; + + case 'J': + Field jaffafield = new Field(); + dungeon[y][x] = jaffafield; + jaffalocation = jaffafield; + break; + + case 'G': + dungeon[y][x] = gate; + lastgate = gate; + gate = new Gate(); + break; + + case 'S': + dungeon[y][x] = new Scale(lastgate, 5); + break; + } + } + } + + // TODO: set up cross references in dungeon matrix + // NOTE: code seems to be correct til this point based on a debugger run-through + + } + System.out.println("<" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon()"); Menu.removeTab(); - return new cicaprojekt.Field(); /*csak hogy ne sírjon*/ } + return new cicaprojekt.Field(); /*csak hogy ne sírjon*/ + } } diff --git a/exampledungeon.dungeon b/exampledungeon.dungeon new file mode 100644 index 0000000..81e7b90 --- /dev/null +++ b/exampledungeon.dungeon @@ -0,0 +1,7 @@ +5x5 + +W W W W W +W O B S W +W W G W W +W F Z F W +W W W W W