summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2014-10-03 01:58:54 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2014-10-03 01:58:54 +0200
commit6809c19db45d5cdb40ce28c52a8a86d94f890473 (patch)
tree2d38fd5db26240a40d02f2af7fd320261bb0c4b3 /core/src/ch/asynk/tankontank
parentd7967cb6fc4b87989ea5222f9ee0ba73f71efee4 (diff)
downloadRustAndDust-6809c19db45d5cdb40ce28c52a8a86d94f890473.zip
RustAndDust-6809c19db45d5cdb40ce28c52a8a86d94f890473.tar.gz
Board,SearchBoard: add getFinalPath(getFinalPath(Vector<GridPoint3> path)
Diffstat (limited to 'core/src/ch/asynk/tankontank')
-rw-r--r--core/src/ch/asynk/tankontank/engine/Board.java50
-rw-r--r--core/src/ch/asynk/tankontank/engine/SearchBoard.java4
2 files changed, 54 insertions, 0 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java
index d754883..9b05b9f 100644
--- a/core/src/ch/asynk/tankontank/engine/Board.java
+++ b/core/src/ch/asynk/tankontank/engine/Board.java
@@ -18,6 +18,7 @@ import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.GridPoint2;
+import com.badlogic.gdx.math.GridPoint3;
import com.badlogic.gdx.math.Matrix4;
import ch.asynk.tankontank.engine.gfx.Image;
@@ -134,6 +135,13 @@ public abstract class Board implements Disposable
}
};
+ private final Pool<GridPoint3> gridPoint3Pool = new Pool<GridPoint3>() {
+ @Override
+ protected GridPoint3 newObject() {
+ return new GridPoint3();
+ }
+ };
+
protected Config cfg;
private Tile[] tiles;
protected SearchBoard searchBoard;
@@ -393,6 +401,48 @@ public abstract class Board implements Disposable
return nodesToSet(paths, points);
}
+ private Orientation getOrientation(SearchBoard.Node from, SearchBoard.Node to)
+ {
+ int dx = to.col - from .col;
+ int dy = to.row - from.row;
+
+ if (dy == 0) {
+ if (dx > 0) return Orientation.NORTH;
+ return Orientation.SOUTH;
+ }
+ if (dy > 0) {
+ if (dx > 0) return Orientation.NORTH_WEST;
+ return Orientation.SOUTH_WEST;
+ } else {
+ if (dx > 0) return Orientation.NORTH_EAST;
+ return Orientation.SOUTH_EAST;
+ }
+ }
+
+ public int getFinalPath(Vector<GridPoint3> path)
+ {
+ List<Vector<SearchBoard.Node>> paths = searchBoard.possiblePaths();
+
+ for (GridPoint3 v : path)
+ gridPoint3Pool.free(v);
+ path.clear();
+
+ if (paths.size() != 1)
+ return 0;
+
+ Vector<SearchBoard.Node> nodes = paths.get(0);
+ SearchBoard.Node prev = nodes.get(nodes.size() - 1);
+ for (int i = (nodes.size() - 2); i >= 0; i--) {
+ SearchBoard.Node node = nodes.get(i);
+ GridPoint3 point = gridPoint3Pool.obtain();
+ point.set(node.col, node.row, getOrientation(prev, node).r);
+ path.add(point);
+ prev = node;
+ }
+
+ return path.size();
+ }
+
public boolean hasUnits(GridPoint2 hex)
{
return getTile(hex.x, hex.y).hasUnits();
diff --git a/core/src/ch/asynk/tankontank/engine/SearchBoard.java b/core/src/ch/asynk/tankontank/engine/SearchBoard.java
index 6b5ef26..0fd21e1 100644
--- a/core/src/ch/asynk/tankontank/engine/SearchBoard.java
+++ b/core/src/ch/asynk/tankontank/engine/SearchBoard.java
@@ -504,7 +504,11 @@ public class SearchBoard
possiblePathsFilters.remove(n);
else
possiblePathsFilters.add(n);
+ return possiblePaths();
+ }
+ public List<Vector<Node>> possiblePaths()
+ {
int s = possiblePathsFilters.size();
List<Vector<Node>> paths = new LinkedList<Vector<Node>>();