Browse Source

implemented vertical movement of tiles

lookup-tables
Stephan Richter 5 years ago
parent
commit
6a1ada0d88
  1. 68
      src/main/java/de/srsoftware/web4rail/Plan.java

68
src/main/java/de/srsoftware/web4rail/Plan.java

@ -47,6 +47,8 @@ public class Plan {
private static final String DIRECTION = "direction"; private static final String DIRECTION = "direction";
private static final String EAST = "east"; private static final String EAST = "east";
private static final String WEST = "west"; private static final String WEST = "west";
private static final String SOUTH = "south";
private static final String NORTH = "north";
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>(); private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
@ -116,32 +118,10 @@ public class Plan {
StringBuffer tiles = new StringBuffer(); StringBuffer tiles = new StringBuffer();
tiles.append(new Tag("div").id("west").title(t("Move west")).content("🢀")); tiles.append(new Tag("div").id("west").title(t("Move west")).content("🢀"));
tiles.append(new Tag("div").id("east").title(t("Move east")).content("🢂")); tiles.append(new Tag("div").id("east").title(t("Move east")).content("🢂"));
tiles.append(new Tag("div").id("north").title(t("Move north")).content("🢁"));
tiles.append(new Tag("div").id("south").title(t("Move south")).content("🢃"));
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu); return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
} }
public Object process(HashMap<String, String> params) {
try {
String action = params.get(ACTION);
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
switch (action) {
case ACTION_ADD:
Tile tile = addTile(params.get(TILE),params.get(X),params.get(Y));
return t("Added {}",tile.getClass().getSimpleName());
case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(X),params.get(Y));
case ACTION_PROPS:
return Tile.propMenu();
case ACTION_SAVE:
return saveTo(params.get(NAME));
default:
LOG.warn("Unknown action: {}",action);
}
return t("Unknown action: {}",action);
} catch (Exception e) {
return e.getMessage();
}
}
private String moveTile(String direction, String x, String y) throws NumberFormatException, IOException { private String moveTile(String direction, String x, String y) throws NumberFormatException, IOException {
return moveTile(direction,Integer.parseInt(x),Integer.parseInt(y)); return moveTile(direction,Integer.parseInt(x),Integer.parseInt(y));
@ -152,10 +132,16 @@ public class Plan {
Vector<Tile> moved = null; Vector<Tile> moved = null;
switch (direction) { switch (direction) {
case EAST: case EAST:
moved = moveHorizontal(x,y,+1); moved = moveTile(x,y,+1,0);
break; break;
case WEST: case WEST:
moved = moveHorizontal(x,y,-1); moved = moveTile(x,y,-1,0);
break;
case NORTH:
moved = moveTile(x,y,0,-1);
break;
case SOUTH:
moved = moveTile(x,y,0,+1);
break; break;
} }
if (!moved.isEmpty()) { if (!moved.isEmpty()) {
@ -167,15 +153,39 @@ public class Plan {
return null; return null;
} }
private Vector<Tile> moveHorizontal(int x, int y,int step) { private Vector<Tile> moveTile(int x, int y,int xstep,int ystep) {
LOG.debug("moveEast({},{})",x,y); LOG.debug("moveEast({},{})",x,y);
Tile tile = this.get(x, y); Tile tile = this.get(x, y);
if (tile == null) return new Vector<Tile>(); if (tile == null) return new Vector<Tile>();
Vector<Tile> result = moveHorizontal(x+step,y,step); Vector<Tile> result = moveTile(x+xstep,y+ystep,xstep,ystep);
set(x+step, y, tile); set(x+xstep, y+ystep, tile);
result.add(tile); result.add(tile);
return result; return result;
} }
public Object process(HashMap<String, String> params) {
try {
String action = params.get(ACTION);
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
switch (action) {
case ACTION_ADD:
Tile tile = addTile(params.get(TILE),params.get(X),params.get(Y));
return t("Added {}",tile.getClass().getSimpleName());
case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(X),params.get(Y));
case ACTION_PROPS:
return Tile.propMenu();
case ACTION_SAVE:
return saveTo(params.get(NAME));
default:
LOG.warn("Unknown action: {}",action);
}
return t("Unknown action: {}",action);
} catch (Exception e) {
return e.getMessage();
}
}
private String saveTo(String name) throws IOException { private String saveTo(String name) throws IOException {
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!"); if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");

Loading…
Cancel
Save