diff options
-rw-r--r-- | core/src/ch/asynk/tankontank/game/Map.java | 142 |
1 files changed, 105 insertions, 37 deletions
diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index 363033d..54d1a16 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -1,6 +1,7 @@ package ch.asynk.tankontank.game; import java.util.Vector; +import java.util.HashSet; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureAtlas; @@ -13,15 +14,27 @@ import ch.asynk.tankontank.engine.Pawn; public abstract class Map extends Board { - private boolean roadsOn = false; - private boolean hexOn = false; - private Hex.Terrain t = Hex.Terrain.CLEAR; + enum Action { + NONE, + DRAG, + PATH, + DIRECTION, + ATTACK + }; - private Pawn currentPawn; - private GridPoint2 currentHex = new GridPoint2(-1, -1); + private Action action = Action.NONE; + + private Pawn pawn; + private Pawn touchPawn; + private GridPoint2 hex = new GridPoint2(-1, -1); + private GridPoint2 touchHex = new GridPoint2(-1, -1); + + private GridPoint2 from = new GridPoint2(-1, -1); + private GridPoint2 to = new GridPoint2(-1, -1); private final Vector<GridPoint2> possibleMoves = new Vector<GridPoint2>(20); private final Vector<GridPoint2> possibleTargets = new Vector<GridPoint2>(10); + private final HashSet<GridPoint2> possiblePaths = new HashSet<GridPoint2>(10); protected abstract void setup(); @@ -38,64 +51,119 @@ public abstract class Map extends Board public boolean drag(float dx, float dy) { - if (currentPawn == null) return false; - currentPawn.translate(dx, dy); + if (pawn == null) return false; + pawn.translate(dx, dy); return true; } public void touchDown(float x, float y) { - if (currentHex.x != -1) - enableOverlayOn(currentHex.x, currentHex.y, Hex.BLUE, false); - - getHexAt(currentHex, x, y); - if (currentHex.x != -1) { - enableOverlayOn(currentHex.x, currentHex.y, Hex.BLUE, true); - currentPawn = removeTopPawnFrom(currentHex); - if (currentPawn != null) { - enablePossibleMoves(false); - enablePossibleTargets(false); - pawnsToDraw.add(currentPawn); + if (hex.x != -1) + enableOverlayOn(hex.x, hex.y, Hex.BLUE, false); + + getHexAt(touchHex, x, y); + if (touchHex.x != -1) { + enableOverlayOn(touchHex.x, touchHex.y, Hex.BLUE, true); + touchPawn = removeTopPawnFrom(touchHex); + if (action == Action.DIRECTION) { + System.out.println("DIRECTION"); + enableFinalPath(false); + action = Action.NONE; + } else { + if (touchPawn != null) { + action = Action.DRAG; + enablePossiblePaths(false, false); + enablePossibleMoves(false); + enablePossibleTargets(false); + possiblePaths.clear(); + pawnsToDraw.add(touchPawn); + pawn = touchPawn; + } else if (possibleMoves.contains(touchHex)) { + int paths = 0; + action = Action.PATH; + if (possiblePaths.size() > 0) { + enablePossiblePaths(false, true); + paths = possiblePathsFilterAdd(touchHex.x, touchHex.y, possiblePaths); + enableOverlayOn(touchHex.x, touchHex.y, Hex.DOT, true); + } else { + enablePossibleMoves(false); + from.set(hex.x, hex.y); + to.set(touchHex.x, touchHex.y); + paths = possiblePaths(pawn, from.x, from.y, to.x, to.y, possiblePaths); + enableOverlayOn(touchHex.x, touchHex.y, Hex.DOT, true); + } + if (paths != 1) + enablePossiblePaths(true, true); + else { + enableFinalPath(true); + action = Action.DIRECTION; + } + } } + hex.set(touchHex.x, touchHex.y); + } else { + // touch out of map + hex.set(-1, -1); } } public void touchUp(float x, float y) { - if (currentHex.x != -1) - enableOverlayOn(currentHex.x, currentHex.y, Hex.BLUE, false); - - getHexAt(currentHex, x, y); - if (currentHex.x == -1) { - resetPawnMoves(currentPawn); - } else { - if (currentPawn != null) { - enableOverlayOn(currentHex.x, currentHex.y, Hex.BLUE, true); - pawnsToDraw.remove(currentPawn); - if (currentHex.x != -1) { - movePawnTo(currentPawn, currentHex); - showPossibleActions(currentPawn); - } + if (hex.x != -1) + enableOverlayOn(hex.x, hex.y, Hex.BLUE, false); + + getHexAt(touchHex, x, y); + if (touchHex.x != -1) { + hex.set(touchHex.x, touchHex.y); + if (action == Action.DRAG) { + enableOverlayOn(hex.x, hex.y, Hex.BLUE, true); + pawnsToDraw.remove(pawn); + movePawnTo(pawn, hex); + showPossibleActions(pawn); + action = Action.NONE; } + } else { + // release out of map + resetPawnMoves(pawn); + hex.set(-1, -1); } } - public void showPossibleActions(Pawn pawn) + private void showPossibleActions(Pawn pawn) { - possibleMovesFrom(pawn, currentHex.x, currentHex.y, possibleMoves); + possibleMovesFrom(pawn, hex.x, hex.y, possibleMoves); enablePossibleMoves(true); - possibleTargetsFrom(pawn, currentHex.x, currentHex.y, possibleTargets); + possibleTargetsFrom(pawn, hex.x, hex.y, possibleTargets); enablePossibleTargets(true); } - public void enablePossibleMoves(boolean enable) + private void enablePossibleMoves(boolean enable) { for(GridPoint2 hex : possibleMoves) enableOverlayOn(hex.x, hex.y, Hex.GREEN, enable); } - public void enablePossibleTargets(boolean enable) + private void enableFinalPath(boolean enable) + { + for(GridPoint2 hex : possiblePaths) { + enableOverlayOn(hex.x, hex.y, Hex.GREEN, false); + enableOverlayOn(hex.x, hex.y, Hex.DOT, false); + enableOverlayOn(hex.x, hex.y, Hex.MOVE, enable); + } + enableOverlayOn(to.x, to.y, Hex.ROSE, enable); + } + + private void enablePossiblePaths(boolean enable, boolean keepDots) + { + for(GridPoint2 hex : possiblePaths) { + enableOverlayOn(hex.x, hex.y, Hex.GREEN, enable); + if (!keepDots) + enableOverlayOn(hex.x, hex.y, Hex.DOT, false); + } + } + + private void enablePossibleTargets(boolean enable) { for(GridPoint2 hex : possibleTargets) enableOverlayOn(hex.x, hex.y, Hex.RED, enable); |