summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2015-01-16 12:55:03 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2015-01-16 12:55:03 +0100
commit47bcd4d0a06f5b41e021a083ad779a1f24263546 (patch)
tree12fa3662c326a34786f773202297274e62d1988a /core
parentdeca688c17bacf8c57eee2f26564dbdec030b2c0 (diff)
downloadRustAndDust-47bcd4d0a06f5b41e021a083ad779a1f24263546.zip
RustAndDust-47bcd4d0a06f5b41e021a083ad779a1f24263546.tar.gz
Path: Poolable extracted out of PossiblePaths
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/tankontank/engine/Path.java57
-rw-r--r--core/src/ch/asynk/tankontank/engine/PossiblePaths.java29
2 files changed, 61 insertions, 25 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/Path.java b/core/src/ch/asynk/tankontank/engine/Path.java
new file mode 100644
index 0000000..7c6a75f
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/engine/Path.java
@@ -0,0 +1,57 @@
+package ch.asynk.tankontank.engine;
+
+import java.util.ArrayList;
+
+import com.badlogic.gdx.utils.Pool;
+import com.badlogic.gdx.utils.Disposable;
+
+public class Path implements Disposable, Pool.Poolable
+{
+ public int cost;
+ public boolean roadMarch;
+ public ArrayList<Tile> tiles;
+
+ public Path()
+ {
+ this.cost = -1;
+ this.roadMarch = true;
+ this.tiles = null;
+ }
+
+ private void init(int size)
+ {
+ if (tiles == null)
+ tiles = new ArrayList<Tile>(size);
+ else
+ tiles. ensureCapacity(size);
+ }
+
+ @Override
+ public void reset()
+ {
+ cost = -1;
+ roadMarch = true;
+ tiles.clear();
+ }
+
+ @Override
+ public void dispose()
+ {
+ tiles.clear();
+ pathPool.free(this);
+ }
+
+ private static final Pool<Path> pathPool = new Pool<Path>() {
+ @Override
+ protected Path newObject() {
+ return new Path();
+ }
+ };
+
+ public static Path get(int size)
+ {
+ Path p = pathPool.obtain();
+ p.init(size);
+ return p;
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/engine/PossiblePaths.java b/core/src/ch/asynk/tankontank/engine/PossiblePaths.java
index da104f7..6b64879 100644
--- a/core/src/ch/asynk/tankontank/engine/PossiblePaths.java
+++ b/core/src/ch/asynk/tankontank/engine/PossiblePaths.java
@@ -12,27 +12,6 @@ import com.badlogic.gdx.math.Vector3;
public class PossiblePaths implements Iterable<Vector3>
{
- public class Path
- {
- public int cost;
- public boolean roadMarch;
- public ArrayList<Tile> tiles;
-
- public Path(int size)
- {
- this.cost = -1;
- this.roadMarch = true;
- this.tiles = new ArrayList<Tile>(size);
- }
-
- public void clear()
- {
- this.cost = -1;
- this.roadMarch = true;
- this.tiles.clear();
- }
- }
-
private final Board board;
public Pawn pawn;
@@ -80,8 +59,8 @@ public class PossiblePaths implements Iterable<Vector3>
this.to = null;
this.distance = -1;
this.orientation = Orientation.KEEP;
- for (Path path : this.paths) path.clear();
- for (Path path : this.filteredPaths) path.clear();
+ for (Path path : this.paths) path.dispose();
+ for (Path path : this.filteredPaths) path.dispose();
this.tiles.clear();
this.stack.clear();
this.ctrlTiles.clear();
@@ -115,7 +94,7 @@ public class PossiblePaths implements Iterable<Vector3>
this.distance = board.distance(from, to);
if (distance < 2) {
Orientation o = Orientation.fromMove(to.col, to.row, from.col, from.row);
- Path path = new Path(0);
+ Path path = Path.get(0);
path.roadMarch = to.road(o);
path.cost = to.costFrom(pawn, o);
paths.add(path);
@@ -145,7 +124,7 @@ public class PossiblePaths implements Iterable<Vector3>
if ((board.distance(next, to) <= l)) {
if (next == to) {
- Path path = new Path(stack.size() + 1);
+ Path path = Path.get(stack.size() + 1);
for (Tile t: stack) {
path.tiles.add(t);
tiles.add(t);