Tile refactored

Every setItemOnTile call were replaced.
This commit is contained in:
ericnerdo 2016-04-24 22:25:20 +02:00
parent 0a4b00aad3
commit 924a5acc26
3 changed files with 21 additions and 59 deletions

View File

@ -1,35 +1,9 @@
package cicaprojekt;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Dungeon {
/* 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
cicaprojekt.Tile buildDungeon(File input) throws IOException
{
Tile oneilllocation = null;
Tile jaffalocation = null;
@ -43,9 +17,8 @@ public class Dungeon {
Tile[][] dungeon = new Tile[width][height];
String line = null;
Gate tempgate = new Gate();
Scale tempscale = new Scale(tempgate, Integer.MAX_VALUE);
int scalecount = 0;
Gate gate = new Gate();
Gate lastgate = gate;
for (int y = 0; y < height; ++y)
{
line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces
@ -63,13 +36,13 @@ public class Dungeon {
case 'Z':
Field zpmfield = new Field();
zpmfield.setItemOnTile(new ZPM());
zpmfield.setZPMOnTile(new ZPM());
dungeon[y][x] = zpmfield;
break;
case 'B':
Field boxfield = new Field();
boxfield.setItemOnTile(new Box());
boxfield.putABox(new Box());
dungeon[y][x] = boxfield;
break;
@ -86,27 +59,19 @@ public class Dungeon {
break;
case 'G':
dungeon[y][x] = tempgate;
dungeon[y][x] = gate;
lastgate = gate;
gate = new Gate();
break;
case 'S':
dungeon[y][x] = tempscale;
scalecount++;
dungeon[y][x] = new Scale(lastgate, 5);
break;
}
}
}
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]));
}
// 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)
@ -135,11 +100,6 @@ public class Dungeon {
}
}
}
Map<String, Tile> playermap = new HashMap<>();
playermap.put("oneill", oneilllocation);
playermap.put("jaffa", jaffalocation);
return playermap;
return oneilllocation;
}
}

View File

@ -13,11 +13,11 @@ public class Player extends PlayerBase{
}
public void boxLift() {
boxLifted = (Box) currentTile.getAdjacentTile(facingDirection).removeItemFromTile();
boxLifted = (Box) currentTile.getAdjacentTile(facingDirection).getABox();
}
public void boxDrop() {
currentTile.getAdjacentTile(facingDirection).setItemOnTile(boxLifted);
currentTile.getAdjacentTile(facingDirection).putABox(boxLifted);
boxLifted = null;
}

View File

@ -12,12 +12,6 @@ public abstract class Tile {
public Tile(){
adjacentTile = new HashMap<Direction, Tile>();
adjacentTile.put(Direction.NORTH, Field.testField);
adjacentTile.put(Direction.EAST, Field.testField);
adjacentTile.put(Direction.SOUTH, Field.testField);
adjacentTile.put(Direction.WEST, Field.testField);
zpmOnTile = new ZPM();
}
public Tile getAdjacentTile(Direction direction) {
@ -34,6 +28,10 @@ public abstract class Tile {
public abstract void onExit(PlayerBase playerBase);
public void setZPMOnTile(ZPM zpm) {
zpmOnTile = zpm;
}
public ZPM getZPMFromTile() {
ZPM zpm = zpmOnTile;
zpmOnTile = null;
@ -41,10 +39,14 @@ public abstract class Tile {
}
public void putABox(Box box) {
if(box == null)
return;
boxStack.push(box);
}
public Box getABox(){
if(boxStack.isEmpty())
return null;
return boxStack.pop();
}
}