From 094ceae2e945fc9d19a5ad8446f309c13fa6ff8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 12 Oct 2014 02:27:53 +0200 Subject: GameState: fixes cleanup rewrite --- core/src/ch/asynk/tankontank/game/GameCtrl.java | 14 +++---- core/src/ch/asynk/tankontank/game/GameState.java | 2 +- core/src/ch/asynk/tankontank/game/Hud.java | 2 +- .../tankontank/game/states/GameStateAnimation.java | 9 ++++- .../tankontank/game/states/GameStateCommon.java | 36 ++++++++++-------- .../tankontank/game/states/GameStateMove.java | 37 ++++++++++++------- .../tankontank/game/states/GameStateRotate.java | 43 ++++++++++------------ .../tankontank/game/states/GameStateSelect.java | 25 ++++++------- 8 files changed, 94 insertions(+), 74 deletions(-) diff --git a/core/src/ch/asynk/tankontank/game/GameCtrl.java b/core/src/ch/asynk/tankontank/game/GameCtrl.java index af0f788..fd4fef5 100644 --- a/core/src/ch/asynk/tankontank/game/GameCtrl.java +++ b/core/src/ch/asynk/tankontank/game/GameCtrl.java @@ -28,7 +28,7 @@ public class GameCtrl implements Disposable this.showMoveAssists = true; this.canCancel = true; this.mustValidate = false; - this.showEnemyPossibilities = false; // TODO + this.showEnemyPossibilities = false; } } @@ -94,16 +94,16 @@ public class GameCtrl implements Disposable public void setAnimationCount(int count) { animationCount = count; + System.err.println(" setAnimationCount(" + count + ")"); } public void animationDone() { animationCount -= 1; - if (animationCount == 0) { - GameState.State next = state.getNextState(); - state.setNextState(GameState.State.SELECT); - setState(next, (next == GameState.State.SELECT)); - } + if (animationCount == 0) + state.done(); + if (animationCount < 0) + System.err.println("animationCount < 0"); } private void nextPlayer() @@ -122,7 +122,7 @@ public class GameCtrl implements Disposable public void setState(GameState.State state, boolean normal) { - this.state.leave(); + this.state.leave(state); switch(state) { case SELECT: diff --git a/core/src/ch/asynk/tankontank/game/GameState.java b/core/src/ch/asynk/tankontank/game/GameState.java index 23b4ffd..e6741a7 100644 --- a/core/src/ch/asynk/tankontank/game/GameState.java +++ b/core/src/ch/asynk/tankontank/game/GameState.java @@ -11,7 +11,7 @@ public interface GameState public void enter(boolean flag); - public void leave(); + public void leave(GameState.State nextState); public void abort(); diff --git a/core/src/ch/asynk/tankontank/game/Hud.java b/core/src/ch/asynk/tankontank/game/Hud.java index ee01a18..54c33e6 100644 --- a/core/src/ch/asynk/tankontank/game/Hud.java +++ b/core/src/ch/asynk/tankontank/game/Hud.java @@ -164,7 +164,7 @@ public class Hud implements Disposable if (btn == moveBtn) ctrl.setState(GameState.State.MOVE); else if (btn == rotateBtn) - ctrl.setState(GameState.State.ROTATE, false); + ctrl.setState(GameState.State.ROTATE); else if (btn == attackBtn) // TODO ctrl.setState(GameState.State.ATTACK); System.out.println(" ATTACK not implemented yet"); diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java b/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java index 820929a..e1b2c08 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java @@ -1,5 +1,7 @@ package ch.asynk.tankontank.game.states; +import ch.asynk.tankontank.game.GameState.State; + public class GameStateAnimation extends GameStateCommon { @Override @@ -8,8 +10,12 @@ public class GameStateAnimation extends GameStateCommon } @Override - public void leave() + public void leave(State nextState) { + if (nextState != State.SELECT) { + from.set(-1, -1); + to.set(-1, -1); + } } @Override @@ -30,5 +36,6 @@ public class GameStateAnimation extends GameStateCommon @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 index e4365e5..456e529 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java @@ -21,6 +21,8 @@ public abstract class GameStateCommon implements GameState 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() @@ -48,22 +50,30 @@ public abstract class GameStateCommon implements GameState @Override public void abort() { - clearAndGoToSelect(); + goToNextState(); } @Override public void done() { - clearAndGoToSelect(); + goToNextState(); } - private void clearAndGoToSelect() + public void clearAll() { - unselectHex(selectedHex); + from.set(-1, -1); + to.set(-1, -1); selectedHex.set(-1, -1); selectedPawn = null; + activePawn = null; + } + + private void goToNextState() + { ctrl.hud.hide(); - ctrl.setState(State.SELECT); + GameState.State next = nextState; + nextState = GameState.State.SELECT; + ctrl.setState(next, (next == GameState.State.SELECT)); } protected static boolean hexInMap(GridPoint2 hex) @@ -84,12 +94,15 @@ public abstract class GameStateCommon implements GameState return hexInMap(upHex); } - protected void setHexAndPawn(GridPoint2 point) + protected void selectHexAndPawn(GridPoint2 point) { selectedHex.set(point); - // TODO : is an enemy or not ? selectedPawn = map.getTopPawnAt(selectedHex); - System.err.println("setHexAndPawn : " + selectedHex.x + ";" + selectedHex.y + " " + selectedPawn); + selectHex(selectedHex); + if (selectedPawn != null) + isEnemy = ctrl.currentPlayer.isEnemy(selectedPawn); + else + isEnemy = false; } protected boolean hasPawn() @@ -112,13 +125,6 @@ public abstract class GameStateCommon implements GameState map.enableOverlayOn(hex, Hex.ASSIST, enable); } - protected void reselectHex() - { - if (selectedHex.x != -1) unselectHex(selectedHex); - setHexAndPawn(downHex); - selectHex(selectedHex); - } - protected boolean sameHexes(GridPoint2 a, GridPoint2 b) { return ((a.x == b.x) && (a.y == b.y)); diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java b/core/src/ch/asynk/tankontank/game/states/GameStateMove.java index bd767e9..56a81a3 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateMove.java @@ -2,47 +2,54 @@ 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); } - from.set(-1, -1); changePawn(upHex); } - - ctrl.hud.show(true, true, false, ((map.activablePawnsCount() + map.activatedPawnsCount()) > 1), ctrl.cfg.canCancel); - ctrl.hud.moveBtn.setOn(); } @Override - public void leave() + public void leave(State nextState) { - // hide all but assists + // hide all but assists : want them when in rotation map.showPossibleMoves(false); unselectHex(from); if (to.x != -1) { unselectHex(to); map.showFinalPath(to, false); } - ctrl.hud.hide(); + + if (nextState != State.SELECT) { + if (to.x == -1 ) + to.set(from); + } } @Override @@ -55,7 +62,7 @@ public class GameStateMove extends GameStateCommon { int s = map.possiblePathsSize(); - if (sameHexes(selectedHex, upHex) || map.isInPossibleMoveAssists(upHex)) { + if (map.isInPossibleMoveAssists(upHex) || (selectedPawn.canMove() && sameHexes(selectedHex, upHex))) { if(!sameHexes(upHex, from)) changePawn(upHex); } else if ((s == 0) && map.isInPossibleMoves(upHex)) { @@ -64,8 +71,12 @@ public class GameStateMove extends GameStateCommon s = togglePoint(s); } - if (s == 1) - ctrl.setState(State.ROTATE); + if (s == 1) { + // prevent changePawn + if (sameHexes(from, selectedHex)) + selectedHex.set(to); + ctrl.setState(State.ROTATE, false); + } } @Override @@ -80,9 +91,9 @@ public class GameStateMove extends GameStateCommon @Override public void done() { + hideAssists(); if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) selectedPawn.move(0); - hideAssists(); super.done(); } @@ -94,15 +105,15 @@ public class GameStateMove extends GameStateCommon private void changePawn(GridPoint2 next) { - // do not show the last moved assist 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); - activePawn = map.getTopPawnAt(from); map.showPossibleMoves(false); map.buildPossibleMoves(activePawn, from); map.showPossibleMoves(true); diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java b/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java index 6048ce4..a57ecb3 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java @@ -6,47 +6,46 @@ import ch.asynk.tankontank.game.GameState.State; public class GameStateRotate extends GameStateCommon { private boolean rotateOnly; - private boolean canDoRotation; + private boolean rotationSet; private Orientation o = Orientation.KEEP; @Override - public void enter(boolean hasFinalMove) + public void enter(boolean rotateOnly) { - if (!hasFinalMove) { - to.set(-1, -1); + 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; } - } - - rotateOnly = (to.x == -1); - - selectHex(from); - if (rotateOnly) { to.set(from); } else { + // show final path selectHex(to); map.showFinalPath(to, true); } - map.showDirections(to, true); - // if ((map.activablePawnsCount() + map.activatedPawnsCount()) == 1) - ctrl.hud.show(true, false, false, false, ctrl.cfg.canCancel); - ctrl.hud.rotateBtn.setOn(); + selectHex(from); + map.showDirections(to, true); - canDoRotation = false; + rotationSet = false; } @Override - public void leave() + public void leave(State nextState) { unselectHex(to); unselectHex(from); map.showFinalPath(to, false); map.showDirections(to, false); to.set(-1, -1); - ctrl.hud.hide(); + from.set(-1, -1); } @Override @@ -57,13 +56,13 @@ public class GameStateRotate extends GameStateCommon @Override public void touchUp() { - if (canDoRotation) return; + 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; - canDoRotation = true; + rotationSet = true; if (ctrl.cfg.mustValidate) { // TODO show overlay @@ -84,10 +83,10 @@ public class GameStateRotate extends GameStateCommon @Override public void done() { - // hideAssists(); doRotation(o); if (selectedPawn.canMove() && (map.activatedPawnsCount() > 0)) selectedPawn.move(0); + super.done(); } private void hideAssists() @@ -98,7 +97,7 @@ public class GameStateRotate extends GameStateCommon private void doRotation(Orientation o) { - if (!canDoRotation) return; + if (!rotationSet) return; if (rotateOnly) { ctrl.setAnimationCount(1); @@ -111,7 +110,5 @@ public class GameStateRotate extends GameStateCommon setNextState(State.MOVE); ctrl.setState(State.ANIMATION); } - - canDoRotation = false; } } diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java b/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java index ede55cb..309ee50 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java @@ -2,11 +2,10 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.game.Map; import ch.asynk.tankontank.game.GameCtrl; +import ch.asynk.tankontank.game.GameState.State; public class GameStateSelect extends GameStateCommon { - private boolean jumpToMove; - public GameStateSelect(GameCtrl ctrl, Map map) { super(ctrl, map); @@ -15,12 +14,12 @@ public class GameStateSelect extends GameStateCommon @Override public void enter(boolean flag) { - ctrl.hud.hide(); - jumpToMove = false; + clearAll(); + map.clearAll(); } @Override - public void leave() + public void leave(State nextState) { hidePossibleTargetsMovesAssists(); } @@ -28,23 +27,23 @@ public class GameStateSelect extends GameStateCommon @Override public void touchDown() { - if (map.isInPossibleMoves(downHex)) - jumpToMove = true; - else - reselectHex(); + if (selectedHex.x != -1) unselectHex(selectedHex); } @Override public void touchUp() { - if (jumpToMove) { - to.set(downHex); + if (!isEnemy && map.isInPossibleMoves(upHex)) { + // quick move + to.set(upHex); ctrl.setState(State.MOVE); return; } + selectHexAndPawn(upHex); hidePossibleTargetsMovesAssists(); - if (hasPawn()) { + + if (hasPawn() && (!isEnemy || ctrl.cfg.showEnemyPossibilities)) { int moves = map.buildPossibleMoves(selectedPawn, selectedHex); int targets = map.buildPossibleTargets(selectedPawn, selectedHex); int assists = map.buildMoveAssists(selectedPawn, selectedHex); @@ -58,7 +57,7 @@ public class GameStateSelect extends GameStateCommon ); } else { ctrl.hud.hide(); - map.clearPossibleTargetsMovesAssists(); + map.clearAll(); } } -- cgit v1.1-2-g2b99