summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/tabletop/board
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-09-15 15:38:09 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-09-15 15:38:09 +0200
commit57cddbf4b79b6c342e53acfa804d01f94d9bd266 (patch)
tree9fcc89fecc3166ff19a7ab90b14f1a4111903df4 /core/src/ch/asynk/gdx/tabletop/board
parent898a7c34189e2b7d52c794c93e9fa0aeb759ca64 (diff)
downloadgdx-boardgame-57cddbf4b79b6c342e53acfa804d01f94d9bd266.zip
gdx-boardgame-57cddbf4b79b6c342e53acfa804d01f94d9bd266.tar.gz
namespecat gdx.board -> gdx.tabletop
Diffstat (limited to 'core/src/ch/asynk/gdx/tabletop/board')
-rw-r--r--core/src/ch/asynk/gdx/tabletop/board/BoardFactory.java42
-rw-r--r--core/src/ch/asynk/gdx/tabletop/board/HexBoard.java136
-rw-r--r--core/src/ch/asynk/gdx/tabletop/board/SquareBoard.java39
-rw-r--r--core/src/ch/asynk/gdx/tabletop/board/TriangleBoard.java102
4 files changed, 319 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/tabletop/board/BoardFactory.java b/core/src/ch/asynk/gdx/tabletop/board/BoardFactory.java
new file mode 100644
index 0000000..e3d6195
--- /dev/null
+++ b/core/src/ch/asynk/gdx/tabletop/board/BoardFactory.java
@@ -0,0 +1,42 @@
+package ch.asynk.gdx.tabletop.board;
+
+import ch.asynk.gdx.tabletop.Board;
+
+public class BoardFactory
+{
+ public enum BoardType
+ {
+ HEX, SQUARE, TRIANGLE
+ }
+
+ public enum BoardOrientation
+ {
+ VERTICAL,
+ HORIZONTAL,
+ }
+
+ public static Board getBoard(BoardType boardType, float side)
+ {
+ return getBoard(boardType, side, 0f, 0f, BoardOrientation.VERTICAL);
+ }
+
+ public static Board getBoard(BoardType boardType, float side, float x0, float y0)
+ {
+ return getBoard(boardType, side, x0, y0, BoardOrientation.VERTICAL);
+ }
+
+ public static Board getBoard(BoardType boardType, float side, float x0, float y0, BoardOrientation boardOrientation)
+ {
+ switch(boardType)
+ {
+ case HEX:
+ return new HexBoard(side, x0, y0, boardOrientation);
+ case SQUARE:
+ return new SquareBoard(side, x0, y0);
+ case TRIANGLE:
+ return new TriangleBoard(side, x0, y0, boardOrientation);
+ default:
+ throw new RuntimeException( String.format("%s board type is not implemented yet.", boardType) );
+ }
+ }
+}
diff --git a/core/src/ch/asynk/gdx/tabletop/board/HexBoard.java b/core/src/ch/asynk/gdx/tabletop/board/HexBoard.java
new file mode 100644
index 0000000..0fd8871
--- /dev/null
+++ b/core/src/ch/asynk/gdx/tabletop/board/HexBoard.java
@@ -0,0 +1,136 @@
+package ch.asynk.gdx.tabletop.board;
+
+import com.badlogic.gdx.math.Vector2;
+
+import ch.asynk.gdx.tabletop.Board;
+
+public class HexBoard implements Board
+{
+ private final float side; // length of the side of the hex
+ private final float x0; // bottom left x offset
+ private final float y0; // bottom left y offset
+ private final BoardFactory.BoardOrientation orientation;
+
+ private final float w; // side to side orthogonal distance
+ private final float dw; // half hex : w/2
+ private final float dh; // hex top : s/2
+ private final float h; // square height : s + dh
+ private final float slope; // dh / dw
+
+ // BoardOrientation.VERTICAL : 2 vertical sides : 2 vertices pointing up and down
+ // coordinates
+ // \
+ // \___
+ // cols are horizontal
+ // rows are at -120°
+ // bottom left is the bottom vertice of the most bottom-left vertical hex side of the map
+ //
+ // BoardOrientation.HORIZONTAL : 2 horizontal sides : 2 vertices pointing left and right
+ // coordinates
+ // |
+ // |
+ // \
+ // \
+ // cols are at +120°
+ // rows are vertical°
+ // bottom left is the left vertice of the most bottom-left horizontal hex side of the map
+
+ public HexBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)
+ {
+ this.side = side;
+ this.x0 = x0;
+ this.y0 = y0;
+ this.orientation = boardOrientation;
+
+ this.w = side * 1.73205f;
+ this.dw = w / 2.0f;
+ this.dh = side / 2.0f;
+ this.h = side + dh;
+ this.slope = dh / dw;
+ }
+
+ @Override public void centerOf(int x, int y, Vector2 v)
+ {
+ float cx = this.x0;
+ float cy = this.y0;
+
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ cx += (this.dw + (x * this.w) - (y * this.dw));
+ cy += (this.dh + (y * this.h));
+ } else {
+ cx += (this.dh + (x * this.h));
+ cy += (this.dw + (y * this.w) - (x * this.dw));
+ }
+
+ v.set(cx, cy);
+ }
+
+ @Override public void toBoard(float x, float y, Vector2 v)
+ {
+ int col = -1;
+ int row = -1;
+
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ // compute row
+ float dy = y - this.y0;
+ row = (int) (dy / this.h);
+ if (dy < 0f) row -= 1;
+
+ // compute col
+ float dx = x - this.x0 + (row * this.dw);
+ col = (int) (dx / this.w);
+ if (dx < 0f) col -= 1;
+
+ // upper rectangle or hex body
+ if (dy > ((row * this.h) + this.side)) {
+ dy -= ((row * this.h) + this.side);
+ dx -= (col * this.w);
+ // upper left or right rectangle
+ if (dx < this.dw) {
+ if (dy > (dx * this.slope)) {
+ // upper left hex
+ row += 1;
+ }
+ } else {
+ // if (dy > ((2 * this.dh) - (dx * this.slope))) {
+ if (dy > ((this.w - dx) * this.slope)) {
+ // upper right hex
+ row += 1;
+ col += 1;
+ }
+ }
+ }
+ } else {
+ // compute col
+ float dx = x - this.x0;
+ col = (int) (dx / this.h);
+ if (dx < 0f) col -= 1;
+
+ // compute row
+ float dy = y - this.y0 + (col * this.dw);
+ row = (int) (dy / this.w);
+ if (dy < 0f) row -= 1;
+
+ // right rectangle or hex body
+ if (dx > ((col * this.h) + this.side)) {
+ dx -= ((col * this.h) + this.side);
+ dy -= (row * this.w);
+ // upper or lower rectangle
+ if (dy > ((this.dw - dx) / this.slope)) {
+ if (dy > ((2 * this.dw) - (dx / this.slope))) {
+ // upper right hex
+ col += 1;
+ row += 1;
+ }
+ } else {
+ if (dy < (dx / this.slope)) {
+ // lower right hex
+ col += 1;
+ }
+ }
+ }
+ }
+
+ v.set(col, row);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/tabletop/board/SquareBoard.java b/core/src/ch/asynk/gdx/tabletop/board/SquareBoard.java
new file mode 100644
index 0000000..d0daf41
--- /dev/null
+++ b/core/src/ch/asynk/gdx/tabletop/board/SquareBoard.java
@@ -0,0 +1,39 @@
+package ch.asynk.gdx.tabletop.board;
+
+import com.badlogic.gdx.math.Vector2;
+
+import ch.asynk.gdx.tabletop.Board;
+
+public class SquareBoard implements Board
+{
+ private final float side; // length of the side of a square
+ private final float x0; // bottom left x offset
+ private final float y0; // bottom left y offset
+
+ public SquareBoard(float side, float x0, float y0)
+ {
+ this.side = side;
+ this.x0 = x0;
+ this.y0 = y0;
+ }
+
+ @Override public void centerOf(int x, int y, Vector2 v)
+ {
+ float cx = this.x0 + (this.side / 2) + (this.side * x);
+ float cy = this.y0 + (this.side / 2) + (this.side * y);
+
+ v.set(cx, cy);
+ }
+
+ @Override public void toBoard(float x, float y, Vector2 v)
+ {
+ float dx = x - this.x0;
+ float dy = y - this.y0;
+ int col = (int) (dx / this.side);
+ int row = (int) (dy / this.side);
+ if (dx < 0) col -=1;
+ if (dy < 0) row -=1;
+
+ v.set(col, row);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/tabletop/board/TriangleBoard.java b/core/src/ch/asynk/gdx/tabletop/board/TriangleBoard.java
new file mode 100644
index 0000000..961f552
--- /dev/null
+++ b/core/src/ch/asynk/gdx/tabletop/board/TriangleBoard.java
@@ -0,0 +1,102 @@
+package ch.asynk.gdx.tabletop.board;
+
+import com.badlogic.gdx.math.Vector2;
+
+import ch.asynk.gdx.tabletop.Board;
+
+public class TriangleBoard implements Board
+{
+ private final float side; // length of the side of the equilateral triangle
+ private final float x0; // bottom left x offset
+ private final float y0; // bottom left y offset
+ private final BoardFactory.BoardOrientation orientation;
+
+ private final float d; // side / 2
+ private final float h; // height of the triangle
+ private final float m; // h / d
+
+ public TriangleBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)
+ {
+ this.side = side;
+ this.x0 = x0;
+ this.y0 = y0;
+ this.orientation = boardOrientation;
+
+ this.d = side / 2f;
+ this.h = side * 0.866f;
+ this.m = this.h / this.d;
+ }
+
+ @Override public void centerOf(int x, int y, Vector2 v)
+ {
+ float cx = this.x0;
+ float cy = this.y0;
+
+ if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
+ cy += (y * this.d);
+ cx += ((this.h * ( (y % 2 == 0) ? 0.33333f : 0.66666f )) + (x * this.h));
+ } else {
+ cx += (this.d + (x * this.d));
+ cy += ((this.h * ( (x % 2 == 0) ? 0.33333f : 0.66666f )) + (y * this.h));
+ }
+
+ v.set(cx, cy);
+ }
+
+ @Override public void toBoard(float x, float y, Vector2 v)
+ {
+ boolean vert = (this.orientation == BoardFactory.BoardOrientation.VERTICAL);
+
+ float dx = x - this.x0;
+ float dy = y - this.y0;
+ float cx = (vert ? this.h : this.d);
+ float cy = (vert ? this.d : this.h);
+
+ int col = (int) (dx / cx);
+ int row = (int) (dy / cy);
+ if (dx < 0) col -=1;
+ if (dy < 0) row -=1;
+ dx -= (col * cx);
+ dy -= (row * cy);
+
+ if (vert) {
+ if (col % 2 == 0) {
+ if (row % 2 == 0) {
+ if (dy > (dx / this.m))
+ row += 1;
+ } else {
+ if (dy + (dx / this.m) > d )
+ row += 1;
+ }
+ } else {
+ if (row % 2 == 0) {
+ if (dy + (dx / this.m) > d )
+ row += 1;
+ } else {
+ if (dy > (dx / this.m))
+ row += 1;
+ }
+ }
+ } else {
+ if (row % 2 == 0) {
+ if (col % 2 == 0) {
+ if (dy > (dx * this.m))
+ col -= 1;
+ } else {
+ if (dy + (dx * this.m) < h )
+ col -= 1;
+ }
+ } else {
+ if (col % 2 == 0) {
+ if (dy + (dx * this.m) < h )
+ col -= 1;
+ } else {
+ if (dy > (dx * this.m))
+ col -= 1;
+ }
+ }
+ }
+
+ v.set(col, row);
+ }
+}