summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/engine
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/ch/asynk/tankontank/engine')
-rw-r--r--core/src/ch/asynk/tankontank/engine/Board.java102
-rw-r--r--core/src/ch/asynk/tankontank/engine/Pawn.java11
-rw-r--r--core/src/ch/asynk/tankontank/engine/SearchBoard.java131
3 files changed, 10 insertions, 234 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java
index e446365..60a3611 100644
--- a/core/src/ch/asynk/tankontank/engine/Board.java
+++ b/core/src/ch/asynk/tankontank/engine/Board.java
@@ -1,11 +1,10 @@
package ch.asynk.tankontank.engine;
-import java.util.Set;
import java.util.List;
-import java.util.ArrayList;
import java.util.Iterator;
-import java.util.LinkedHashSet;
import java.util.Collection;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
import com.badlogic.gdx.Gdx;
@@ -28,8 +27,6 @@ import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation;
public abstract class Board implements Disposable
{
private final Tile neighbours[] = new Tile[6];
- protected List<ArrayList<SearchBoard.Node>> paths;
- private final ArrayList<Vector3> finalPath = new ArrayList<Vector3>(10);
public interface TileBuilder
{
@@ -140,9 +137,6 @@ public abstract class Board implements Disposable
for (int i = 0, n = animations.size(); i < n; i++)
animations.get(i).dispose();
animations.clear();
- for (Vector3 v : finalPath)
- vector3Pool.free(v);
- finalPath.clear();
}
public float getWidth()
@@ -322,91 +316,6 @@ public abstract class Board implements Disposable
return assists.size();
}
- private int nodesToSet(List<ArrayList<SearchBoard.Node>> nodes, TileCollection tiles)
- {
- tiles.clear();
-
- for (ArrayList<SearchBoard.Node> path : nodes) {
- for (int i = 1, n = (path.size() - 1); i < n; i++) {
- SearchBoard.Node node = path.get(i);
- Tile tile = getTile(node.col, node.row);
- if (!tiles.contains(tile))
- tiles.add(tile);
- }
- }
-
- return nodes.size();
- }
-
- protected int collectPossiblePaths(Pawn pawn, Tile to, TileCollection tiles)
- {
- Tile from = pawn.getTile();
- paths = searchBoard.possiblePaths(pawn, from.getCol(), from.getRow(), to.getCol(), to.getRow());
- return nodesToSet(paths, tiles);
- }
-
- protected int possiblePathsFilterToggle(Tile tile, TileCollection tiles)
- {
- paths = searchBoard.possiblePathsFilterToggle(tile.getCol(), tile.getRow());
- return nodesToSet(paths, tiles);
- }
-
- protected int getPathCost(Pawn pawn, int i)
- {
- return searchBoard.pathCost(pawn, paths.get(i));
- }
-
- protected int getCoordinatePath(Pawn pawn, int idx, ArrayList<Vector3> path, Orientation finalOrientation)
- {
- for (Vector3 v : path)
- vector3Pool.free(v);
- path.clear();
-
- Vector2 tmpCoords = new Vector2();
-
- Vector3 p = pawn.getPosition();
- Vector3 v = vector3Pool.obtain();
- v.set(p.x, p.y, 0f);
- Orientation prevOrientation = pawn.getOrientation();
-
- ArrayList<SearchBoard.Node> nodes = paths.get(idx);
- SearchBoard.Node prevNode = nodes.get(0);
- // Gdx.app.debug("Board", "getCoordinatePath()");
- // Gdx.app.debug("Board", " " + prevNode);
-
- for (int i = 1, n = nodes.size(); i < n; i++) {
- SearchBoard.Node node = nodes.get(i);
- // Gdx.app.debug("Board", " " + node);
- Orientation o = Orientation.fromMove(prevNode.col, prevNode.row, node.col, node.row);
- if ((o != Orientation.KEEP) && (o != prevOrientation)) {
- v.z = o.r();
- path.add(v);
- v = vector3Pool.obtain();
- }
- pawn.getPosAt(getTile(node.col, node.row), tmpCoords);
- v.set(tmpCoords.x, tmpCoords.y, o.r());
- path.add(v);
- prevOrientation = o;
- v = vector3Pool.obtain();
- v.set(tmpCoords.x, tmpCoords.y, 0f);
-
- prevNode = node;
- }
-
- if (finalOrientation != prevOrientation) {
- v.z = finalOrientation.r();
- path.add(v);
- } else {
- vector3Pool.free(v);
- }
-
- // Gdx.app.debug("Board", " =>");
- // for (Vector3 vector :path)
- // Gdx.app.debug("Board", " " + vector);
-
- return path.size();
- }
-
public void enableOverlayOn(Tile tile, int i, boolean enable)
{
if(tile.enableOverlay(i, enable))
@@ -450,12 +359,11 @@ public abstract class Board implements Disposable
return pawn;
}
- protected void movePawn(final Pawn pawn, int cost, Orientation o, RunnableAnimation whenDone)
+ protected void movePawn(final Pawn pawn, PossiblePaths possiblePaths, RunnableAnimation whenDone)
{
- getCoordinatePath(pawn, 0, finalPath, o);
removePawn(pawn);
- AnimationSequence seq = pawn.getMoveAnimation(finalPath, 2);
+ AnimationSequence seq = pawn.getMoveAnimation(possiblePaths.iterator(), possiblePaths.pathSteps(0) + 2);
seq.addAnimation(RunnableAnimation.get(pawn, new Runnable() {
@Override
public void run() {
@@ -466,7 +374,7 @@ public abstract class Board implements Disposable
}));
seq.addAnimation(whenDone);
addAnimation(seq);
- pawn.move(cost);
+ pawn.move(possiblePaths.pathCost(0));
}
protected void rotatePawn(final Pawn pawn, Orientation o, RunnableAnimation whenDone)
diff --git a/core/src/ch/asynk/tankontank/engine/Pawn.java b/core/src/ch/asynk/tankontank/engine/Pawn.java
index db54f36..38510e2 100644
--- a/core/src/ch/asynk/tankontank/engine/Pawn.java
+++ b/core/src/ch/asynk/tankontank/engine/Pawn.java
@@ -1,7 +1,6 @@
package ch.asynk.tankontank.engine;
-import java.util.ArrayList;
-import java.util.ArrayDeque;
+import java.util.Iterator;
import com.badlogic.gdx.utils.Disposable;
@@ -246,12 +245,12 @@ public abstract class Pawn implements Moveable, Disposable
return seq;
}
- public AnimationSequence getMoveAnimation(ArrayList<Vector3> path, int size)
+ public AnimationSequence getMoveAnimation(Iterator<Vector3> vectors, int size)
{
prevPosition.set(position);
- AnimationSequence seq = AnimationSequence.get(path.size() + size);
- for (Vector3 v : path)
- seq.addAnimation(MoveToAnimation.get(this, v, MOVE_TIME));
+ AnimationSequence seq = AnimationSequence.get(size);
+ while (vectors.hasNext())
+ seq.addAnimation(MoveToAnimation.get(this, vectors.next(), MOVE_TIME));
return seq;
}
diff --git a/core/src/ch/asynk/tankontank/engine/SearchBoard.java b/core/src/ch/asynk/tankontank/engine/SearchBoard.java
index 55e9113..632dfdd 100644
--- a/core/src/ch/asynk/tankontank/engine/SearchBoard.java
+++ b/core/src/ch/asynk/tankontank/engine/SearchBoard.java
@@ -41,14 +41,8 @@ public class SearchBoard
private ArrayDeque<Node> stack;
private LinkedList<Node> queue;
private ArrayDeque<Node> roadMarch;
-
- private List<Node> targets;
private List<Node> los;
- private List<Node> path;
- private List<ArrayList<Node>> possiblePaths;
- private List<Node> possiblePathsFilters;
-
public SearchBoard(Board board, int cols, int rows)
{
this.cols = cols;
@@ -66,13 +60,7 @@ public class SearchBoard
this.queue = new LinkedList<Node>();
this.stack = new ArrayDeque<Node>(20);
this.roadMarch = new ArrayDeque<Node>(5);
-
- this.targets = new ArrayList<Node>(10);
this.los = new ArrayList<Node>(10);
-
- this.path = new ArrayList<Node>(20);
- this.possiblePaths = new LinkedList<ArrayList<Node>>();
- this.possiblePathsFilters = new ArrayList<Node>(5);
}
// FIXME be carefull with this if I expand the size of the board to be bigger than the playable size
@@ -547,123 +535,4 @@ public class SearchBoard
return los;
}
-
- public void clearPossiblePaths()
- {
- path.clear();
- for (List<Node> v : possiblePaths)
- v.clear();
- possiblePaths.clear();
- possiblePathsFilters.clear();
- }
-
- public List<ArrayList<Node>> possiblePathsFilterToggle(int col, int row)
- {
- Node n = getNode(col, row);
- if (possiblePathsFilters.contains(n))
- possiblePathsFilters.remove(n);
- else
- possiblePathsFilters.add(n);
- return possiblePaths();
- }
-
- public List<ArrayList<Node>> possiblePaths()
- {
- int s = possiblePathsFilters.size();
-
- List<ArrayList<Node>> paths = new LinkedList<ArrayList<Node>>();
- for (ArrayList<Node> path : possiblePaths) {
- int ok = 0;
- for (Node filter : possiblePathsFilters) {
- if (path.contains(filter))
- ok += 1;
- }
- if (ok == s) {
- if (path.size() == (s + 2)) {
- paths.clear();
- paths.add(path);
- return paths;
- } else
- paths.add(path);
- }
- }
-
- return paths;
- }
-
- public List<ArrayList<Node>> possiblePaths(Pawn pawn, int col0, int row0, int col1, int row1)
- {
- clearPossiblePaths();
-
- Node from = getNode(col0, row0);
- Node to = getNode(col1, row1);
-
- if (distance(from, to) == 1) {
- ArrayList<Node> temp = new ArrayList<Node>(2);
- temp.add(from);
- temp.add(to);
- possiblePaths.add(temp);
- } else {
- path.add(from);
- findAllPaths(pawn, from, to, pawn.getMovementPoints(), true, pawn.getRoadMarchBonus());
- }
-
- return possiblePaths;
- }
-
- private void findAllPaths(Pawn pawn, Node from, Node to, int mvtLeft, boolean roadMarch, int roadMarchBonus)
- {
- Node moves[] = new Node[6];
- adjacentMoves(from, moves);
-
- for(int i = 0; i < 6; i++) {
- Node next = moves[i];
- if (next == null) continue;
-
- Tile t = getTile(next);
- boolean road = t.road(board.getSide(i));
- int cost = t.costFrom(pawn, board.getSide(i), road);
- int r = (mvtLeft - cost);
- if (roadMarch & road) r += roadMarchBonus;
-
- if ((distance(next, to) <= r)) {
- if (next == to) {
- ArrayList<Node> temp = new ArrayList<Node>(path.size() + 1);
- for (Node n: path)
- temp.add(n);
- temp.add(next);
- possiblePaths.add(temp);
- } else {
- path.add(next);
- findAllPaths(pawn, next, to, (mvtLeft - cost), (roadMarch & road), roadMarchBonus);
- path.remove(path.size() - 1);
- }
- }
- }
- }
-
- public int pathCost(Pawn pawn, List<Node> path)
- {
- int cost = 0;
- boolean roadMarch = true;
- Node prev = null;
-
- for (Node next : path) {
- if (prev != null) {
- Orientation o = Orientation.fromMove(next.col, next.row, prev.col, prev.row);
- Tile t = getTile(next);
- boolean road = t.road(o);
- cost += t.costFrom(pawn, o, road);
- roadMarch &= road;
- }
- prev = next;
- }
-
- if (roadMarch)
- cost -= pawn.getRoadMarchBonus();
- if (cost < 1)
- cost = 1;
-
- return cost;
- }
}