diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-10-10 15:34:48 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-10-10 15:34:48 +0200 |
commit | e4e1f2d8e5c4b58b4592ef637e3f150c7f2a027c (patch) | |
tree | 609eda05855f5561056c44160eb8f8a2265fb9b8 | |
parent | c7cdb725e1710c64ff7c18d0cd8f964d3234809a (diff) | |
download | RustAndDust-e4e1f2d8e5c4b58b4592ef637e3f150c7f2a027c.zip RustAndDust-e4e1f2d8e5c4b58b4592ef637e3f150c7f2a027c.tar.gz |
rework GameState
6 files changed, 181 insertions, 56 deletions
diff --git a/core/src/ch/asynk/tankontank/game/Hud.java b/core/src/ch/asynk/tankontank/game/Hud.java index 701f2d3..2273724 100644 --- a/core/src/ch/asynk/tankontank/game/Hud.java +++ b/core/src/ch/asynk/tankontank/game/Hud.java @@ -170,7 +170,7 @@ public class Hud implements Disposable if (btn == moveBtn) ctrl.setState(GameState.State.MOVE); else if (btn == rotateBtn) - ctrl.setState(GameState.State.ROTATE); + ctrl.setState(GameState.State.ROTATE, false); 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 5bb63fa..a40f431 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateAnimation.java @@ -3,7 +3,7 @@ package ch.asynk.tankontank.game.states; public class GameStateAnimation extends GameStateCommon { @Override - public void enter(boolean reset) + public void enter(boolean normal) { } @@ -25,8 +25,10 @@ public class GameStateAnimation extends GameStateCommon @Override public void abort() { - unselectHex(hex); - pawn = null; - super.abort(); + } + + @Override + public void done() + { } } diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java index 4288ba0..369b10c 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateCommon.java @@ -13,11 +13,11 @@ public abstract class GameStateCommon implements GameState protected static GameCtrl ctrl; protected static Map map; protected static Pawn pawn; - protected static GridPoint2 hex = new GridPoint2(0, 0); + protected static Pawn activePawn; + protected static GridPoint2 hex = 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); @@ -48,8 +48,19 @@ public abstract class GameStateCommon implements GameState @Override public void abort() { + clearAndGoToSelect(); + } + + @Override + public void done() + { + clearAndGoToSelect(); + } + + private void clearAndGoToSelect() + { unselectHex(hex); - hex.set(0, 0); + hex.set(-1, -1); pawn = null; ctrl.hud.hide(); ctrl.setState(State.SELECT); @@ -95,9 +106,14 @@ public abstract class GameStateCommon implements GameState map.enableOverlayOn(hex, Hex.SELECT, true); } + protected void showAssist(GridPoint2 hex, boolean enable) + { + map.enableOverlayOn(hex, Hex.ASSIST, enable); + } + protected void reselectHex() { - unselectHex(hex); + if (hex.x != -1) unselectHex(hex); setHexAndPawn(downHex); selectHex(hex); } diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java b/core/src/ch/asynk/tankontank/game/states/GameStateMove.java index 8238cd8..7a953e3 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateMove.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateMove.java @@ -1,18 +1,44 @@ package ch.asynk.tankontank.game.states; +import com.badlogic.gdx.math.GridPoint2; + public class GameStateMove extends GameStateCommon { @Override - public void enter(boolean reset) + public void enter(boolean normal) { map.clearPossiblePaths(); - buildAndShowMoves(); - ctrl.hud.show(false, true, false, ctrl.cfg.mustValidate, ctrl.cfg.canCancel); + + if (normal) { + from.set(hex); + activePawn = pawn; + map.buildAndShowMovesAndAssits(activePawn, from); + } else { + if ((activePawn == pawn) || !pawn.canMove()) { + upHex.set(map.getFirstMoveAssist()); + activePawn = map.getTopPawnAt(upHex); + } else { + upHex.set(hex); + } + 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() { + // hide all but assists + map.showPossibleMoves(false); + unselectHex(from); + if (to.x != -1) { + unselectHex(to); + map.showFinalPath(to, false); + } + ctrl.hud.hide(); } @Override @@ -25,50 +51,63 @@ public class GameStateMove extends GameStateCommon { int s = map.possiblePathsSize(); - if (s == 0) { - if (map.isInPossibleMoves(upHex)) - s = buildPaths(); - } else { - if (map.isInPossiblePaths(upHex)) - s = togglePoint(s); + if (sameHexes(hex, upHex) || map.isInPossibleMoveAssists(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) - ctrl.setState(State.DIRECTION); + ctrl.setState(State.ROTATE); } @Override public void abort() { - map.showMoveAssists(false); - map.showPossibleMoves(false); - if (from.x != -1) { - unselectHex(from); - from.set(-1, -1); - } - if (to.x != -1) { - unselectHex(to); - map.showFinalPath(to, false); - to.set(-1, -1); - } + hideAssists(); + ctrl.setAnimationCount(map.activatedPawnsCount()); + map.revertMoves(); super.abort(); } - private void buildAndShowMoves() + @Override + public void done() { - map.showPossibleMoves(false); + if (pawn.canMove() && (map.activatedPawnsCount() > 0)) + pawn.move(0); + hideAssists(); + super.done(); + } + + private void hideAssists() + { + showAssist(hex, false); map.showMoveAssists(false); - map.buildPossibleMoves(pawn, hex); - map.buildMoveAssists(pawn, hex); + } + + private void changePawn(GridPoint2 next) + { + // do not show the last moved assist + if (from.x != -1) { + unselectHex(from); + showAssist(from, true); + } + from.set(next); + selectHex(from); + showAssist(from, false); + activePawn = map.getTopPawnAt(from); + map.showPossibleMoves(false); + map.buildPossibleMoves(activePawn, from); map.showPossibleMoves(true); - map.showMoveAssists(true); } private int buildPaths() { - from.set(hex.x, hex.y); to.set(upHex.x, upHex.y); - int s = map.buildPossiblePaths(pawn, from, to); + int s = map.buildPossiblePaths(activePawn, from, to); selectHex(to); map.showPossibleMoves(false); map.showPossiblePaths(true, true); diff --git a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java b/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java index d3848b0..5fc74af 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateRotate.java @@ -1,21 +1,47 @@ 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 canDoRotation; + private Orientation o = Orientation.KEEP; + @Override - public void enter(boolean reset) + public void enter(boolean normal) { - map.showDirections(hex, true); - ctrl.hud.show(true, false, false, ctrl.cfg.mustValidate, ctrl.cfg.canCancel); + if (!normal) + to.set(-1, -1); + + rotateOnly = (to.x == -1); + + selectHex(from); + if (rotateOnly) { + to.set(from); + } else { + 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(); + + canDoRotation = false; } @Override public void leave() { - unselectHex(hex); - map.showDirections(hex, false); + unselectHex(to); + unselectHex(from); + map.showFinalPath(to, false); + map.showDirections(to, false); + to.set(-1, -1); + ctrl.hud.hide(); } @Override @@ -26,23 +52,61 @@ public class GameStateRotate extends GameStateCommon @Override public void touchUp() { - Orientation o = Orientation.fromAdj(hex.x, hex.y, downHex.x, downHex.y); + if (canDoRotation) return; - if (o != Orientation.KEEP) { - if (pawn.getOrientation() != o) { - map.rotatePawn(pawn, o); - ctrl.setState(State.ANIMATION); - } else { - ctrl.animationDone(); - } - ctrl.hud.hide(); - } + // 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; + 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() + { + // hideAssists(); + doRotation(o); + if (pawn.canMove() && (map.activatedPawnsCount() > 0)) + pawn.move(0); + } + + private void hideAssists() + { + showAssist(hex, false); + map.showMoveAssists(false); + } + + private void doRotation(Orientation o) + { + if (!canDoRotation) 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); + } + + 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 bb15acc..28ef39b 100644 --- a/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java +++ b/core/src/ch/asynk/tankontank/game/states/GameStateSelect.java @@ -11,8 +11,9 @@ public class GameStateSelect extends GameStateCommon } @Override - public void enter(boolean reset) + public void enter(boolean normal) { + ctrl.hud.hide(); } @Override @@ -37,7 +38,7 @@ public class GameStateSelect extends GameStateCommon int assists = map.buildMoveAssists(pawn, hex); showPossibleTargetsMovesAssists(pawn); ctrl.hud.show( - pawn.canRotate(), + false, (pawn.canMove() && (moves > 0)), (pawn.canAttack() && (targets > 0)), false, @@ -52,7 +53,10 @@ public class GameStateSelect extends GameStateCommon @Override public void abort() { - hidePossibleTargetsMovesAssists(); - super.abort(); + } + + @Override + public void done() + { } } |