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; package cicaprojekt;
import java.io.*; import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Dungeon { public class Dungeon {
/* NOTE: this function assumes that the parameter input is a well-formatted dungeon file. cicaprojekt.Tile buildDungeon(File input) throws IOException
* 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 oneilllocation = null;
Tile jaffalocation = null; Tile jaffalocation = null;
@ -43,9 +17,8 @@ public class Dungeon {
Tile[][] dungeon = new Tile[width][height]; Tile[][] dungeon = new Tile[width][height];
String line = null; String line = null;
Gate tempgate = new Gate(); Gate gate = new Gate();
Scale tempscale = new Scale(tempgate, Integer.MAX_VALUE); Gate lastgate = gate;
int scalecount = 0;
for (int y = 0; y < height; ++y) for (int y = 0; y < height; ++y)
{ {
line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces
@ -63,13 +36,13 @@ public class Dungeon {
case 'Z': case 'Z':
Field zpmfield = new Field(); Field zpmfield = new Field();
zpmfield.setItemOnTile(new ZPM()); zpmfield.setZPMOnTile(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.putABox(new Box());
dungeon[y][x] = boxfield; dungeon[y][x] = boxfield;
break; break;
@ -86,27 +59,19 @@ public class Dungeon {
break; break;
case 'G': case 'G':
dungeon[y][x] = tempgate; dungeon[y][x] = gate;
lastgate = gate;
gate = new Gate();
break; break;
case 'S': case 'S':
dungeon[y][x] = tempscale; dungeon[y][x] = new Scale(lastgate, 5);
scalecount++;
break; break;
} }
} }
} }
reader.readLine(); // throw empty line away // NOTE: code seems to be correct till this point based on a debugger run-through
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 */ /* setting up Tile cross references */
for (int y = 0; y < height; ++y) for (int y = 0; y < height; ++y)
@ -135,11 +100,6 @@ public class Dungeon {
} }
} }
} }
return oneilllocation;
Map<String, Tile> playermap = new HashMap<>();
playermap.put("oneill", oneilllocation);
playermap.put("jaffa", jaffalocation);
return playermap;
} }
} }

View File

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

View File

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