summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-01-21 16:34:49 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2020-01-21 16:34:49 +0100
commit6c3a48e5bf3e9efa91995ecf5edf7fb0a1d2e2e6 (patch)
treec18d4d48370af3d44fd6a8c41159b598a5a160f3
parent7a6948222f59f23fe6326b15746f23e5b4ed823c (diff)
downloadgdx-boardgame-6c3a48e5bf3e9efa91995ecf5edf7fb0a1d2e2e6.zip
gdx-boardgame-6c3a48e5bf3e9efa91995ecf5edf7fb0a1d2e2e6.tar.gz
Board : add size() and getIdx(int, int)
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/Board.java2
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java37
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java7
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java4
-rw-r--r--test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java2
5 files changed, 51 insertions, 1 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
index 2228e25..b1adf1c 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
@@ -5,6 +5,8 @@ import com.badlogic.gdx.math.Vector2;
public interface Board
{
public int[] getAngles();
+ public int size();
+ public int getIdx(int x, int y);
public boolean isOnMap(int x, int y);
public void centerOf(int x, int y, Vector2 v);
public void toBoard(float x, float y, Vector2 v);
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
index 226cdb9..00704ef 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
@@ -17,6 +17,8 @@ public class HexBoard implements Board
private final float h; // square height : s + dh
private final float slope; // dh / dw
+ private final int tl; // tiles in 2 consecutive lines
+
// BoardOrientation.VERTICAL : 2 vertical sides : 2 vertices pointing up and down
// coordinates
// \
@@ -53,6 +55,21 @@ public class HexBoard implements Board
this.dh = side / 2.0f;
this.h = side + dh;
this.slope = dh / dw;
+
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ this.tl = (2 * cols - 1);
+ } else {
+ this.tl = (2 * rows - 1);
+ }
+ }
+
+ @Override public int size()
+ {
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ return (rows / 2) * tl + ((rows % 2) * cols);
+ } else {
+ return (cols / 2) * tl + ((cols % 2) * rows);
+ }
}
@Override public int[] getAngles()
@@ -64,6 +81,26 @@ public class HexBoard implements Board
}
}
+ @Override public int getIdx(int x, int y)
+ {
+ if (!isOnMap(x, y)) return -1;
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ int n = y / 2;
+ int i = x - n + n * tl;
+ if ((y % 2) != 0) {
+ i += (cols - 1);
+ }
+ return i;
+ } else {
+ int n = x / 2;
+ int i = y - n + n * tl;
+ if ((x % 2) != 0) {
+ i += (rows - 1);
+ }
+ return i;
+ }
+ }
+
@Override public boolean isOnMap(int x, int y)
{
if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
index d16945c..7f5de42 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
@@ -22,11 +22,18 @@ public class SquareBoard implements Board
this.y0 = y0;
}
+ @Override public int size() { return cols * rows; }
+
@Override public int[] getAngles()
{
return angles;
}
+ @Override public int getIdx(int x, int y)
+ {
+ return (y * cols + x);
+ }
+
@Override public boolean isOnMap(int x, int y)
{
if (x < 0 || x >= cols || y < 0 || y >= rows) return false;
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
index bcb4a54..4c9a013 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
@@ -39,6 +39,10 @@ public class TriangleBoard implements Board
this.h43 = this.h * 1.33333f;
}
+ // FIXME size() getIdx(int, int)
+ @Override public int size() { return 1; }
+ @Override public int getIdx(int x, int y) { return -1; }
+
@Override public int[] getAngles()
{
if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java
index 37fdade..8bf1d50 100644
--- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java
+++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java
@@ -57,7 +57,7 @@ public class BoardScreen extends AbstractScreen
public boolean touch(float x, float y)
{
board.toBoard(x, y, v);
- GdxBoardTest.debug("BoardScreen", String.format("touchDown [%d;%d] => [%d;%d]", (int)x, (int)y, (int)v.x, (int)v.y));
+ GdxBoardTest.debug("BoardScreen", String.format("touchDown [%d;%d] => [%d;%d] => %d", (int)x, (int)y, (int)v.x, (int)v.y, board.getIdx((int)v.x, (int)v.y)));
float d0 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TCHEBYCHEV);
float d1 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TAXICAB);
float d2 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.EUCLIDEAN);