fixed parts of Dungeon.buildDungeon() fucked up by ericnerdo

This commit is contained in:
Kjistóf 2016-04-24 23:29:10 +02:00
parent 1ab13398bb
commit 82394cdca4

View File

@ -1,12 +1,40 @@
package cicaprojekt;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Dungeon {
cicaprojekt.Tile buildDungeon(File input) throws IOException {
/* NOTE: this function assumes that the parameter input is a well-formatted dungeon file.
* Such file looks like as follows:
*
* <map width>x<map height>
* <empty line>
* <map matrix line 1>
* ...
* ...
* <map matrix line <map height>>
* <empty line>
* <scale y>-<scale x>-<gate y>-<gate x>-<scale trigger weight>
* ...
* ...
* <scale y>-<scale x>-<gate y>-<gate x>-<scale trigger weight>
*
* where the map matrix is a matrix of the following chars:
* W: Wall
* F: Field
* Z: Field with a ZMP
* B: Field with a Box
* O: Field with ONeill
* J: Field with Jaffa
* G: Gate
* S: Scale */
Map<String, Tile> buildDungeon(File input) throws IOException
{
Tile oneilllocation = null;
Tile jaffalocation = null;
try (BufferedReader reader = new BufferedReader(new FileReader(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]);
@ -15,11 +43,14 @@ public class Dungeon {
Tile[][] dungeon = new Tile[width][height];
String line = 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) {
Gate tempgate = new Gate();
Scale tempscale = new Scale(tempgate, Integer.MAX_VALUE);
int scalecount = 0;
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':
@ -55,45 +86,60 @@ public class Dungeon {
break;
case 'G':
dungeon[y][x] = gate;
lastgate = gate;
gate = new Gate();
dungeon[y][x] = tempgate;
break;
case 'S':
dungeon[y][x] = new Scale(lastgate, 5);
dungeon[y][x] = tempscale;
scalecount++;
break;
}
}
}
// NOTE: code seems to be correct till this point based on a debugger run-through
reader.readLine(); // throw empty line away
for (int i = 0; i < scalecount; ++i) // set up scale-gate connections
{
String[] scaledata = reader.readLine().split("-");
dungeon[Integer.parseInt(scaledata[0])][Integer.parseInt(scaledata[1])] =
new Scale((Gate)dungeon[Integer.parseInt(scaledata[2])][Integer.parseInt(scaledata[3])],
Integer.parseInt(scaledata[4]));
}
/* 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);
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);
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);
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);
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);
}
}
}
return oneilllocation;
Map<String, Tile> playermap = new HashMap<>();
playermap.put("oneill", oneilllocation);
playermap.put("jaffa", jaffalocation);
return playermap;
}
}
}