summaryrefslogtreecommitdiffstats
path: root/core/src/ch
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-09-14 16:11:36 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-09-14 16:11:36 +0200
commit90457d70af946143fee8d41e5a102db10f3013d2 (patch)
treec54bb40ec508ad54a2c08ef60f121c6777c3ec14 /core/src/ch
parentf908758ad74c6cba76b8888f90f170c4e04918f9 (diff)
downloadgdx-boardgame-90457d70af946143fee8d41e5a102db10f3013d2.zip
gdx-boardgame-90457d70af946143fee8d41e5a102db10f3013d2.tar.gz
add TriangleBoard
Diffstat (limited to 'core/src/ch')
-rw-r--r--core/src/ch/asynk/gdx/board/board/BoardFactory.java4
-rw-r--r--core/src/ch/asynk/gdx/board/board/TriangleBoard.java102
2 files changed, 105 insertions, 1 deletions
diff --git a/core/src/ch/asynk/gdx/board/board/BoardFactory.java b/core/src/ch/asynk/gdx/board/board/BoardFactory.java
index fc30e72..c4e1ea1 100644
--- a/core/src/ch/asynk/gdx/board/board/BoardFactory.java
+++ b/core/src/ch/asynk/gdx/board/board/BoardFactory.java
@@ -6,7 +6,7 @@ public class BoardFactory
{
public enum BoardType
{
- HEX, SQUARE,
+ HEX, SQUARE, TRIANGLE
}
public enum BoardOrientation
@@ -33,6 +33,8 @@ public class BoardFactory
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/board/board/TriangleBoard.java b/core/src/ch/asynk/gdx/board/board/TriangleBoard.java
new file mode 100644
index 0000000..eb1ccdf
--- /dev/null
+++ b/core/src/ch/asynk/gdx/board/board/TriangleBoard.java
@@ -0,0 +1,102 @@
+package ch.asynk.gdx.board.board;
+
+import com.badlogic.gdx.math.Vector2;
+
+import ch.asynk.gdx.board.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);
+ }
+}