summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/Piece.java4
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/Board.java8
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java34
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java16
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java16
5 files changed, 76 insertions, 2 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/Piece.java b/core/src/ch/asynk/gdx/boardgame/Piece.java
index c9bf6fe..5008d9a 100644
--- a/core/src/ch/asynk/gdx/boardgame/Piece.java
+++ b/core/src/ch/asynk/gdx/boardgame/Piece.java
@@ -29,7 +29,7 @@ public class Piece extends Sprite implements Drawable, Positionable, Rotable, Sc
public void getPosOn(Tile tile, Orientation orientation, Vector3 v)
{
- v.set((tile.x - (getWidth() / 2f)), (tile.y- (getHeight() / 2f)), orientation.r());
+ v.set((tile.x - (getWidth() / 2f)), (tile.y - (getHeight() / 2f)), orientation.r());
}
public void setPosition(float x, float y, float r)
@@ -42,7 +42,7 @@ public class Piece extends Sprite implements Drawable, Positionable, Rotable, Sc
{
return (
(Math.abs(getX() - (tile.x - (getWidth() / 2f))) < 3) &&
- (Math.abs(getY() - (tile.y- (getHeight() / 2f))) < 3)
+ (Math.abs(getY() - (tile.y - (getHeight() / 2f))) < 3)
);
}
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
index 1037842..4043849 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
@@ -7,4 +7,12 @@ public interface Board
public int[] getAngles();
public void centerOf(int x, int y, Vector2 v);
public void toBoard(float x, float y, Vector2 v);
+ public float distance(int x0, int y0, int x1, int y1, Geometry geometry);
+
+ enum Geometry
+ {
+ EUCLIDEAN,
+ TAXICAB,
+ TCHEBYCHEV
+ }
}
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
index c78fb77..6a409ad 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
@@ -144,4 +144,38 @@ public class HexBoard implements Board
v.set(col, row);
}
+
+ @Override public float distance(int x0, int y0, int x1, int y1, Geometry geometry)
+ {
+ if (geometry == Board.Geometry.EUCLIDEAN) {
+ int dx = (x1 - x0);
+ int dy = (y1 - y0);
+
+ if (dx == 0) {
+ return Math.abs(dy);
+ }
+ else if (dy == 0 || dx == dy) {
+ return Math.abs(dx);
+ }
+ float fdx = dx - dy / 2f;
+ float fdy = dy * 0.86602f;
+ return (float)Math.sqrt((fdx * fdx) + (fdy * fdy));
+ } else {
+ int dx = Math.abs(x1 - x0);
+ int dy = Math.abs(y1 - y0);
+ int dz = Math.abs((x0 - y0) - (x1 - y1));
+
+ if (dx > dy) {
+ if (dx > dz)
+ return dx;
+ else
+ return dz;
+ } else {
+ if (dy > dz)
+ return dy;
+ else
+ return dz;
+ }
+ }
+ }
}
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
index 665d1ca..5a7d2b8 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
@@ -42,4 +42,20 @@ public class SquareBoard implements Board
v.set(col, row);
}
+
+ @Override public float distance(int x0, int y0, int x1, int y1, Geometry geometry)
+ {
+ int dx = Math.abs(x1 - x0);
+ int dy = Math.abs(y1 - y0);
+
+ switch (geometry) {
+ case EUCLIDEAN:
+ return (float)Math.sqrt((dx * dx) + (dy * dy));
+ case TAXICAB:
+ return dx + dy;
+ case TCHEBYCHEV:
+ return (dx > dy ? dx : dy);
+ }
+ return -1;
+ }
}
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
index 1fbd034..4be04f3 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
@@ -116,4 +116,20 @@ public class TriangleBoard implements Board
v.set(col, row);
}
+
+ @Override public float distance(int x0, int y0, int x1, int y1, Geometry geometry)
+ {
+ int dx = Math.abs(x1 - x0);
+ int dy = Math.abs(y1 - y0);
+
+ switch (geometry) {
+ case EUCLIDEAN:
+ return 0; // FIXME
+ case TAXICAB:
+ return dx + dy; // move should only be allowed through the 3 sides not through de vertices
+ case TCHEBYCHEV:
+ return dx + dy;
+ }
+ return -1;
+ }
}