summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/src/ch/asynk/tankontank/game/GameCtrl.java58
-rw-r--r--core/src/ch/asynk/tankontank/game/GameState.java18
-rw-r--r--core/src/ch/asynk/tankontank/game/GameStateCommon.java82
-rw-r--r--core/src/ch/asynk/tankontank/game/GameStateDirection.java29
-rw-r--r--core/src/ch/asynk/tankontank/game/GameStateNone.java55
-rw-r--r--core/src/ch/asynk/tankontank/game/GameStatePath.java86
6 files changed, 328 insertions, 0 deletions
diff --git a/core/src/ch/asynk/tankontank/game/GameCtrl.java b/core/src/ch/asynk/tankontank/game/GameCtrl.java
new file mode 100644
index 0000000..356e4eb
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameCtrl.java
@@ -0,0 +1,58 @@
+package ch.asynk.tankontank.game;
+
+import ch.asynk.tankontank.game.Map;
+
+public class GameCtrl
+{
+ private GameState noneState = new GameStateNone();
+ private GameState pathState = new GameStatePath();
+ private GameState directionState = new GameStateDirection();
+
+ private GameState state;
+
+ public GameCtrl(Map map)
+ {
+ this.noneState = new GameStateNone(this, map);
+ this.pathState = new GameStatePath();
+ this.directionState = new GameStateDirection();
+
+ this.state = noneState;
+ }
+
+ public void setState(GameState.State state, boolean forward)
+ {
+ switch(state) {
+ case NONE:
+ this.state = noneState;
+ break;
+ case PATH:
+ this.state = pathState;
+ break;
+ case DIRECTION:
+ this.state = directionState;
+ break;
+ default:
+ break;
+ }
+
+ if (forward)
+ this.state.touchDown();
+ }
+
+ public void touchDown(float x, float y)
+ {
+ if (GameStateCommon.down(x, y))
+ state.touchDown();
+ }
+
+ public void touchUp(float x, float y)
+ {
+ if (GameStateCommon.up(x, y))
+ state.touchUp();
+ }
+
+ public boolean drag(float dx, float dy)
+ {
+ return state.drag(dx, dy);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/game/GameState.java b/core/src/ch/asynk/tankontank/game/GameState.java
new file mode 100644
index 0000000..b53195a
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameState.java
@@ -0,0 +1,18 @@
+package ch.asynk.tankontank.game;
+
+public interface GameState
+{
+ enum State {
+ NONE,
+ PATH,
+ DIRECTION
+ };
+
+ public void abort();
+
+ public void touchDown();
+
+ public void touchUp();
+
+ public boolean drag(float dx, float dy);
+}
diff --git a/core/src/ch/asynk/tankontank/game/GameStateCommon.java b/core/src/ch/asynk/tankontank/game/GameStateCommon.java
new file mode 100644
index 0000000..79395eb
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameStateCommon.java
@@ -0,0 +1,82 @@
+package ch.asynk.tankontank.game;
+
+import com.badlogic.gdx.math.GridPoint2;
+
+import ch.asynk.tankontank.engine.Pawn;
+import ch.asynk.tankontank.engine.Tile;
+
+public abstract class GameStateCommon implements GameState
+{
+ protected static GameCtrl ctrl;
+ protected static Map map;
+ protected static Pawn pawn;
+ protected static Tile tile;
+ protected static GridPoint2 hex = new GridPoint2(0, 0);
+
+ protected static GridPoint2 downHex = new GridPoint2(-1, -1);
+ protected static GridPoint2 upHex = new GridPoint2(-1, -1);
+
+ protected GameStateCommon()
+ {
+ }
+
+ public GameStateCommon(GameCtrl ctrl, Map map)
+ {
+ this.ctrl = ctrl;
+ this.map = map;
+ }
+
+ // downHex
+
+ protected static boolean downHexInMap()
+ {
+ if (downHex.x == -1) return false;
+ return !map.isOffMap(downHex);
+ }
+
+ protected static boolean down(float x, float y)
+ {
+ map.getHexAt(downHex, x, y);
+ return downHexInMap();
+ }
+
+ protected static boolean up(float x, float y)
+ {
+ map.getHexAt(upHex, x, y);
+ return downHexInMap();
+ }
+
+ // pawn
+
+ protected void setPawn()
+ {
+ pawn = map.getTopPawnAt(hex);
+ }
+
+ // hex
+
+ protected void setHex()
+ {
+ hex.set(downHex.x, downHex.y);
+ }
+
+ protected boolean hexHasUnit()
+ {
+ return map.hasUnits(hex);
+ }
+
+ protected void unselectHex()
+ {
+ map.enableOverlayOn(hex, Hex.BLUE, false);
+ }
+
+ protected void selectHex()
+ {
+ map.enableOverlayOn(hex, Hex.BLUE, true);
+ }
+
+ // protected Hex getHex(int col, int row)
+ // {
+ // return (Hex) map.getTile(col, row);
+ // }
+}
diff --git a/core/src/ch/asynk/tankontank/game/GameStateDirection.java b/core/src/ch/asynk/tankontank/game/GameStateDirection.java
new file mode 100644
index 0000000..733ccab
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameStateDirection.java
@@ -0,0 +1,29 @@
+package ch.asynk.tankontank.game;
+
+public class GameStateDirection implements GameState
+{
+ @Override
+ public boolean drag(float dx, float dy)
+ {
+ return false;
+ }
+
+ @Override
+ public void touchDown()
+ {
+ System.out.println("GameStateDirection: touchDown()");
+ // enableOverlayOn(to.x, to.y, Hex.ROSE, enable);
+ }
+
+ @Override
+ public void touchUp()
+ {
+ System.out.println("GameStateDirection: touchUp()");
+ }
+
+ @Override
+ public void abort()
+ {
+ System.err.println("GameStateDirection: abort");
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/game/GameStateNone.java b/core/src/ch/asynk/tankontank/game/GameStateNone.java
new file mode 100644
index 0000000..61a9d21
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameStateNone.java
@@ -0,0 +1,55 @@
+package ch.asynk.tankontank.game;
+
+import ch.asynk.tankontank.game.Map;
+
+public class GameStateNone extends GameStateCommon
+{
+ public GameStateNone()
+ {
+ super();
+ }
+
+ public GameStateNone(GameCtrl ctrl, Map map)
+ {
+ super(ctrl, map);
+ }
+
+ @Override
+ public boolean drag(float dx, float dy)
+ {
+ return false;
+ }
+
+ @Override
+ public void touchDown()
+ {
+ System.out.println("GameStateNone: touchDown()");
+ if (map.isInPossibleMoves(downHex)) {
+ ctrl.setState(State.PATH, true);
+ } else {
+ unselectHex();
+ setHex();
+ selectHex();
+ if (hexHasUnit()) {
+ setPawn();
+ map.showPossibleActions(pawn, hex, true);
+ } else {
+ map.showPossibleActions(pawn, hex, false);
+ }
+ }
+ }
+
+ @Override
+ public void touchUp()
+ {
+ System.out.println("GameStateNone: touchUp()");
+ if (!hexHasUnit())
+ unselectHex();
+ }
+
+ @Override
+ public void abort()
+ {
+ System.err.println("GameStateNone: abort");
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/game/GameStatePath.java b/core/src/ch/asynk/tankontank/game/GameStatePath.java
new file mode 100644
index 0000000..d7d5b4c
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/game/GameStatePath.java
@@ -0,0 +1,86 @@
+package ch.asynk.tankontank.game;
+
+import com.badlogic.gdx.math.GridPoint2;
+
+public class GameStatePath extends GameStateCommon
+{
+ private GridPoint2 from = new GridPoint2(-1, -1);
+ private GridPoint2 to = new GridPoint2(-1, -1);
+
+ @Override
+ public boolean drag(float dx, float dy)
+ {
+ return false;
+ }
+
+ @Override
+ public void touchDown()
+ {
+ System.out.println("GameStatePath: touchDown()");
+ int s = map.possiblePathsSize();
+ if (s == 0) {
+ s = buildPaths();
+ } else {
+ if (map.isInPossiblePaths(downHex)) {
+ s = togglePoint();
+ } else {
+ s = reset(false);
+ }
+ }
+
+ if (s == 1) {
+ map.enableFinalPath(true);
+ ctrl.setState(State.DIRECTION, true);
+ }
+ }
+
+ @Override
+ public void touchUp()
+ {
+ System.out.println("GameStatePath: touchUp()");
+ }
+
+ @Override
+ public void abort()
+ {
+ System.err.println("GameStatePath: abort");
+ }
+
+ private int buildPaths()
+ {
+ from.set(hex.x, hex.y);
+ to.set(downHex.x, downHex.y);
+ int s = map.buildPossiblePaths(pawn, from, to);
+ map.enablePossibleMoves(false);
+ map.toggleDotOverlay(downHex);
+ map.enablePossiblePaths(true, true);
+ return s;
+ }
+
+ private int togglePoint()
+ {
+ int s = 0;
+ if ((downHex.x == from.x) && (downHex.y == from.y)) {
+ s = map.possiblePathsSize();
+ } else if ((downHex.x == to.x) && (downHex.y == to.y)) {
+ s = reset(true);
+ } else {
+ map.enablePossiblePaths(false, true);
+ map.toggleDotOverlay(downHex);
+ s = map.possiblePathsPointToggle(downHex);
+ map.enablePossiblePaths(true, true);
+ }
+ return s;
+ }
+
+ private int reset(boolean showMoves)
+ {
+ to.set(-1, -1);
+ from.set(-1, -1);
+ map.clearPossibles();
+ if (showMoves)
+ map.enablePossibleMoves(true);
+ ctrl.setState(State.NONE, false);
+ return -1;
+ }
+}