diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-08 11:18:28 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-08 11:18:28 +0200 |
commit | 39a71bad998277f15c99fdc53c2c8673689e6417 (patch) | |
tree | 6e7ce3b8f81fecc3fdfa5529aeecaa88762d7b83 /core/src/ch/asynk/gdx/boardgame/boards | |
parent | b6ae9fddbe9c1d611691257ead8c78a47c41cf0b (diff) | |
download | gdx-boardgame-39a71bad998277f15c99fdc53c2c8673689e6417.zip gdx-boardgame-39a71bad998277f15c99fdc53c2c8673689e6417.tar.gz |
ch.asynk.gdx.boardgame.board -> ch.asynk.gdx.boardgame.boards
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame/boards')
5 files changed, 326 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java new file mode 100644 index 0000000..ab12ce6 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java @@ -0,0 +1,9 @@ +package ch.asynk.gdx.boardgame.boards; + +import com.badlogic.gdx.math.Vector2; + +public interface Board +{ + 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/BoardFactory.java b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java new file mode 100644 index 0000000..ff5ba08 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java @@ -0,0 +1,40 @@ +package ch.asynk.gdx.boardgame.boards; + +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/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java new file mode 100644 index 0000000..cd66e5a --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -0,0 +1,134 @@ +package ch.asynk.gdx.boardgame.boards; + +import com.badlogic.gdx.math.Vector2; + +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/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java new file mode 100644 index 0000000..8ad98f7 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java @@ -0,0 +1,37 @@ +package ch.asynk.gdx.boardgame.boards; + +import com.badlogic.gdx.math.Vector2; + +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/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java new file mode 100644 index 0000000..dcb6324 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -0,0 +1,106 @@ +package ch.asynk.gdx.boardgame.boards; + +import com.badlogic.gdx.math.Vector2; + +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 + private final float h13; // 1/3 height of the triangle + private final float h23; // 2/3 height of the triangle + private final float h43; // 4/3 height of the triangle + + 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; + this.h13 = this.h * 0.33333f; + this.h23 = this.h * 0.66666f; + this.h43 = this.h * 1.33333f; + } + + @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 += ((x * this.h) + (((x + y) % 2 == 0) ? this.h23 : this.h13)); + } else { + cx += (this.d + (x * this.d)); + cy += ((y * this.h) + (((x + y) % 2 == 0) ? this.h13 : this.h23)); + } + + 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); + } +} |