From 4e20b4f3336fa2171159d0e92973923c800cdd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sat, 9 Jan 2016 12:40:24 +0100 Subject: Move: swallows PathIterator --- core/src/ch/asynk/rustanddust/engine/Move.java | 64 ++++++++++++++++- .../ch/asynk/rustanddust/engine/PathIterator.java | 80 ---------------------- 2 files changed, 62 insertions(+), 82 deletions(-) delete mode 100644 core/src/ch/asynk/rustanddust/engine/PathIterator.java 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 +public class Move extends Path implements Iterable, Iterator { public enum MoveType { @@ -69,6 +70,14 @@ public class Move extends Path implements Iterable 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 } @Override + @SuppressWarnings("unchecked") public Iterator 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) 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 -{ - 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 path; - - public PathIterator(Pawn pawn, Tile from, Tile to, Orientation orientation, List 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(); - } -} -- cgit v1.1-2-g2b99