diff options
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r-- | core/src/ch/asynk/zproject/engine/Board.java | 9 | ||||
-rw-r--r-- | core/src/ch/asynk/zproject/engine/board/BoardFactory.java | 38 | ||||
-rw-r--r-- | core/src/ch/asynk/zproject/engine/board/HexBoard.java | 148 |
3 files changed, 195 insertions, 0 deletions
diff --git a/core/src/ch/asynk/zproject/engine/Board.java b/core/src/ch/asynk/zproject/engine/Board.java new file mode 100644 index 0000000..b43f87e --- /dev/null +++ b/core/src/ch/asynk/zproject/engine/Board.java @@ -0,0 +1,9 @@ +package ch.asynk.zproject.engine; + +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/zproject/engine/board/BoardFactory.java b/core/src/ch/asynk/zproject/engine/board/BoardFactory.java new file mode 100644 index 0000000..a81e963 --- /dev/null +++ b/core/src/ch/asynk/zproject/engine/board/BoardFactory.java @@ -0,0 +1,38 @@ +package ch.asynk.zproject.engine.board; + +import ch.asynk.zproject.engine.Board; + +public class BoardFactory +{ + public enum BoardType + { + HEX, + } + + public enum BoardOrientation + { + VERTICAL, + HORIZONTAL, + } + + public static Board getBoard(BoardType boardType, int cols, int rows, float side) + { + return getBoard(boardType, cols, rows, side, 0f, 0f, BoardOrientation.VERTICAL); + } + + public static Board getBoard(BoardType boardType, int cols, int rows, float side, float x0, float y0) + { + return getBoard(boardType, cols, rows, side, x0, y0, BoardOrientation.VERTICAL); + } + + public static Board getBoard(BoardType boardType, int cols, int rows, float side, float x0, float y0, BoardOrientation boardOrientation) + { + switch(boardType) + { + case HEX: + return new HexBoard(cols, rows, 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/zproject/engine/board/HexBoard.java b/core/src/ch/asynk/zproject/engine/board/HexBoard.java new file mode 100644 index 0000000..24ce535 --- /dev/null +++ b/core/src/ch/asynk/zproject/engine/board/HexBoard.java @@ -0,0 +1,148 @@ +package ch.asynk.zproject.engine.board; + +import com.badlogic.gdx.math.Vector2; + +import ch.asynk.zproject.engine.Board; + +public class HexBoard implements Board +{ + private int cols; // # columns + private int rows; // # rows + private float side; // length of the side of the hex + private float x0; // bottom left x offset + private float y0; // bottom left y offset + private BoardFactory.BoardOrientation orientation; + + private float w; // side to side orthogonal distance + private float dw; // half hex : w/2 + private float dh; // hex top : s/2 + private float h; // square height : s + dh + private 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(int cols, int rows, float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation) + { + this.cols = cols; + this.rows = rows; + 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 < 0.f) { + 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 < 0.f) { + 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); + } +} |