summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-01-09 12:40:24 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-01-09 12:40:24 +0100
commit4e20b4f3336fa2171159d0e92973923c800cdd83 (patch)
tree1e70b14b63f519149e1e3a3f6baeb80b0b4e52c4 /core/src/ch/asynk
parent648a5e0ab3b30d1c0c395059f1f4bbb5d31bfb81 (diff)
downloadRustAndDust-4e20b4f3336fa2171159d0e92973923c800cdd83.zip
RustAndDust-4e20b4f3336fa2171159d0e92973923c800cdd83.tar.gz
Move: swallows PathIterator
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r--core/src/ch/asynk/rustanddust/engine/Move.java64
-rw-r--r--core/src/ch/asynk/rustanddust/engine/PathIterator.java80
2 files changed, 62 insertions, 82 deletions
diff --git a/core/src/ch/asynk/rustanddust/engine/Move.java b/core/src/ch/asynk/rustanddust/engine/Move.java
index 077823f..09e7330 100644
--- a/core/src/ch/asynk/rustanddust/engine/Move.java
+++ b/core/src/ch/asynk/rustanddust/engine/Move.java
@@ -3,9 +3,10 @@ package ch.asynk.rustanddust.engine;
import java.util.Iterator;
import com.badlogic.gdx.utils.Pool;
+import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
-public class Move extends Path implements Iterable<Vector3>
+public class Move extends Path implements Iterable<Vector3>, Iterator
{
public enum MoveType
{
@@ -69,6 +70,14 @@ public class Move extends Path implements Iterable<Vector3>
public Tile to;
public Orientation orientation;
public MoveType type;
+ // iterator
+ private int i;
+ private int s;
+ private Tile t;
+ private boolean r;
+ private Orientation o;
+ private Vector3 v = new Vector3();
+ private Vector2 pos = new Vector2();
public Move()
{
@@ -151,8 +160,59 @@ public class Move extends Path implements Iterable<Vector3>
}
@Override
+ @SuppressWarnings("unchecked")
public Iterator<Vector3> iterator()
{
- return new PathIterator(pawn, from, to, orientation, tiles);
+ this.i = 0;
+ this.s = tiles.size();
+ this.t = from;
+ this.r = (from == to);
+ this.o = pawn.getOrientation();
+ this.v.set(pawn.getPosition().x, pawn.getPosition().y, o.r());
+ return (Iterator<Vector3>) this;
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ if ((r || (i > s)) && (o == orientation))
+ return false;
+ return true;
+ }
+
+ @Override
+ public Vector3 next()
+ {
+ if (!hasNext())
+ throw new java.util.NoSuchElementException();
+
+ if (r || (i > s)) {
+ v.z = orientation.r();
+ o = orientation;
+ return v;
+ }
+ Tile nextTile;
+ if (i < s)
+ nextTile = tiles.get(i);
+ else
+ nextTile = to;
+ Orientation nextO = Orientation.fromMove(t.col, t.row, nextTile.col, nextTile.row);
+ if (nextO != o) {
+ v.z = nextO.r();
+ o = nextO;
+ return v;
+ }
+ pawn.getPosAt(nextTile, pos);
+ v.x = pos.x;
+ v.y = pos.y;
+ t = nextTile;
+ i += 1;
+ return v;
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
}
}
diff --git a/core/src/ch/asynk/rustanddust/engine/PathIterator.java b/core/src/ch/asynk/rustanddust/engine/PathIterator.java
deleted file mode 100644
index 099c65e..0000000
--- a/core/src/ch/asynk/rustanddust/engine/PathIterator.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package ch.asynk.rustanddust.engine;
-
-import java.util.List;
-import java.util.Iterator;
-
-import com.badlogic.gdx.math.Vector2;
-import com.badlogic.gdx.math.Vector3;
-
-public class PathIterator implements Iterator<Vector3>
-{
- private Pawn pawn;
- private boolean rotation;
- private Tile to;
- private Orientation o;
- private Orientation orientation;
- private Tile tile;
- private Vector2 pos = new Vector2();
- private Vector3 v = new Vector3();
- private int i;
- private int s;
- private List<Tile> path;
-
- public PathIterator(Pawn pawn, Tile from, Tile to, Orientation orientation, List<Tile> path)
- {
- this.pawn = pawn;
- this.to = to;
- this.tile = from;
- this.rotation = (from == to);
- this.orientation = orientation;
- this.path = path;
- this.o = pawn.getOrientation();
- this.v.set(pawn.getPosition().x, pawn.getPosition().y, o.r());
- this.i = 0;
- this.s = path.size();
- }
-
- @Override
- public boolean hasNext()
- {
- if ((rotation || (i > s)) && (o == orientation))
- return false;
- return true;
- }
-
- @Override
- public Vector3 next()
- {
- if (!hasNext())
- throw new java.util.NoSuchElementException();
-
- if (rotation || (i > s)) {
- v.z = orientation.r();
- o = orientation;
- return v;
- }
- Tile nextTile;
- if (i < s)
- nextTile = path.get(i);
- else
- nextTile = to;
- Orientation nextO = Orientation.fromMove(tile.col, tile.row, nextTile.col, nextTile.row);
- if (nextO != o) {
- v.z = nextO.r();
- o = nextO;
- return v;
- }
- pawn.getPosAt(nextTile, pos);
- v.x = pos.x;
- v.y = pos.y;
- tile = nextTile;
- i += 1;
- return v;
- }
-
- @Override
- public void remove()
- {
- throw new UnsupportedOperationException();
- }
-}