From e60b3d647b9eff905977fd9f5a7760ce0b0113ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 18 Jan 2015 22:06:22 +0100 Subject: remove Pawn.Movement class, use Move instead --- core/src/ch/asynk/tankontank/engine/Board.java | 19 ++++- core/src/ch/asynk/tankontank/engine/Pawn.java | 87 ++++++++++------------ .../ch/asynk/tankontank/engine/PossiblePaths.java | 51 +++---------- core/src/ch/asynk/tankontank/game/Map.java | 21 ++++-- core/src/ch/asynk/tankontank/game/Unit.java | 18 ++++- .../ch/asynk/tankontank/game/states/StateMove.java | 2 +- .../asynk/tankontank/game/states/StateRotate.java | 4 +- 7 files changed, 93 insertions(+), 109 deletions(-) diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java index 28164ce..5ec1210 100644 --- a/core/src/ch/asynk/tankontank/engine/Board.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -405,6 +405,12 @@ public abstract class Board implements Disposable, Animation return n; } + public Pawn setPawnOnto(Pawn pawn, Move move) + { + pawn.move(move); + return setPawnOnto(pawn, move.to, move.orientation.r()); + } + public Pawn setPawnOnto(Pawn pawn, Tile tile, Orientation o) { return setPawnOnto(pawn, tile, o.r()); @@ -422,21 +428,26 @@ public abstract class Board implements Disposable, Animation return RunnableAnimation.get(pawn, new Runnable() { @Override public void run() { - setPawnOnto(pawn, pawn.movement.to, pawn.movement.orientation); + setPawnOnto(pawn, pawn.move.to, pawn.move.orientation); } }); } - protected void movePawn(final Pawn pawn, PossiblePaths possiblePaths, RunnableAnimation whenDone, MoveToAnimationCb cb) + protected void movePawn(final Pawn pawn, Move move, RunnableAnimation whenDone, MoveToAnimationCb cb) { + pawn.move(move); removePawn(pawn); - AnimationSequence seq = pawn.getMoveAnimation(possiblePaths.iterator(), (possiblePaths.pathSteps(0) + 2), cb); + AnimationSequence seq = pawn.getMoveAnimation(move.iterator(), (move.steps() + 2), cb); seq.addAnimation(getSetPawnOntoAnimation(pawn)); seq.addAnimation(whenDone); addAnimation(seq); + } - pawn.move(); + protected void enterPawn(final Pawn pawn, Move move) + { + pawn.enter(move); + setPawnOnto(pawn, move.to, move.orientation); } protected void revertLastPawnMove(final Pawn pawn, RunnableAnimation whenDone) diff --git a/core/src/ch/asynk/tankontank/engine/Pawn.java b/core/src/ch/asynk/tankontank/engine/Pawn.java index d3359ef..a2e6951 100644 --- a/core/src/ch/asynk/tankontank/engine/Pawn.java +++ b/core/src/ch/asynk/tankontank/engine/Pawn.java @@ -50,47 +50,6 @@ public abstract class Pawn implements Moveable, Disposable } } - public class Movement - { - Tile from; - Tile to; - int distance; - public int cost; - boolean roadMarch; - public boolean entryMove; - Orientation orientation; - - public String toString() - { - if (to == null) - return "move : HQ activation"; - else if (from == null) - return "move : reinforcement -> [" + to.col + ";" + to.row + ";" + orientation + "] dist:" + distance + " cost:" + cost + " road:" + roadMarch + " entry:" + entryMove; - else - return "move : [" + from.col + ";" + from.row + "] -> [" + to.col + ";" + to.row + ";" + orientation + "] dist:" + distance + " cost:" + cost + " road:" + roadMarch + " entry:" + entryMove; - } - - public void reset() - { - from = null; - to = null; - cost = 0; - roadMarch = false; - entryMove = false; - orientation = Orientation.KEEP; - } - - public boolean isRotation() - { - return (distance == 0); - } - - public boolean isComplete() - { - return ((from != null) && (to != null)); - } - } - private static final float MOVE_TIME = 0.4f; private Vector3 position; @@ -102,7 +61,7 @@ public abstract class Pawn implements Moveable, Disposable private Sprite sprite; private StackedImages overlays; public Engagement engagement = new Engagement(); - public Movement movement= new Movement(); + protected Move move; public abstract int getMovementPoints(); public abstract int getRoadMarchBonus(); @@ -166,19 +125,49 @@ public abstract class Pawn implements Moveable, Disposable public void reset() { engagement.reset(); - movement.reset(); + if (move != null) { + move.dispose(); + move = null; + } + } + + public void move(Move move) + { + if (move.isEntry()) + throw new RuntimeException("wrong MoveType"); + + if (this.move != null) { + if (this.move.isEntry()) + this.move.dispose(); + else + throw new RuntimeException("try to override an existing move instance"); + } + + setMove(move); } - public void enterBoard(Tile to, Orientation o) + public void enter(Move move) { - movement.to = to; - movement.from = null; - movement.entryMove = true; - movement.orientation = o; - movement.cost = to.costFrom(this, o); + if (!move.isEntry()) + throw new RuntimeException("wrong MoveType"); + + if (this.move != null) + throw new RuntimeException("try to override an existing move instance"); + + setMove(move); + } + + private void setMove(Move move) + { + this.move = move; move(); } + public boolean justEntered() + { + return ((move != null) && move.isEntry()); + } + public boolean is(Faction faction) { return (this.faction == faction); diff --git a/core/src/ch/asynk/tankontank/engine/PossiblePaths.java b/core/src/ch/asynk/tankontank/engine/PossiblePaths.java index 163df7c..847b88e 100644 --- a/core/src/ch/asynk/tankontank/engine/PossiblePaths.java +++ b/core/src/ch/asynk/tankontank/engine/PossiblePaths.java @@ -1,6 +1,5 @@ package ch.asynk.tankontank.engine; -import java.util.Iterator; import java.util.ArrayList; import java.util.List; import java.util.LinkedList; @@ -11,7 +10,7 @@ import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; -public class PossiblePaths implements Disposable, Iterable +public class PossiblePaths implements Disposable { private final Board board; @@ -186,20 +185,17 @@ public class PossiblePaths implements Disposable, Iterable return filteredPaths.size(); } - public void applyToPawn(int i) + public int pathCost(int i) { - pawn.movement.from = from; - pawn.movement.to = to; - pawn.movement.orientation = orientation; - Path path = paths.get(i); - pawn.movement.cost = path.cost; - pawn.movement.distance = this.distance; - pawn.movement.roadMarch = path.roadMarch; + return paths.get(i).cost; } - public int pathCost(int i) + public Move getMove() { - return paths.get(i).cost; + if (size() != 1) + return null; + + return Move.get(pawn, from, to, orientation, getPath(0)); } public Path getPath(int i) @@ -217,38 +213,9 @@ public class PossiblePaths implements Disposable, Iterable to = board.getAdjTileAt(to, exit); } - public int pathSteps(int idx) - { - int steps = 0; - - Tile tile = from; - Orientation o = pawn.getOrientation(); - for (Tile next : getPath(idx).tiles) { - Orientation nextO = Orientation.fromMove(tile.col, tile.row, next.col, next.row); - if (nextO != o) { - steps += 2; - o = nextO; - } else - steps += 1; - tile = next; - } - if (orientation != Orientation.fromMove(tile.col, tile.row, to.col, to.row)) - steps += 2; - else - steps +=1; - - return steps; - } - - @Override - public Iterator iterator() - { - return new PathIterator(pawn, from, to, orientation, getPath(0).tiles); - } - private void printToErr(String what, List paths) { - System.err.println(what + " ("+paths.size()+") " + from + " -> " + to); + System.err.println(what + pawn + " ("+paths.size()+") " + from + " -> " + to); for (Path path : paths) { System.err.println(String.format(" - path (l:%d c:%d r:%b)", path.tiles.size(), path.cost, path.roadMarch)); for(Tile tile : path.tiles) diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index bc3bd43..f32b8d2 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import ch.asynk.tankontank.TankOnTank; import ch.asynk.tankontank.engine.Pawn; import ch.asynk.tankontank.engine.Board; +import ch.asynk.tankontank.engine.Move; import ch.asynk.tankontank.engine.SelectedTile; import ch.asynk.tankontank.engine.Orientation; import ch.asynk.tankontank.engine.Meteorology; @@ -206,7 +207,7 @@ public abstract class Map extends Board public int collectMoveableUnits(Unit unit) { - if (unit.isHq() && !unit.movement.entryMove) { + if (unit.canHQMove()) { collectMoveAssists(unit, moveableUnits.asPawns()); } else { moveableUnits.clear(); @@ -271,8 +272,11 @@ public abstract class Map extends Board Orientation entry = findBestEntry(unit, to, allowedMoves); if (entry == Orientation.KEEP) return false; - unit.enterBoard(to, entry); - setPawnOnto(unit, to, entry); + + Move move = Move.get(unit, null, to, entry, null); + move.setEnter(); + + enterPawn(unit, move); objectives.claim(to, unit.getArmy()); return true; } @@ -280,7 +284,11 @@ public abstract class Map extends Board public boolean setOnBoard(Unit unit, Hex to, Orientation entry) { TankOnTank.debug("Map", String.format("set %s %s %s", to.toShort(), unit, entry)); - setPawnOnto(unit, to, entry); + + Move move = Move.get(unit, null, to, entry, null); + move.setSet(); + + setPawnOnto(unit, move); objectives.claim(to, unit.getArmy()); return true; } @@ -288,7 +296,7 @@ public abstract class Map extends Board public void leaveBoard(Unit unit) { Hex hex = unit.getHex(); - if (unit.movement.entryMove) { + if (unit.justEntered()) { objectives.revert(); unit.reset(); } @@ -315,8 +323,7 @@ public abstract class Map extends Board public int moveUnit(Unit unit) { - possiblePaths.applyToPawn(0); - movePawn(unit, possiblePaths, notifyDoneAnimation(unit), objectives); + movePawn(unit, possiblePaths.getMove(), notifyDoneAnimation(unit), objectives); return startMove(unit); } diff --git a/core/src/ch/asynk/tankontank/game/Unit.java b/core/src/ch/asynk/tankontank/game/Unit.java index 12cac1f..87ba3cd 100644 --- a/core/src/ch/asynk/tankontank/game/Unit.java +++ b/core/src/ch/asynk/tankontank/game/Unit.java @@ -285,6 +285,11 @@ public class Unit extends HeadedPawn return (isEnemy(other) && canEngage()); } + public boolean canHQMove() + { + return (isHq() && ((move == null) || (!move.isEntry()))); + } + public void setMoved() { hasMoved = true; @@ -294,13 +299,18 @@ public class Unit extends HeadedPawn @Override public void move() { - TankOnTank.debug(movement.toString()); - if (movement.cost > mpLeft) TankOnTank.debug("ERROR: Movement point exceeded: " + movement.cost + "/" + mpLeft + " please report"); + int cost = move.cost; + + if (move.roadMarch && (cost > mpLeft)) + cost -= getRoadMarchBonus(); + + if (cost > mpLeft) + TankOnTank.debug("ERROR: Movement point exceeded: " + cost + "/" + mpLeft + " please report"); - if (movement.isComplete()) + if (move.isComplete()) setMoved(); - mpLeft -= movement.cost; + mpLeft -= cost; } @Override diff --git a/core/src/ch/asynk/tankontank/game/states/StateMove.java b/core/src/ch/asynk/tankontank/game/states/StateMove.java index 7ab8be8..0ed1b2a 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateMove.java +++ b/core/src/ch/asynk/tankontank/game/states/StateMove.java @@ -69,7 +69,7 @@ public class StateMove extends StateCommon public StateType abort() { hideAssists(); - if (activeUnit.movement.entryMove) { + if (activeUnit.justEntered()) { map.leaveBoard(activeUnit); ctrl.player.revertUnitEntry(activeUnit); return StateType.ABORT; diff --git a/core/src/ch/asynk/tankontank/game/states/StateRotate.java b/core/src/ch/asynk/tankontank/game/states/StateRotate.java index 2e1e822..2b45512 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateRotate.java +++ b/core/src/ch/asynk/tankontank/game/states/StateRotate.java @@ -54,7 +54,7 @@ public class StateRotate extends StateCommon { StateType nextState = StateType.ABORT; ctrl.hud.actionButtons.hide(); - if (activeUnit.movement.entryMove) { + if (activeUnit.justEntered()) { map.leaveBoard(activeUnit); ctrl.player.revertUnitEntry(activeUnit); nextState = StateType.ABORT; @@ -95,7 +95,7 @@ public class StateRotate extends StateCommon return; } - if (!activeUnit.movement.entryMove && rotateOnly && (o == activeUnit.getOrientation())) + if (!activeUnit.justEntered() && rotateOnly && (o == activeUnit.getOrientation())) return; map.possiblePaths.orientation = o; -- cgit v1.1-2-g2b99