diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-10-05 11:15:26 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-10-05 11:15:26 +0200 |
commit | cb35ef7704c53e5f269f700c988ddbcb31134b30 (patch) | |
tree | e240925831c907a1cdd0c53f35cea22a44bafa6a /core/src/ch/asynk/tankontank/game/states | |
parent | 633676e3ca84af32fbdfceb20309e53421ca01e1 (diff) | |
download | RustAndDust-cb35ef7704c53e5f269f700c988ddbcb31134b30.zip RustAndDust-cb35ef7704c53e5f269f700c988ddbcb31134b30.tar.gz |
clean up GameState
Diffstat (limited to 'core/src/ch/asynk/tankontank/game/states')
4 files changed, 229 insertions, 0 deletions
diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java new file mode 100644 index 0000000..d02d345 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java @@ -0,0 +1,75 @@ +package ch.asynk.tankontank.game.states; + +import com.badlogic.gdx.math.GridPoint2; + +import ch.asynk.tankontank.engine.Pawn; +import ch.asynk.tankontank.engine.Tile; +import ch.asynk.tankontank.game.Map; +import ch.asynk.tankontank.game.Hex; +import ch.asynk.tankontank.game.GameCtrl; +import ch.asynk.tankontank.game.GameState; + +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; + } + + protected static boolean hexInMap(GridPoint2 hex) + { + if (hex.x == -1) return false; + return !map.isOffMap(hex); + } + + public boolean downInMap(float x, float y) + { + map.getHexAt(downHex, x, y); + return hexInMap(downHex); + } + + public boolean upInMap(float x, float y) + { + map.getHexAt(upHex, x, y); + return hexInMap(upHex); + } + + protected void setPawn() + { + pawn = map.getTopPawnAt(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); + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateDirection.java b/core/src/ch/asynk/tankontank/game/states/GameStateDirection.java new file mode 100644 index 0000000..39f3743 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/GameStateDirection.java @@ -0,0 +1,45 @@ +package ch.asynk.tankontank.game.states; + +import ch.asynk.tankontank.engine.Orientation; + +public class GameStateDirection extends GameStateCommon +{ + @Override + public void touchDown() + { + } + + @Override + public void touchUp() + { + Orientation o = Orientation.KEEP;; + + if (downHex.y == hex.y) { + if (downHex.x == (hex.x - 1)) { + o = Orientation.SOUTH; + } else if (downHex.x == (hex.x + 1)) { + o = Orientation.NORTH; + } + } else if (downHex.y == (hex.y - 1)) { + if (downHex.x == (hex.x - 1)) { + o = Orientation.SOUTH_EAST; + } else if (downHex.x == hex.x) { + o = Orientation.NORTH_EAST; + } + + } else if (downHex.y == (hex.y + 1)) { + if (downHex.x == hex.x) { + o = Orientation.SOUTH_WEST; + } else if (downHex.x == (hex.x + 1)) { + o = Orientation.NORTH_WEST; + } + } + + if (o != Orientation.KEEP) + map.movePawn(pawn, o); + + map.enableFinalPath(hex, false); + map.resetPaths(); + ctrl.setState(State.NONE, false); + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateNone.java b/core/src/ch/asynk/tankontank/game/states/GameStateNone.java new file mode 100644 index 0000000..bec92ee --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/GameStateNone.java @@ -0,0 +1,37 @@ +package ch.asynk.tankontank.game.states; + +import ch.asynk.tankontank.game.Map; +import ch.asynk.tankontank.game.GameCtrl; + +public class GameStateNone extends GameStateCommon +{ + public GameStateNone(GameCtrl ctrl, Map map) + { + super(ctrl, map); + } + + @Override + public void touchDown() + { + if (map.isInPossibleMoves(downHex)) { + map.enablePossibleTargets(false); + ctrl.setState(State.PATH, true); + } else { + unselectHex(); + setHex(); + selectHex(); + } + } + + @Override + public void touchUp() + { + if (hexHasUnit()) { + setPawn(); + map.showPossibleActions(pawn, hex, true); + } else { + map.showPossibleActions(pawn, hex, false); + unselectHex(); + } + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStatePath.java b/core/src/ch/asynk/tankontank/game/states/GameStatePath.java new file mode 100644 index 0000000..9ffd111 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/GameStatePath.java @@ -0,0 +1,72 @@ +package ch.asynk.tankontank.game.states; + +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 void touchDown() + { + } + + @Override + public void touchUp() + { + int s = map.possiblePathsSize(); + if (s == 0) { + s = buildPaths(); + } else { + if (map.isInPossiblePaths(downHex)) + s = togglePoint(); + else + s = reset(); + } + + if (s == 1) { + unselectHex(); + hex.set(to.x, to.y); + map.enableFinalPath(to, true); + ctrl.setState(State.DIRECTION, false); + } + } + + 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(); + } else { + map.enablePossiblePaths(false, true); + map.toggleDotOverlay(downHex); + s = map.possiblePathsPointToggle(downHex); + map.enablePossiblePaths(true, true); + } + return s; + } + + private int reset() + { + to.set(-1, -1); + from.set(-1, -1); + map.hidePaths(); + map.resetPaths(); + ctrl.setState(State.NONE, false); + return -1; + } +} |