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 | |
parent | 5297877a607167c991364493f3e7fd283d1ead17 (diff) | |
download | gdx-boardgame-3a153fe86fcd3ae224b7c6ddba1780eeed89ec8a.zip gdx-boardgame-3a153fe86fcd3ae224b7c6ddba1780eeed89ec8a.tar.gz |
Board : add boolean isOnBoard(int, int)
7 files changed, 68 insertions, 23 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; diff --git a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java index 6fcc30f..0990d7b 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java @@ -41,7 +41,7 @@ public class AnimationsScreen extends AbstractScreen super(app, ""); this.map = app.assets.getTexture(app.assets.MAP_00); - this.board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL); + this.board = BoardFactory.getBoard(0, 0, BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL); this.camera = this.cam = new Camera(10, map.getWidth(), map.getHeight(), 1.0f, 0.3f, false); this.panzer = getPiece(app, 7, 4, Orientation.NW, app.assets.PANZER, 1f); diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java index f609f2c..37fdade 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -61,12 +61,14 @@ public class BoardScreen extends AbstractScreen float d0 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TCHEBYCHEV); float d1 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TAXICAB); float d2 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.EUCLIDEAN); - GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); - pos.set(v); - board.centerOf((int)v.x, (int)v.y, v); - panzer.centerOn(v.x, v.y); - panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); - GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + if (board.isOnMap((int)v.x, (int)v.y)) { + GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); + pos.set(v); + board.centerOf((int)v.x, (int)v.y, v); + panzer.centerOn(v.x, v.y); + panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); + GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + } return true; } @@ -78,7 +80,7 @@ public class BoardScreen extends AbstractScreen dy = 0; w = map.getWidth(); h = map.getHeight(); - board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL); + board = BoardFactory.getBoard(10, 9, BoardFactory.BoardType.HEX, 110, 50, 103, BoardFactory.BoardOrientation.VERTICAL); } public void setHEX_H() @@ -89,7 +91,7 @@ public class BoardScreen extends AbstractScreen dy = - dx; w = map.getHeight(); h = map.getWidth(); - board = BoardFactory.getBoard(BoardFactory.BoardType.HEX, 110, 103, 50, BoardFactory.BoardOrientation.HORIZONTAL); + board = BoardFactory.getBoard(9, 10, BoardFactory.BoardType.HEX, 110, 103, 50, BoardFactory.BoardOrientation.HORIZONTAL); } public void setSQUARE() @@ -100,7 +102,7 @@ public class BoardScreen extends AbstractScreen dy = 0; w = map.getWidth(); h = map.getHeight(); - board = BoardFactory.getBoard(BoardFactory.BoardType.SQUARE, 83, 5, 5); + board = BoardFactory.getBoard(8, 8, BoardFactory.BoardType.SQUARE, 83, 5, 5); } public void setTRI_H() @@ -111,7 +113,7 @@ public class BoardScreen extends AbstractScreen dy = 0; w = map.getWidth(); h = map.getHeight(); - board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 109, 53, BoardFactory.BoardOrientation.HORIZONTAL); + board = BoardFactory.getBoard(21, 8, BoardFactory.BoardType.TRIANGLE, 150, 109, 53, BoardFactory.BoardOrientation.HORIZONTAL); } public void setTRI_V() @@ -122,7 +124,7 @@ public class BoardScreen extends AbstractScreen dy = - dx; w = map.getHeight(); h = map.getWidth(); - board = BoardFactory.getBoard(BoardFactory.BoardType.TRIANGLE, 150, 16, 110, BoardFactory.BoardOrientation.VERTICAL); + board = BoardFactory.getBoard(8, 21, BoardFactory.BoardType.TRIANGLE, 150, 16, 110, BoardFactory.BoardOrientation.VERTICAL); } } |