From 6cb55bb250d60e7c85bb0936f9eb3dfd5311a1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 23 Oct 2014 21:25:39 +0200 Subject: Map,Board: use TileList and HexList for moves and attacks --- core/src/ch/asynk/tankontank/engine/Board.java | 90 +++++++------------------- core/src/ch/asynk/tankontank/game/Map.java | 60 ++++++++--------- 2 files changed, 52 insertions(+), 98 deletions(-) diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java index 694161b..160b8c7 100644 --- a/core/src/ch/asynk/tankontank/engine/Board.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -250,74 +250,32 @@ public abstract class Board implements Disposable debugShapes.setTransformMatrix(prevTransform); } - protected void clearPointVector(ArrayList points) - { - for (GridPoint2 point : points) - gridPoint2Pool.free(point); - points.clear(); - } - - private void nodesToPoints(List nodes, ArrayList points) - { - int ns = nodes.size(); - int ps = points.size(); - - if (ps > ns) { - for (int i = (ps - 1); i >= ns; i--) - gridPoint2Pool.free(points.remove(i)); - } else - points.ensureCapacity(ns); - - int i = 0; - for (SearchBoard.Node node : nodes) { - if (i < ps) { - points.get(i).set(node.col, node.row); - } else { - GridPoint2 point = gridPoint2Pool.obtain(); - point.set(node.col, node.row); - points.add(point); - } - i += 1; - } - } - - protected int buildPossibleMovesFrom(Pawn pawn, GridPoint2 coords, ArrayList moves) + protected int buildPossibleMovesFrom(Pawn pawn, GridPoint2 coords, TileList moves) { List nodes = searchBoard.possibleMovesFrom(pawn, coords.x, coords.y); - nodesToPoints(nodes, moves); - return moves.size(); + return moves.fromNodes(nodes); } - protected int buildPossibleTargetsFrom(Pawn pawn, GridPoint2 coords, ArrayList targets) + protected int buildPossibleTargetsFrom(Pawn pawn, GridPoint2 coords, TileList targets) { List nodes = searchBoard.possibleTargetsFrom(pawn, coords.x, coords.y); - nodesToPoints(nodes, targets); - return targets.size(); + return targets.fromNodes(nodes); } - protected int buildPossibleTargetsFrom(Pawn pawn, GridPoint2 coords, Iterator units, ArrayList targets) + protected int buildPossibleTargetsFrom(Pawn pawn, GridPoint2 coords, Iterator units, TileList targets) { - clearPointVector(targets); - - GridPoint2 to = gridPoint2Pool.obtain(); + targets.clear(); while (units.hasNext()) { Pawn target = units.next(); Tile tile = target.getTile(); - to.set(tile.getCol(), tile.getRow()); - if (searchBoard.buildAttack(pawn, true, target, coords.x, coords.y, to.x, to.y)) { - targets.add(to); - to = gridPoint2Pool.obtain(); - } + if (searchBoard.buildAttack(pawn, true, target, coords.x, coords.y, tile.getCol(), tile.getRow())) + targets.add(tile); } - int s = targets.size(); - if ((s > 0) && (to != targets.get(s - 1))) - gridPoint2Pool.free(to); - - return s; + return targets.size(); } - protected int buildMoveAssists(Pawn pawn, GridPoint2 coords, List assists) + protected int buildMoveAssists(Pawn pawn, GridPoint2 coords, TileList assists) { assists.clear(); getAdjacentTiles(coords, neighbours); @@ -327,38 +285,28 @@ public abstract class Board implements Disposable // FIXME should support may pawns per tile Pawn p = t.getTopPawn(); if ((p != null) && p.canMove() && !pawn.isEnemy(p)) { - GridPoint2 assist = gridPoint2Pool.obtain(); - assist.set(t.getCol(), t.getRow()); - assists.add(assist); + assists.add(p.getTile()); } } } return assists.size(); } - protected int buildAttackAssists(Pawn pawn, Pawn target, GridPoint2 coords, Iterator units, ArrayList assists) + protected int buildAttackAssists(Pawn pawn, Pawn target, GridPoint2 coords, Iterator units, TileList assists) { - clearPointVector(assists); - - GridPoint2 from = gridPoint2Pool.obtain(); + assists.clear(); while (units.hasNext()) { Pawn p = units.next(); if ((p == pawn) || !p.canAttack()) continue; Tile tile = p.getTile(); - from.set(tile.getCol(), tile.getRow()); - if (searchBoard.buildAttack(p, !p.canAssistAttackWithoutLos(), target, from.x, from.y, coords.x, coords.y)) { + if (searchBoard.buildAttack(p, !p.canAssistAttackWithoutLos(), target, tile.getCol(), tile.getRow(), coords.x, coords.y)) { if (p != pawn) { - assists.add(from); - from = gridPoint2Pool.obtain(); + assists.add(tile); } } } - int s = assists.size(); - if ((s > 0) && (from != assists.get(s- 1))) - gridPoint2Pool.free(from); - - return s; + return assists.size(); } protected void clearPointSet(Set points) @@ -479,9 +427,15 @@ public abstract class Board implements Disposable return getTile(coords.x, coords.y).isOverlayEnabled(i); } + // FIXME should be removed public void enableOverlayOn(GridPoint2 coords, int i, boolean enable) { Tile tile = getTile(coords.x, coords.y); + enableOverlayOn(tile, i, enable); + } + + public void enableOverlayOn(Tile tile, int i, boolean enable) + { if(tile.enableOverlay(i, enable)) tilesToDraw.add(tile); else diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index e6ce882..94b66ca 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -25,11 +25,11 @@ public abstract class Map extends Board private final Ctrl ctrl; private final ArrayList finalPath = new ArrayList(10); - private final ArrayList possibleMoves = new ArrayList(40); - private final ArrayList possibleTargets = new ArrayList(10); + private final HexList possibleMoves; + private final HexList possibleTargets; + private final HexList moveAssists; + private final HexList attackAssists; private final HashSet possiblePaths = new HashSet(10); - private final ArrayList moveAssists = new ArrayList(6); - private final ArrayList attackAssists = new ArrayList(6); private final ArrayList activablePawns = new ArrayList(7); private final ArrayList activatedPawns = new ArrayList(7); @@ -46,6 +46,10 @@ public abstract class Map extends Board this.explosion = new SpriteAnimation(game.manager.get("data/explosion.png", Texture.class), 10, 4, 40); this.explosions = new SpriteAnimation(game.manager.get("data/explosions.png", Texture.class), 16, 8, 15); setup(); + possibleMoves = new HexList(this, 40); + possibleTargets = new HexList(this, 10); + moveAssists = new HexList(this, 6); + attackAssists = new HexList(this, 6); } @Override @@ -57,13 +61,13 @@ public abstract class Map extends Board public void clearAll() { - clearPointVector(moveAssists);; - clearPointVector(attackAssists); + possibleMoves.clear(); + possibleTargets.clear(); + moveAssists.clear(); + attackAssists.clear(); activablePawns.clear(); activatedPawns.clear(); clearPointSet(possiblePaths); - clearPointVector(possibleMoves); - clearPointVector(possibleTargets); clearCoordinateVector(finalPath); } @@ -74,7 +78,7 @@ public abstract class Map extends Board public void clearPossibleTargets() { - clearPointVector(possibleTargets); + possibleTargets.clear(); } public void clearActivablePawns() @@ -99,7 +103,9 @@ public abstract class Map extends Board public GridPoint2 getFirstMoveAssist() { - return moveAssists.get(0); + // FIXME + Hex h = (Hex) moveAssists.get(0); + return new GridPoint2(h.getCol(), h.getRow()); } public int activablePawnsCount() @@ -119,17 +125,17 @@ public abstract class Map extends Board public boolean isInPossibleMoves(GridPoint2 hex) { - return possibleMoves.contains(hex); + return possibleMoves.contains(getHex(hex.x, hex.y)); } public boolean isInPossibleMoveAssists(GridPoint2 hex) { - return moveAssists.contains(hex); + return moveAssists.contains(getHex(hex.x, hex.y)); } public boolean isInPossibleAttackAssists(GridPoint2 hex) { - return attackAssists.contains(hex); + return attackAssists.contains(getHex(hex.x, hex.y)); } public boolean isInPossiblePaths(GridPoint2 hex) @@ -139,7 +145,7 @@ public abstract class Map extends Board public boolean isInPossibleTargets(GridPoint2 hex) { - return possibleTargets.contains(hex); + return possibleTargets.contains(getHex(hex.x, hex.y)); } public void selectHex(GridPoint2 hex, boolean enable) @@ -159,28 +165,24 @@ public abstract class Map extends Board public void showPossibleMoves(boolean enable) { - for(GridPoint2 hex : possibleMoves) - enableOverlayOn(hex, Hex.MOVE1, enable); + possibleMoves.enable(Hex.MOVE1, enable); } public void showMoveAssists(boolean enable) { - for(GridPoint2 hex : moveAssists) - enableOverlayOn(hex, Hex.ASSIST, enable); + moveAssists.enable(Hex.ASSIST, enable); } public void showAttackAssists(boolean enable) { - for(GridPoint2 hex : attackAssists) { - enableOverlayOn(hex, Hex.ASSIST, enable); - enableOverlayOn(hex, Hex.TARGET, false); - } + attackAssists.enable(Hex.ASSIST, enable); + // TODO why the above ??? + attackAssists.enable(Hex.TARGET, false); } public void showPossibleTargets(boolean enable) { - for(GridPoint2 hex : possibleTargets) - enableOverlayOn(hex, Hex.TARGET, enable); + possibleTargets.enable(Hex.TARGET, enable); } public void showPossiblePaths(boolean enable, boolean keepFinal) @@ -241,7 +243,7 @@ public abstract class Map extends Board public int buildMoveAssists(Pawn pawn, GridPoint2 hex) { if (!pawn.isHq()) { - clearPointVector(moveAssists); + moveAssists.clear(); return 0; } return buildMoveAssists(pawn, hex, moveAssists); @@ -251,8 +253,7 @@ public abstract class Map extends Board { int s = buildAttackAssists(pawn, target, hex, units, attackAssists); activatedPawns.add(pawn); - for (GridPoint2 p : attackAssists) - activablePawns.add(getTopPawnAt(p)); + attackAssists.getPawns(activablePawns); return s; } @@ -278,8 +279,7 @@ public abstract class Map extends Board buildPossibleMoves(pawn, hex); buildMoveAssists(pawn, hex); activablePawns.add(pawn); - for (GridPoint2 p : moveAssists) - activablePawns.add(getTopPawnAt(p)); + moveAssists.getPawns(activablePawns); showPossibleMoves(true); showMoveAssists(true); } @@ -390,7 +390,7 @@ public abstract class Map extends Board } private int finishMove(Pawn pawn, GridPoint2 from) { - moveAssists.remove(from); + moveAssists.remove(getHex(from.x, from.y)); activablePawns.remove(pawn); activatedPawns.add(pawn); return activablePawns.size(); -- cgit v1.1-2-g2b99