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.
This commit is contained in:
Kjistóf 2016-04-23 15:07:48 +02:00
parent 98d888b696
commit e1d24d05dd
2 changed files with 84 additions and 3 deletions

View File

@ -1,12 +1,86 @@
package cicaprojekt; package cicaprojekt;
import java.io.File; import java.io.*;
public class Dungeon { public class Dungeon {
cicaprojekt.Tile buildDungeon(File input){ cicaprojekt.Tile buildDungeon(File input) throws IOException
{
Menu.addTab(); Menu.addTab();
System.out.println(">" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon(" + input + ")"); 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()"); System.out.println("<" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon()");
Menu.removeTab(); Menu.removeTab();
return new cicaprojekt.Field(); /*csak hogy ne sírjon*/ } return new cicaprojekt.Field(); /*csak hogy ne sírjon*/
}
} }

7
exampledungeon.dungeon Normal file
View File

@ -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