From 6a1ada0d889f4ae39c73f2105d2a7cdcd056a4c1 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sun, 13 Sep 2020 20:25:03 +0200 Subject: [PATCH] implemented vertical movement of tiles --- .../java/de/srsoftware/web4rail/Plan.java | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/srsoftware/web4rail/Plan.java b/src/main/java/de/srsoftware/web4rail/Plan.java index c70736a..1f1bb9d 100644 --- a/src/main/java/de/srsoftware/web4rail/Plan.java +++ b/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 EAST = "east"; private static final String WEST = "west"; + private static final String SOUTH = "south"; + private static final String NORTH = "north"; private HashMap> tiles = new HashMap>(); @@ -116,32 +118,10 @@ public class Plan { StringBuffer tiles = new StringBuffer(); 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("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); } - - public Object process(HashMap 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 { return moveTile(direction,Integer.parseInt(x),Integer.parseInt(y)); @@ -152,10 +132,16 @@ public class Plan { Vector moved = null; switch (direction) { case EAST: - moved = moveHorizontal(x,y,+1); + moved = moveTile(x,y,+1,0); break; 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; } if (!moved.isEmpty()) { @@ -167,15 +153,39 @@ public class Plan { return null; } - private Vector moveHorizontal(int x, int y,int step) { + private Vector moveTile(int x, int y,int xstep,int ystep) { LOG.debug("moveEast({},{})",x,y); Tile tile = this.get(x, y); if (tile == null) return new Vector(); - Vector result = moveHorizontal(x+step,y,step); - set(x+step, y, tile); + Vector result = moveTile(x+xstep,y+ystep,xstep,ystep); + set(x+xstep, y+ystep, tile); result.add(tile); return result; } + + public Object process(HashMap 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 { if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");