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 {
cicaprojekt.Tile buildDungeon(File input) throws IOException
{
Menu.addTab();
System.out.println(">" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon(" + input + ")");
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)
try(BufferedReader reader = new BufferedReader(new FileReader(input)))
{
line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces
for (int x = 0; x < width; ++x)
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;
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':
dungeon[y][x] = new Wall();
break;
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 'F':
dungeon[y][x] = new Field();
break;
case 'Z':
Field zpmfield = new Field();
zpmfield.setItemOnTile(new ZPM());
dungeon[y][x] = zpmfield;
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 '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 '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 '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 'G':
dungeon[y][x] = gate;
lastgate = gate;
gate = new Gate();
break;
case 'S':
dungeon[y][x] = new Scale(lastgate, 5);
break;
case 'S':
dungeon[y][x] = new Scale(lastgate, 5);
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
// 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*/
System.out.println("<" + "[" + ":" + this.getClass().getSimpleName() + "]" + Menu.tabulator + "Dungeon.buildDungeon()");
Menu.removeTab();
return oneilllocation;
}
}