From cb35ef7704c53e5f269f700c988ddbcb31134b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 5 Oct 2014 11:15:26 +0200 Subject: clean up GameState --- core/src/ch/asynk/tankontank/game/GameCtrl.java | 15 ++-- core/src/ch/asynk/tankontank/game/GameState.java | 4 ++ .../ch/asynk/tankontank/game/GameStateCommon.java | 82 ---------------------- .../asynk/tankontank/game/GameStateDirection.java | 43 ------------ .../ch/asynk/tankontank/game/GameStateNone.java | 41 ----------- .../ch/asynk/tankontank/game/GameStatePath.java | 72 ------------------- .../tankontank/game/states/GameStateCommon.java | 75 ++++++++++++++++++++ .../tankontank/game/states/GameStateDirection.java | 45 ++++++++++++ .../tankontank/game/states/GameStateNone.java | 37 ++++++++++ .../tankontank/game/states/GameStatePath.java | 72 +++++++++++++++++++ 10 files changed, 243 insertions(+), 243 deletions(-) delete mode 100644 core/src/ch/asynk/tankontank/game/GameStateCommon.java delete mode 100644 core/src/ch/asynk/tankontank/game/GameStateDirection.java delete mode 100644 core/src/ch/asynk/tankontank/game/GameStateNone.java delete mode 100644 core/src/ch/asynk/tankontank/game/GameStatePath.java create mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateCommon.java create mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateDirection.java create mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateNone.java create mode 100644 core/src/ch/asynk/tankontank/game/states/GameStatePath.java diff --git a/core/src/ch/asynk/tankontank/game/GameCtrl.java b/core/src/ch/asynk/tankontank/game/GameCtrl.java index cdbced1..577aa02 100644 --- a/core/src/ch/asynk/tankontank/game/GameCtrl.java +++ b/core/src/ch/asynk/tankontank/game/GameCtrl.java @@ -2,11 +2,16 @@ package ch.asynk.tankontank.game; import ch.asynk.tankontank.game.Map; +import ch.asynk.tankontank.game.states.GameStateCommon; +import ch.asynk.tankontank.game.states.GameStateNone; +import ch.asynk.tankontank.game.states.GameStatePath; +import ch.asynk.tankontank.game.states.GameStateDirection; + public class GameCtrl { - private GameState noneState = new GameStateNone(); - private GameState pathState = new GameStatePath(); - private GameState directionState = new GameStateDirection(); + private GameState noneState; + private GameState pathState; + private GameState directionState ; private GameState state; @@ -41,13 +46,13 @@ public class GameCtrl public void touchDown(float x, float y) { - if (GameStateCommon.down(x, y)) + if (state.downInMap(x, y)) state.touchDown(); } public void touchUp(float x, float y) { - if (GameStateCommon.up(x, y)) + if (state.upInMap(x, y)) state.touchUp(); } } diff --git a/core/src/ch/asynk/tankontank/game/GameState.java b/core/src/ch/asynk/tankontank/game/GameState.java index af434ef..a66dba1 100644 --- a/core/src/ch/asynk/tankontank/game/GameState.java +++ b/core/src/ch/asynk/tankontank/game/GameState.java @@ -11,4 +11,8 @@ public interface GameState public void touchDown(); public void touchUp(); + + public boolean downInMap(float x, float y); + + public boolean upInMap(float x, float y); } diff --git a/core/src/ch/asynk/tankontank/game/GameStateCommon.java b/core/src/ch/asynk/tankontank/game/GameStateCommon.java deleted file mode 100644 index 79395eb..0000000 --- a/core/src/ch/asynk/tankontank/game/GameStateCommon.java +++ /dev/null @@ -1,82 +0,0 @@ -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 deleted file mode 100644 index 1fefb3b..0000000 --- a/core/src/ch/asynk/tankontank/game/GameStateDirection.java +++ /dev/null @@ -1,43 +0,0 @@ -package ch.asynk.tankontank.game; - -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/GameStateNone.java b/core/src/ch/asynk/tankontank/game/GameStateNone.java deleted file mode 100644 index 6243091..0000000 --- a/core/src/ch/asynk/tankontank/game/GameStateNone.java +++ /dev/null @@ -1,41 +0,0 @@ -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 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/GameStatePath.java b/core/src/ch/asynk/tankontank/game/GameStatePath.java deleted file mode 100644 index f9a6016..0000000 --- a/core/src/ch/asynk/tankontank/game/GameStatePath.java +++ /dev/null @@ -1,72 +0,0 @@ -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 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; - } -} 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; + } +} -- cgit v1.1-2-g2b99