diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-01-21 09:31:16 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-01-21 09:31:16 +0100 |
commit | 3a153fe86fcd3ae224b7c6ddba1780eeed89ec8a (patch) | |
tree | a9ee37d934a8885c83e3398dd3dc03e943da5320 /core | |
parent | 5297877a607167c991364493f3e7fd283d1ead17 (diff) | |
download | gdx-boardgame-3a153fe86fcd3ae224b7c6ddba1780eeed89ec8a.zip gdx-boardgame-3a153fe86fcd3ae224b7c6ddba1780eeed89ec8a.tar.gz |
Board : add boolean isOnBoard(int, int)
Diffstat (limited to 'core')
5 files changed, 54 insertions, 11 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java index 4043849..2228e25 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2; public interface Board { public int[] getAngles(); + 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); public float distance(int x0, int y0, int x1, int y1, Geometry geometry); diff --git a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java index 54eaf38..7813de8 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java @@ -15,29 +15,29 @@ public class BoardFactory HORIZONTAL, } - public static Board getBoard(BoardType boardType, float side) + public static Board getBoard(int cols, int rows, BoardType boardType, float side) { - return getBoard(boardType, side, 0f, 0f, BoardOrientation.VERTICAL); + return getBoard(cols, rows, boardType, side, 0f, 0f, BoardOrientation.VERTICAL); } - public static Board getBoard(BoardType boardType, float side, float x0, float y0) + public static Board getBoard(int cols, int rows, BoardType boardType, float side, float x0, float y0) { - return getBoard(boardType, side, x0, y0, BoardOrientation.VERTICAL); + return getBoard(cols, rows, boardType, side, x0, y0, BoardOrientation.VERTICAL); } - public static Board getBoard(BoardType boardType, float side, float x0, float y0, BoardOrientation boardOrientation) + public static Board getBoard(int cols, int rows, BoardType boardType, float side, float x0, float y0, BoardOrientation boardOrientation) { Board board = null; switch(boardType) { case HEX: - board = new HexBoard(side, x0, y0, boardOrientation); + board = new HexBoard(cols, rows, side, x0, y0, boardOrientation); break; case SQUARE: - board = new SquareBoard(side, x0, y0); + board = new SquareBoard(cols, rows, side, x0, y0); break; case TRIANGLE: - board = new TriangleBoard(side, x0, y0, boardOrientation); + board = new TriangleBoard(cols, rows, side, x0, y0, boardOrientation); break; } if (board == null) { diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index 6a409ad..226cdb9 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -9,6 +9,8 @@ public class HexBoard implements Board private final float y0; // bottom left y offset private final BoardFactory.BoardOrientation orientation; + private final int cols; // # colmuns + private final int rows; // # rows 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 @@ -37,8 +39,10 @@ public class HexBoard implements Board private static final int [] vAngles = {60, 0, 60, -1, 120, 180, 240, -1, 300}; private static final int [] hAngles = {90, -1, 30, 90, 150, -1, 210, 270, 330}; - public HexBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation) + 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; @@ -60,6 +64,18 @@ public class HexBoard implements Board } } + @Override public boolean isOnMap(int x, int y) + { + if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { + if ((y < 0) || (y >= rows)) return false; + if ((x < ((y + 1) / 2)) || (x >= (cols + (y / 2)))) return false; + } else { + if ((x < 0) || (x >= cols)) return false; + if ((y < ((x + 1) / 2)) || (y >= (rows + (x / 2)))) return false; + } + return true; + } + @Override public void centerOf(int x, int y, Vector2 v) { float cx = this.x0; diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java index 5a7d2b8..d16945c 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.math.Vector2; public class SquareBoard implements Board { + private final int cols; // # colmuns + private final int rows; // # rows 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 @@ -11,8 +13,10 @@ public class SquareBoard implements Board // [0] is 0° facing East private static final int [] angles = {90, 0, -1, 90, -1, 180, -1, 270, -1, 0}; - public SquareBoard(float side, float x0, float y0) + public SquareBoard(int cols, int rows, float side, float x0, float y0) { + this.cols = cols; + this.rows = rows; this.side = side; this.x0 = x0; this.y0 = y0; @@ -23,6 +27,12 @@ public class SquareBoard implements Board return angles; } + @Override public boolean isOnMap(int x, int y) + { + if (x < 0 || x >= cols || y < 0 || y >= rows) return false; + return true; + } + @Override public void centerOf(int x, int y, Vector2 v) { float cx = this.x0 + (this.side / 2) + (this.side * x); diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java index 4be04f3..bcb4a54 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.math.Vector2; public class TriangleBoard implements Board { + private final int cols; // # colmuns + private final int rows; // # rows 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 @@ -20,8 +22,10 @@ public class TriangleBoard implements Board private static final int [] vAngles = {60, 0, 60, -1, 120, 180, 240, -1, 300}; private static final int [] hAngles = {30, -1, 30, 90, 150, -1, 210, 270, 330}; - public TriangleBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation) + public TriangleBoard(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; @@ -44,6 +48,18 @@ public class TriangleBoard implements Board } } + @Override public boolean isOnMap(int x, int y) + { + if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { + if ((y < 0) || (y >= rows)) return false; + if ((x < 0) || (x >= cols)) return false; + } else { + if ((y < 0) || (y >= rows)) return false; + if ((x < -1) || (x >= (cols - 1))) return false; + } + return true; + } + @Override public void centerOf(int x, int y, Vector2 v) { float cx = this.x0; |