finished implementing map parser (Dungeon.buildDungeon())

the parser now sets up references between adjecent Tiles, however a team
consultation is needed regarding the Gates/Scales on the map. note that
the part of the parser that sets the references up is still untested.
This commit is contained in:
Kjistóf 2016-04-23 17:08:32 +02:00
parent e1d24d05dd
commit 7eeeefa3e2

View File

@ -5,82 +5,107 @@ import java.io.*;
public class Dungeon { public class Dungeon {
cicaprojekt.Tile buildDungeon(File input) throws IOException 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 oneilllocation = null;
Tile jaffalocation = null; Tile jaffalocation = null;
Gate gate = new Gate(); try(BufferedReader reader = new BufferedReader(new FileReader(input)))
Gate lastgate = gate;
for (int y = 0; y < height; ++y)
{ {
line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces String[] sizedata = reader.readLine().split("x"); // read size data at beginning of file
for (int x = 0; x < width; ++x) 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;
Gate gate = new Gate();
Gate lastgate = gate;
for (int y = 0; y < height; ++y)
{ {
switch (line.charAt(x)) // set the dungeon up line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces
for (int x = 0; x < width; ++x)
{ {
case 'W': switch (line.charAt(x)) // set the dungeon up
dungeon[y][x] = new Wall(); {
break; case 'W':
dungeon[y][x] = new Wall();
break;
case 'F': case 'F':
dungeon[y][x] = new Field(); dungeon[y][x] = new Field();
break; break;
case 'Z': case 'Z':
Field zpmfield = new Field(); Field zpmfield = new Field();
zpmfield.setItemOnTile(new ZPM()); zpmfield.setItemOnTile(new ZPM());
dungeon[y][x] = zpmfield; dungeon[y][x] = zpmfield;
break; break;
case 'B': case 'B':
Field boxfield = new Field(); Field boxfield = new Field();
boxfield.setItemOnTile(new Box()); boxfield.setItemOnTile(new Box());
dungeon[y][x] = boxfield; dungeon[y][x] = boxfield;
break; break;
case 'O': case 'O':
Field oneillfield = new Field(); Field oneillfield = new Field();
dungeon[y][x] = oneillfield; dungeon[y][x] = oneillfield;
oneilllocation = oneillfield; oneilllocation = oneillfield;
break; break;
case 'J': case 'J':
Field jaffafield = new Field(); Field jaffafield = new Field();
dungeon[y][x] = jaffafield; dungeon[y][x] = jaffafield;
jaffalocation = jaffafield; jaffalocation = jaffafield;
break; break;
case 'G': case 'G':
dungeon[y][x] = gate; dungeon[y][x] = gate;
lastgate = gate; lastgate = gate;
gate = new Gate(); gate = new Gate();
break; break;
case 'S': case 'S':
dungeon[y][x] = new Scale(lastgate, 5); dungeon[y][x] = new Scale(lastgate, 5);
break; break;
}
}
}
// NOTE: code seems to be correct till this point based on a debugger run-through
/* setting up Tile cross references */
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
if (x-1 >= 0) // leftwards Tile reference
dungeon[y][x].setAdajacentTile(dungeon[y][x-1], Direction.WEST);
else
dungeon[y][x].setAdajacentTile(null, Direction.WEST);
if (x+1 < width) // rightwards Tile reference
dungeon[y][x].setAdajacentTile(dungeon[y][x+1], Direction.EAST);
else
dungeon[y][x].setAdajacentTile(null, Direction.EAST);
if (y+1 < height) // upwards Tile reference
dungeon[y][x].setAdajacentTile(dungeon[y+1][x], Direction.NORTH);
else
dungeon[y][x].setAdajacentTile(null, Direction.NORTH);
if (y-1 >= 0) // downwards Tile reference
dungeon[y][x].setAdajacentTile(dungeon[y-1][x], Direction.SOUTH);
else
dungeon[y][x].setAdajacentTile(null, Direction.SOUTH);
} }
} }
} }
// TODO: set up cross references in dungeon matrix System.out.println("<" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon()");
// NOTE: code seems to be correct til this point based on a debugger run-through Menu.removeTab();
return oneilllocation;
}
System.out.println("<" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon()");
Menu.removeTab();
return new cicaprojekt.Field(); /*csak hogy ne sírjon*/
} }
} }