From 3afc38971c4d7cbbea48128c930f00d5734a529f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 12 Oct 2014 11:55:52 +0200 Subject: GameState* -> State --- core/src/ch/asynk/tankontank/game/Ctrl.java | 32 ++--- core/src/ch/asynk/tankontank/game/GameState.java | 31 ----- core/src/ch/asynk/tankontank/game/Hud.java | 6 +- core/src/ch/asynk/tankontank/game/State.java | 31 +++++ .../tankontank/game/states/GameStateAnimation.java | 41 ------ .../tankontank/game/states/GameStateCommon.java | 147 -------------------- .../tankontank/game/states/GameStateMove.java | 147 -------------------- .../tankontank/game/states/GameStateRotate.java | 114 ---------------- .../tankontank/game/states/GameStateSelect.java | 73 ---------- .../tankontank/game/states/StateAnimation.java | 41 ++++++ .../asynk/tankontank/game/states/StateCommon.java | 148 +++++++++++++++++++++ .../ch/asynk/tankontank/game/states/StateMove.java | 147 ++++++++++++++++++++ .../asynk/tankontank/game/states/StateRotate.java | 114 ++++++++++++++++ .../asynk/tankontank/game/states/StateSelect.java | 73 ++++++++++ 14 files changed, 573 insertions(+), 572 deletions(-) delete mode 100644 core/src/ch/asynk/tankontank/game/GameState.java create mode 100644 core/src/ch/asynk/tankontank/game/State.java delete mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java delete mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateCommon.java delete mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateMove.java delete mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateRotate.java delete mode 100644 core/src/ch/asynk/tankontank/game/states/GameStateSelect.java create mode 100644 core/src/ch/asynk/tankontank/game/states/StateAnimation.java create mode 100644 core/src/ch/asynk/tankontank/game/states/StateCommon.java create mode 100644 core/src/ch/asynk/tankontank/game/states/StateMove.java create mode 100644 core/src/ch/asynk/tankontank/game/states/StateRotate.java create mode 100644 core/src/ch/asynk/tankontank/game/states/StateSelect.java diff --git a/core/src/ch/asynk/tankontank/game/Ctrl.java b/core/src/ch/asynk/tankontank/game/Ctrl.java index b2920c2..b6d5966 100644 --- a/core/src/ch/asynk/tankontank/game/Ctrl.java +++ b/core/src/ch/asynk/tankontank/game/Ctrl.java @@ -3,11 +3,11 @@ package ch.asynk.tankontank.game; import com.badlogic.gdx.utils.Disposable; import ch.asynk.tankontank.TankOnTank; -import ch.asynk.tankontank.game.states.GameStateCommon; -import ch.asynk.tankontank.game.states.GameStateSelect; -import ch.asynk.tankontank.game.states.GameStateMove; -import ch.asynk.tankontank.game.states.GameStateRotate; -import ch.asynk.tankontank.game.states.GameStateAnimation; +import ch.asynk.tankontank.game.states.StateCommon; +import ch.asynk.tankontank.game.states.StateSelect; +import ch.asynk.tankontank.game.states.StateMove; +import ch.asynk.tankontank.game.states.StateRotate; +import ch.asynk.tankontank.game.states.StateAnimation; public class Ctrl implements Disposable { @@ -42,14 +42,14 @@ public class Ctrl implements Disposable public Player usPlayer; public Player currentPlayer; - private GameState selectState; - private GameState pathState; - private GameState rotateState; - private GameState animationState; + private State selectState; + private State pathState; + private State rotateState; + private State animationState; private int animationCount = 0; - private GameState state; + private State state; public Ctrl(final TankOnTank game) { @@ -62,10 +62,10 @@ public class Ctrl implements Disposable this.usPlayer = factory.getPlayer(Army.US); this.gePlayer = factory.getPlayer(Army.GE); - this.selectState = new GameStateSelect(this, map); - this.pathState = new GameStateMove(); - this.rotateState = new GameStateRotate(); - this.animationState = new GameStateAnimation(); + this.selectState = new StateSelect(this, map); + this.pathState = new StateMove(); + this.rotateState = new StateRotate(); + this.animationState = new StateAnimation(); this.state = selectState; this.currentPlayer = factory.fakeSetup(map, gePlayer, usPlayer); @@ -115,12 +115,12 @@ public class Ctrl implements Disposable } - public void setState(GameState.State state) + public void setState(State.StateType state) { setState(state, true); } - public void setState(GameState.State state, boolean normal) + public void setState(State.StateType state, boolean normal) { this.state.leave(state); diff --git a/core/src/ch/asynk/tankontank/game/GameState.java b/core/src/ch/asynk/tankontank/game/GameState.java deleted file mode 100644 index e6741a7..0000000 --- a/core/src/ch/asynk/tankontank/game/GameState.java +++ /dev/null @@ -1,31 +0,0 @@ -package ch.asynk.tankontank.game; - -public interface GameState -{ - enum State { - SELECT, - MOVE, - ROTATE, - ANIMATION - }; - - public void enter(boolean flag); - - public void leave(GameState.State nextState); - - public void abort(); - - public void done(); - - public void touchDown(); - - public void touchUp(); - - public boolean downInMap(float x, float y); - - public boolean upInMap(float x, float y); - - public GameState.State getNextState(); - - public void setNextState(GameState.State next); -} diff --git a/core/src/ch/asynk/tankontank/game/Hud.java b/core/src/ch/asynk/tankontank/game/Hud.java index 3d5014b..cc9dadc 100644 --- a/core/src/ch/asynk/tankontank/game/Hud.java +++ b/core/src/ch/asynk/tankontank/game/Hud.java @@ -162,11 +162,11 @@ public class Hud implements Disposable if (!buttonsRect.contains(x,y)) return false; if (btn == moveBtn) - ctrl.setState(GameState.State.MOVE); + ctrl.setState(State.StateType.MOVE); else if (btn == rotateBtn) - ctrl.setState(GameState.State.ROTATE); + ctrl.setState(State.StateType.ROTATE); else if (btn == attackBtn) - // TODO ctrl.setState(GameState.State.ATTACK); + // TODO ctrl.setState(StateType.ATTACK); System.err.println(" ATTACK not implemented yet"); else if (btn == checkBtn) ctrl.done(); diff --git a/core/src/ch/asynk/tankontank/game/State.java b/core/src/ch/asynk/tankontank/game/State.java new file mode 100644 index 0000000..a289edc --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/State.java @@ -0,0 +1,31 @@ +package ch.asynk.tankontank.game; + +public interface State +{ + enum StateType { + SELECT, + MOVE, + ROTATE, + ANIMATION + }; + + public void enter(boolean flag); + + public void leave(StateType nextState); + + public void abort(); + + public void done(); + + public void touchDown(); + + public void touchUp(); + + public boolean downInMap(float x, float y); + + public boolean upInMap(float x, float y); + + public StateType getNextState(); + + public void setNextState(StateType next); +} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java b/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java deleted file mode 100644 index e1b2c08..0000000 --- a/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java +++ /dev/null @@ -1,41 +0,0 @@ -package ch.asynk.tankontank.game.states; - -import ch.asynk.tankontank.game.GameState.State; - -public class GameStateAnimation extends GameStateCommon -{ - @Override - public void enter(boolean flag) - { - } - - @Override - public void leave(State nextState) - { - if (nextState != State.SELECT) { - from.set(-1, -1); - to.set(-1, -1); - } - } - - @Override - public void touchDown() - { - } - - @Override - public void touchUp() - { - } - - @Override - public void abort() - { - } - - @Override - public void done() - { - super.done(); - } -} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java deleted file mode 100644 index a759dc2..0000000 --- a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java +++ /dev/null @@ -1,147 +0,0 @@ -package ch.asynk.tankontank.game.states; - -import com.badlogic.gdx.math.GridPoint2; - -import ch.asynk.tankontank.engine.Pawn; -import ch.asynk.tankontank.game.Map; -import ch.asynk.tankontank.game.Hex; -import ch.asynk.tankontank.game.Ctrl; -import ch.asynk.tankontank.game.GameState; - -public abstract class GameStateCommon implements GameState -{ - protected static Ctrl ctrl; - protected static Map map; - protected static Pawn activePawn; - protected static Pawn selectedPawn; - - protected static GridPoint2 selectedHex = new GridPoint2(-1, -1); - protected static GridPoint2 downHex = new GridPoint2(-1, -1); - protected static GridPoint2 upHex = new GridPoint2(-1, -1); - protected static GridPoint2 from = new GridPoint2(-1, -1); - protected static GridPoint2 to = new GridPoint2(-1, -1); - - protected boolean isEnemy; - - protected static GameState.State nextState = GameState.State.SELECT; - - protected GameStateCommon() - { - } - - public GameStateCommon(Ctrl ctrl, Map map) - { - this.ctrl = ctrl; - this.map = map; - } - - @Override - public GameState.State getNextState() - { - return nextState; - } - - @Override - public void setNextState(GameState.State state) - { - nextState = state; - } - - @Override - public void abort() - { - goToNextState(); - } - - @Override - public void done() - { - goToNextState(); - } - - public void clearAll() - { - from.set(-1, -1); - to.set(-1, -1); - selectedHex.set(-1, -1); - selectedPawn = null; - activePawn = null; - } - - private void goToNextState() - { - ctrl.hud.hide(); - GameState.State next = nextState; - nextState = GameState.State.SELECT; - ctrl.setState(next, (next == GameState.State.SELECT)); - } - - 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 selectHexAndPawn(GridPoint2 point) - { - selectedHex.set(point); - selectedPawn = map.getTopPawnAt(selectedHex); - selectHex(selectedHex); - if (selectedPawn != null) - isEnemy = ctrl.currentPlayer.isEnemy(selectedPawn); - else - isEnemy = false; - System.err.println((isEnemy ? "enemy " : "friend ") + selectedPawn + " (" + selectedHex.x + ";" + selectedHex.y + ")"); - } - - protected boolean hasPawn() - { - return (selectedPawn != null); - } - - protected void unselectHex(GridPoint2 hex) - { - map.enableOverlayOn(hex, Hex.SELECT, false); - } - - protected void selectHex(GridPoint2 hex) - { - map.enableOverlayOn(hex, Hex.SELECT, true); - } - - protected void showAssist(GridPoint2 hex, boolean enable) - { - map.enableOverlayOn(hex, Hex.ASSIST, enable); - } - - protected boolean sameHexes(GridPoint2 a, GridPoint2 b) - { - return ((a.x == b.x) && (a.y == b.y)); - } - - protected void hidePossibleTargetsMovesAssists() - { - map.showPossibleMoves(false); - map.showPossibleTargets(false); - map.showMoveAssists(false); - } - - protected void showPossibleTargetsMovesAssists(Pawn pawn) - { - if (ctrl.cfg.showMoves && pawn.canMove()) map.showPossibleMoves(true); - if (ctrl.cfg.showTargets && pawn.canAttack()) map.showPossibleTargets(true); - if (ctrl.cfg.showMoveAssists && pawn.canMove()) map.showMoveAssists(true); - } -} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java b/core/src/ch/asynk/tankontank/game/states/GameStateMove.java deleted file mode 100644 index 56a81a3..0000000 --- a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java +++ /dev/null @@ -1,147 +0,0 @@ -package ch.asynk.tankontank.game.states; - -import com.badlogic.gdx.math.GridPoint2; - -import ch.asynk.tankontank.game.GameState.State; - -public class GameStateMove extends GameStateCommon -{ - @Override - public void enter(boolean fromSelect) - { - map.clearPossiblePaths(); - ctrl.hud.show(true, true, false, ((map.activablePawnsCount() + map.activatedPawnsCount()) > 1), ctrl.cfg.canCancel); - ctrl.hud.moveBtn.setOn(); - - if (fromSelect) { - // use selectedHex and selectedPawn - from.set(selectedHex); - activePawn = selectedPawn; - map.buildAndShowMovesAndAssits(activePawn, from); - if (to.x != -1) { - // quick move -> replay touchUp - upHex.set(to); - touchUp(); - } - } else { - // back from rotation -> use the above and unmodified activePawn - if ((activePawn == selectedPawn) || !selectedPawn.canMove()) { - upHex.set(map.getFirstMoveAssist()); - activePawn = map.getTopPawnAt(upHex); - } else { - upHex.set(selectedHex); - } - changePawn(upHex); - } - } - - @Override - public void leave(State nextState) - { - // hide all but assists : want them when in rotation - map.showPossibleMoves(false); - unselectHex(from); - if (to.x != -1) { - unselectHex(to); - map.showFinalPath(to, false); - } - - if (nextState != State.SELECT) { - if (to.x == -1 ) - to.set(from); - } - } - - @Override - public void touchDown() - { - } - - @Override - public void touchUp() - { - int s = map.possiblePathsSize(); - - if (map.isInPossibleMoveAssists(upHex) || (selectedPawn.canMove() && sameHexes(selectedHex, upHex))) { - if(!sameHexes(upHex, from)) - changePawn(upHex); - } else if ((s == 0) && map.isInPossibleMoves(upHex)) { - s = buildPaths(); - } else if (map.isInPossiblePaths(upHex)) { - s = togglePoint(s); - } - - if (s == 1) { - // prevent changePawn - if (sameHexes(from, selectedHex)) - selectedHex.set(to); - ctrl.setState(State.ROTATE, false); - } - } - - @Override - public void abort() - { - hideAssists(); - ctrl.setAnimationCount(map.activatedPawnsCount()); - map.revertMoves(); - super.abort(); - } - - @Override - public void done() - { - hideAssists(); - if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) - selectedPawn.move(0); - super.done(); - } - - private void hideAssists() - { - showAssist(selectedHex, false); - map.showMoveAssists(false); - } - - private void changePawn(GridPoint2 next) - { - if (from.x != -1) { - // toggle selected to assist - unselectHex(from); - showAssist(from, true); - } - from.set(next); - activePawn = map.getTopPawnAt(from); - selectHex(from); - showAssist(from, false); - map.showPossibleMoves(false); - map.buildPossibleMoves(activePawn, from); - map.showPossibleMoves(true); - } - - private int buildPaths() - { - to.set(upHex.x, upHex.y); - int s = map.buildPossiblePaths(activePawn, from, to); - selectHex(to); - map.showPossibleMoves(false); - map.showPossiblePaths(true, true); - return s; - } - - private int togglePoint(int s) - { - if (sameHexes(downHex, from)) { - // - } else if (sameHexes(downHex, to)) { - // - } else { - map.showPossiblePaths(false, true); - map.togglePathOverlay(downHex); - s = map.possiblePathsPointToggle(downHex); - map.showPossiblePaths(true, true); - } - - return s; - } -} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java b/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java deleted file mode 100644 index a57ecb3..0000000 --- a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java +++ /dev/null @@ -1,114 +0,0 @@ -package ch.asynk.tankontank.game.states; - -import ch.asynk.tankontank.engine.Orientation; -import ch.asynk.tankontank.game.GameState.State; - -public class GameStateRotate extends GameStateCommon -{ - private boolean rotateOnly; - private boolean rotationSet; - private Orientation o = Orientation.KEEP; - - @Override - public void enter(boolean rotateOnly) - { - this.rotateOnly = rotateOnly; - - // if ((map.activablePawnsCount() + map.activatedPawnsCount()) == 1) - ctrl.hud.show(true, false, false, false, ctrl.cfg.canCancel); - ctrl.hud.rotateBtn.setOn(); - - if (rotateOnly) { - if (from.x == -1) { - // rotateBtn from Select state - from.set(selectedHex); - activePawn = selectedPawn; - } - to.set(from); - } else { - // show final path - selectHex(to); - map.showFinalPath(to, true); - } - - selectHex(from); - map.showDirections(to, true); - - rotationSet = false; - } - - @Override - public void leave(State nextState) - { - unselectHex(to); - unselectHex(from); - map.showFinalPath(to, false); - map.showDirections(to, false); - to.set(-1, -1); - from.set(-1, -1); - } - - @Override - public void touchDown() - { - } - - @Override - public void touchUp() - { - if (rotationSet) return; - - // FIXME: if to is on the border of the board ... - o = Orientation.fromAdj(to.x, to.y, downHex.x, downHex.y); - - if (o == Orientation.KEEP) return; - rotationSet = true; - - if (ctrl.cfg.mustValidate) { - // TODO show overlay - ctrl.hud.show(true, false, false, true, ctrl.cfg.canCancel); - } else - doRotation(o); - } - - @Override - public void abort() - { - hideAssists(); - ctrl.setAnimationCount(map.activatedPawnsCount()); - map.revertMoves(); - super.abort(); - } - - @Override - public void done() - { - doRotation(o); - if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) - selectedPawn.move(0); - super.done(); - } - - private void hideAssists() - { - showAssist(selectedHex, false); - map.showMoveAssists(false); - } - - private void doRotation(Orientation o) - { - if (!rotationSet) return; - - if (rotateOnly) { - ctrl.setAnimationCount(1); - if (map.rotatePawn(activePawn, from, o) > 0) - setNextState(State.MOVE); - ctrl.setState(State.ANIMATION); - } else { - ctrl.setAnimationCount(1); - if (map.movePawn(activePawn, from, o) > 0) - setNextState(State.MOVE); - ctrl.setState(State.ANIMATION); - } - } -} diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java b/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java deleted file mode 100644 index 711daae..0000000 --- a/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java +++ /dev/null @@ -1,73 +0,0 @@ -package ch.asynk.tankontank.game.states; - -import ch.asynk.tankontank.game.Map; -import ch.asynk.tankontank.game.Ctrl; -import ch.asynk.tankontank.game.GameState.State; - -public class GameStateSelect extends GameStateCommon -{ - public GameStateSelect(Ctrl ctrl, Map map) - { - super(ctrl, map); - } - - @Override - public void enter(boolean flag) - { - clearAll(); - map.clearAll(); - } - - @Override - public void leave(State nextState) - { - hidePossibleTargetsMovesAssists(); - } - - @Override - public void touchDown() - { - if (selectedHex.x != -1) unselectHex(selectedHex); - } - - @Override - public void touchUp() - { - if (!isEnemy && map.isInPossibleMoves(upHex)) { - // quick move - to.set(upHex); - ctrl.setState(State.MOVE); - return; - } - - selectHexAndPawn(upHex); - hidePossibleTargetsMovesAssists(); - - if (hasPawn() && (!isEnemy || ctrl.cfg.showEnemyPossibilities)) { - int moves = map.buildPossibleMoves(selectedPawn, selectedHex); - int targets = map.buildPossibleTargets(selectedPawn, selectedHex); - int assists = map.buildMoveAssists(selectedPawn, selectedHex); - showPossibleTargetsMovesAssists(selectedPawn); - ctrl.hud.show( - selectedPawn.canMove(), - (selectedPawn.canMove() && (moves > 0)), - (selectedPawn.canAttack() && (targets > 0)), - false, - false - ); - } else { - ctrl.hud.hide(); - map.clearAll(); - } - } - - @Override - public void abort() - { - } - - @Override - public void done() - { - } -} diff --git a/core/src/ch/asynk/tankontank/game/states/StateAnimation.java b/core/src/ch/asynk/tankontank/game/states/StateAnimation.java new file mode 100644 index 0000000..753cbf8 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/StateAnimation.java @@ -0,0 +1,41 @@ +package ch.asynk.tankontank.game.states; + +import ch.asynk.tankontank.game.State.StateType; + +public class StateAnimation extends StateCommon +{ + @Override + public void enter(boolean flag) + { + } + + @Override + public void leave(StateType nextState) + { + if (nextState != StateType.SELECT) { + from.set(-1, -1); + to.set(-1, -1); + } + } + + @Override + public void touchDown() + { + } + + @Override + public void touchUp() + { + } + + @Override + public void abort() + { + } + + @Override + public void done() + { + super.done(); + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/StateCommon.java b/core/src/ch/asynk/tankontank/game/states/StateCommon.java new file mode 100644 index 0000000..8d7a48d --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/StateCommon.java @@ -0,0 +1,148 @@ +package ch.asynk.tankontank.game.states; + +import com.badlogic.gdx.math.GridPoint2; + +import ch.asynk.tankontank.engine.Pawn; +import ch.asynk.tankontank.game.Map; +import ch.asynk.tankontank.game.Hex; +import ch.asynk.tankontank.game.Ctrl; +import ch.asynk.tankontank.game.State; +import ch.asynk.tankontank.game.State.StateType; + +public abstract class StateCommon implements State +{ + protected static Ctrl ctrl; + protected static Map map; + protected static Pawn activePawn; + protected static Pawn selectedPawn; + + protected static GridPoint2 selectedHex = new GridPoint2(-1, -1); + protected static GridPoint2 downHex = new GridPoint2(-1, -1); + protected static GridPoint2 upHex = new GridPoint2(-1, -1); + protected static GridPoint2 from = new GridPoint2(-1, -1); + protected static GridPoint2 to = new GridPoint2(-1, -1); + + protected boolean isEnemy; + + protected static StateType nextState = StateType.SELECT; + + protected StateCommon() + { + } + + public StateCommon(Ctrl ctrl, Map map) + { + this.ctrl = ctrl; + this.map = map; + } + + @Override + public StateType getNextState() + { + return nextState; + } + + @Override + public void setNextState(StateType state) + { + nextState = state; + } + + @Override + public void abort() + { + goToNextState(); + } + + @Override + public void done() + { + goToNextState(); + } + + public void clearAll() + { + from.set(-1, -1); + to.set(-1, -1); + selectedHex.set(-1, -1); + selectedPawn = null; + activePawn = null; + } + + private void goToNextState() + { + ctrl.hud.hide(); + StateType next = nextState; + nextState = StateType.SELECT; + ctrl.setState(next, (next == StateType.SELECT)); + } + + 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 selectHexAndPawn(GridPoint2 point) + { + selectedHex.set(point); + selectedPawn = map.getTopPawnAt(selectedHex); + selectHex(selectedHex); + if (selectedPawn != null) + isEnemy = ctrl.currentPlayer.isEnemy(selectedPawn); + else + isEnemy = false; + System.err.println((isEnemy ? "enemy " : "friend ") + selectedPawn + " (" + selectedHex.x + ";" + selectedHex.y + ")"); + } + + protected boolean hasPawn() + { + return (selectedPawn != null); + } + + protected void unselectHex(GridPoint2 hex) + { + map.enableOverlayOn(hex, Hex.SELECT, false); + } + + protected void selectHex(GridPoint2 hex) + { + map.enableOverlayOn(hex, Hex.SELECT, true); + } + + protected void showAssist(GridPoint2 hex, boolean enable) + { + map.enableOverlayOn(hex, Hex.ASSIST, enable); + } + + protected boolean sameHexes(GridPoint2 a, GridPoint2 b) + { + return ((a.x == b.x) && (a.y == b.y)); + } + + protected void hidePossibleTargetsMovesAssists() + { + map.showPossibleMoves(false); + map.showPossibleTargets(false); + map.showMoveAssists(false); + } + + protected void showPossibleTargetsMovesAssists(Pawn pawn) + { + if (ctrl.cfg.showMoves && pawn.canMove()) map.showPossibleMoves(true); + if (ctrl.cfg.showTargets && pawn.canAttack()) map.showPossibleTargets(true); + if (ctrl.cfg.showMoveAssists && pawn.canMove()) map.showMoveAssists(true); + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/StateMove.java b/core/src/ch/asynk/tankontank/game/states/StateMove.java new file mode 100644 index 0000000..2038855 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/StateMove.java @@ -0,0 +1,147 @@ +package ch.asynk.tankontank.game.states; + +import com.badlogic.gdx.math.GridPoint2; + +import ch.asynk.tankontank.game.State.StateType; + +public class StateMove extends StateCommon +{ + @Override + public void enter(boolean fromSelect) + { + map.clearPossiblePaths(); + ctrl.hud.show(true, true, false, ((map.activablePawnsCount() + map.activatedPawnsCount()) > 1), ctrl.cfg.canCancel); + ctrl.hud.moveBtn.setOn(); + + if (fromSelect) { + // use selectedHex and selectedPawn + from.set(selectedHex); + activePawn = selectedPawn; + map.buildAndShowMovesAndAssits(activePawn, from); + if (to.x != -1) { + // quick move -> replay touchUp + upHex.set(to); + touchUp(); + } + } else { + // back from rotation -> use the above and unmodified activePawn + if ((activePawn == selectedPawn) || !selectedPawn.canMove()) { + upHex.set(map.getFirstMoveAssist()); + activePawn = map.getTopPawnAt(upHex); + } else { + upHex.set(selectedHex); + } + changePawn(upHex); + } + } + + @Override + public void leave(StateType nextState) + { + // hide all but assists : want them when in rotation + map.showPossibleMoves(false); + unselectHex(from); + if (to.x != -1) { + unselectHex(to); + map.showFinalPath(to, false); + } + + if (nextState != StateType.SELECT) { + if (to.x == -1 ) + to.set(from); + } + } + + @Override + public void touchDown() + { + } + + @Override + public void touchUp() + { + int s = map.possiblePathsSize(); + + if (map.isInPossibleMoveAssists(upHex) || (selectedPawn.canMove() && sameHexes(selectedHex, upHex))) { + if(!sameHexes(upHex, from)) + changePawn(upHex); + } else if ((s == 0) && map.isInPossibleMoves(upHex)) { + s = buildPaths(); + } else if (map.isInPossiblePaths(upHex)) { + s = togglePoint(s); + } + + if (s == 1) { + // prevent changePawn + if (sameHexes(from, selectedHex)) + selectedHex.set(to); + ctrl.setState(StateType.ROTATE, false); + } + } + + @Override + public void abort() + { + hideAssists(); + ctrl.setAnimationCount(map.activatedPawnsCount()); + map.revertMoves(); + super.abort(); + } + + @Override + public void done() + { + hideAssists(); + if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) + selectedPawn.move(0); + super.done(); + } + + private void hideAssists() + { + showAssist(selectedHex, false); + map.showMoveAssists(false); + } + + private void changePawn(GridPoint2 next) + { + if (from.x != -1) { + // toggle selected to assist + unselectHex(from); + showAssist(from, true); + } + from.set(next); + activePawn = map.getTopPawnAt(from); + selectHex(from); + showAssist(from, false); + map.showPossibleMoves(false); + map.buildPossibleMoves(activePawn, from); + map.showPossibleMoves(true); + } + + private int buildPaths() + { + to.set(upHex.x, upHex.y); + int s = map.buildPossiblePaths(activePawn, from, to); + selectHex(to); + map.showPossibleMoves(false); + map.showPossiblePaths(true, true); + return s; + } + + private int togglePoint(int s) + { + if (sameHexes(downHex, from)) { + // + } else if (sameHexes(downHex, to)) { + // + } else { + map.showPossiblePaths(false, true); + map.togglePathOverlay(downHex); + s = map.possiblePathsPointToggle(downHex); + map.showPossiblePaths(true, true); + } + + return s; + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/StateRotate.java b/core/src/ch/asynk/tankontank/game/states/StateRotate.java new file mode 100644 index 0000000..f692ed0 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/StateRotate.java @@ -0,0 +1,114 @@ +package ch.asynk.tankontank.game.states; + +import ch.asynk.tankontank.engine.Orientation; +import ch.asynk.tankontank.game.State.StateType; + +public class StateRotate extends StateCommon +{ + private boolean rotateOnly; + private boolean rotationSet; + private Orientation o = Orientation.KEEP; + + @Override + public void enter(boolean rotateOnly) + { + this.rotateOnly = rotateOnly; + + // if ((map.activablePawnsCount() + map.activatedPawnsCount()) == 1) + ctrl.hud.show(true, false, false, false, ctrl.cfg.canCancel); + ctrl.hud.rotateBtn.setOn(); + + if (rotateOnly) { + if (from.x == -1) { + // rotateBtn from Select state + from.set(selectedHex); + activePawn = selectedPawn; + } + to.set(from); + } else { + // show final path + selectHex(to); + map.showFinalPath(to, true); + } + + selectHex(from); + map.showDirections(to, true); + + rotationSet = false; + } + + @Override + public void leave(StateType nextState) + { + unselectHex(to); + unselectHex(from); + map.showFinalPath(to, false); + map.showDirections(to, false); + to.set(-1, -1); + from.set(-1, -1); + } + + @Override + public void touchDown() + { + } + + @Override + public void touchUp() + { + if (rotationSet) return; + + // FIXME: if to is on the border of the board ... + o = Orientation.fromAdj(to.x, to.y, downHex.x, downHex.y); + + if (o == Orientation.KEEP) return; + rotationSet = true; + + if (ctrl.cfg.mustValidate) { + // TODO show overlay + ctrl.hud.show(true, false, false, true, ctrl.cfg.canCancel); + } else + doRotation(o); + } + + @Override + public void abort() + { + hideAssists(); + ctrl.setAnimationCount(map.activatedPawnsCount()); + map.revertMoves(); + super.abort(); + } + + @Override + public void done() + { + doRotation(o); + if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) + selectedPawn.move(0); + super.done(); + } + + private void hideAssists() + { + showAssist(selectedHex, false); + map.showMoveAssists(false); + } + + private void doRotation(Orientation o) + { + if (!rotationSet) return; + + if (rotateOnly) { + ctrl.setAnimationCount(1); + if (map.rotatePawn(activePawn, from, o) > 0) + setNextState(StateType.MOVE); + ctrl.setState(StateType.ANIMATION); + } else { + ctrl.setAnimationCount(1); + if (map.movePawn(activePawn, from, o) > 0) + setNextState(StateType.MOVE); + ctrl.setState(StateType.ANIMATION); + } + } +} diff --git a/core/src/ch/asynk/tankontank/game/states/StateSelect.java b/core/src/ch/asynk/tankontank/game/states/StateSelect.java new file mode 100644 index 0000000..ec1ba42 --- /dev/null +++ b/core/src/ch/asynk/tankontank/game/states/StateSelect.java @@ -0,0 +1,73 @@ +package ch.asynk.tankontank.game.states; + +import ch.asynk.tankontank.game.Map; +import ch.asynk.tankontank.game.Ctrl; +import ch.asynk.tankontank.game.State.StateType; + +public class StateSelect extends StateCommon +{ + public StateSelect(Ctrl ctrl, Map map) + { + super(ctrl, map); + } + + @Override + public void enter(boolean flag) + { + clearAll(); + map.clearAll(); + } + + @Override + public void leave(StateType nextState) + { + hidePossibleTargetsMovesAssists(); + } + + @Override + public void touchDown() + { + if (selectedHex.x != -1) unselectHex(selectedHex); + } + + @Override + public void touchUp() + { + if (!isEnemy && map.isInPossibleMoves(upHex)) { + // quick move + to.set(upHex); + ctrl.setState(StateType.MOVE); + return; + } + + selectHexAndPawn(upHex); + hidePossibleTargetsMovesAssists(); + + if (hasPawn() && (!isEnemy || ctrl.cfg.showEnemyPossibilities)) { + int moves = map.buildPossibleMoves(selectedPawn, selectedHex); + int targets = map.buildPossibleTargets(selectedPawn, selectedHex); + int assists = map.buildMoveAssists(selectedPawn, selectedHex); + showPossibleTargetsMovesAssists(selectedPawn); + ctrl.hud.show( + selectedPawn.canMove(), + (selectedPawn.canMove() && (moves > 0)), + (selectedPawn.canAttack() && (targets > 0)), + false, + false + ); + } else { + ctrl.hud.hide(); + map.clearAll(); + } + } + + @Override + public void abort() + { + } + + @Override + public void done() + { + } +} -- cgit v1.1-2-g2b99