summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-06-01 13:52:17 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-06-01 13:52:17 +0200
commitd355fc5e47a1019bde41f2d92398371922e1c9af (patch)
tree2ca3dd493be98c50819d97f488035179310493fc /core/src/ch/asynk/gdx
parent4dca631629e49b12d44cfbf6682519ea8a7afb0e (diff)
downloadgdx-boardgame-d355fc5e47a1019bde41f2d92398371922e1c9af.zip
gdx-boardgame-d355fc5e47a1019bde41f2d92398371922e1c9af.tar.gz
Board : add getTile(…), TileProvider goes into BoardFactory
Diffstat (limited to 'core/src/ch/asynk/gdx')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/Board.java4
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java17
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java24
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java28
-rw-r--r--core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java12
5 files changed, 55 insertions, 30 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
index 7c72c25..0a7acf1 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java
@@ -4,20 +4,20 @@ import com.badlogic.gdx.math.Vector2;
import ch.asynk.gdx.boardgame.Piece;
import ch.asynk.gdx.boardgame.Tile;
-import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider;
import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileKeyGenerator;
public interface Board extends TileKeyGenerator
{
public int size();
public int[] getAngles();
+ public Tile getTile(int x, int y);
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 Tile[] getAdjacents();
- public void buildAdjacents(int x, int y, TileProvider tileProvider);
+ public void buildAdjacents(int x, int y);
enum Geometry
{
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java
index 7813de8..8b17efe 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java
@@ -1,6 +1,7 @@
package ch.asynk.gdx.boardgame.boards;
import ch.asynk.gdx.boardgame.Orientation;
+import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider;
public class BoardFactory
{
@@ -15,29 +16,29 @@ public class BoardFactory
HORIZONTAL,
}
- public static Board getBoard(int cols, int rows, BoardType boardType, float side)
+ public static Board getBoard(int cols, int rows, BoardType boardType, float side, TileProvider tileProvider)
{
- return getBoard(cols, rows, boardType, side, 0f, 0f, BoardOrientation.VERTICAL);
+ return getBoard(cols, rows, boardType, side, 0f, 0f, BoardOrientation.VERTICAL, tileProvider);
}
- public static Board getBoard(int cols, int rows, BoardType boardType, float side, float x0, float y0)
+ public static Board getBoard(int cols, int rows, BoardType boardType, float side, float x0, float y0, TileProvider tileProvider)
{
- return getBoard(cols, rows, boardType, side, x0, y0, BoardOrientation.VERTICAL);
+ return getBoard(cols, rows, boardType, side, x0, y0, BoardOrientation.VERTICAL, tileProvider);
}
- public static Board getBoard(int cols, int rows, 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, TileProvider tileProvider)
{
Board board = null;
switch(boardType)
{
case HEX:
- board = new HexBoard(cols, rows, side, x0, y0, boardOrientation);
+ board = new HexBoard(cols, rows, side, x0, y0, boardOrientation, tileProvider);
break;
case SQUARE:
- board = new SquareBoard(cols, rows, side, x0, y0);
+ board = new SquareBoard(cols, rows, side, x0, y0, tileProvider);
break;
case TRIANGLE:
- board = new TriangleBoard(cols, rows, side, x0, y0, boardOrientation);
+ board = new TriangleBoard(cols, rows, side, x0, y0, boardOrientation, tileProvider);
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 362efdf..86347d1 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java
@@ -46,8 +46,9 @@ public class HexBoard implements Board
private static final int [] hAngles = { -1, 30, 90, 150, -1, 210, 270, 330, 30};
private final Tile[] adjacents;
+ private final TileProvider tileProvider;
- public HexBoard(int cols, int rows, float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)
+ public HexBoard(int cols, int rows, float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation, TileProvider tileProvider)
{
this.cols = cols;
this.rows = rows;
@@ -55,6 +56,7 @@ public class HexBoard implements Board
this.x0 = x0;
this.y0 = y0;
this.orientation = boardOrientation;
+ this.tileProvider = tileProvider;
this.w = side * 1.73205f;
this.dw = w / 2.0f;
@@ -80,6 +82,12 @@ public class HexBoard implements Board
}
}
+ @Override public Tile getTile(int x, int y)
+ {
+ if (!isOnMap(x, y)) return null;
+ return tileProvider.getTile(x, y);
+ }
+
@Override public int[] getAngles()
{
if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
@@ -91,14 +99,14 @@ public class HexBoard implements Board
@Override public Tile[] getAdjacents() { return adjacents; }
- @Override public void buildAdjacents(int x, int y, TileProvider tileProvider)
+ @Override public void buildAdjacents(int x, int y)
{
- adjacents[0] = tileProvider.getTile(x + 1, y);
- adjacents[1] = tileProvider.getTile(x + 1, y + 1);
- adjacents[2] = tileProvider.getTile(x , y + 1);
- adjacents[3] = tileProvider.getTile(x - 1, y);
- adjacents[4] = tileProvider.getTile(x - 1, y - 1);
- adjacents[5] = tileProvider.getTile(x , y - 1);
+ adjacents[0] = getTile(x + 1, y);
+ adjacents[1] = getTile(x + 1, y + 1);
+ adjacents[2] = getTile(x , y + 1);
+ adjacents[3] = getTile(x - 1, y);
+ adjacents[4] = getTile(x - 1, y - 1);
+ adjacents[5] = getTile(x , y - 1);
}
@Override public int genKey(int x, int y)
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
index d09a39d..3997c1c 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java
@@ -18,34 +18,42 @@ public class SquareBoard implements Board
private static final int [] angles = {0, -1, 90, -1, 180, -1, 270, -1, 90};
private final Tile[] adjacents;
+ private final TileProvider tileProvider;
- public SquareBoard(int cols, int rows, float side, float x0, float y0)
+ public SquareBoard(int cols, int rows, float side, float x0, float y0, TileProvider tileProvider)
{
this.cols = cols;
this.rows = rows;
this.side = side;
this.x0 = x0;
this.y0 = y0;
+ this.tileProvider = tileProvider;
this.adjacents = new Tile[8];
}
@Override public int size() { return cols * rows; }
+ @Override public Tile getTile(int x, int y)
+ {
+ if (!isOnMap(x, y)) return null;
+ return tileProvider.getTile(x, y);
+ }
+
@Override public int[] getAngles() { return angles; }
@Override public Tile[] getAdjacents() { return adjacents; }
- @Override public void buildAdjacents(int x, int y, TileProvider tileProvider)
+ @Override public void buildAdjacents(int x, int y)
{
- adjacents[0] = tileProvider.getTile(x + 1, y);
- adjacents[1] = tileProvider.getTile(x + 1, y + 1);
- adjacents[2] = tileProvider.getTile(x , y + 1);
- adjacents[3] = tileProvider.getTile(x - 1, y + 1);
- adjacents[4] = tileProvider.getTile(x - 1, y);
- adjacents[5] = tileProvider.getTile(x - 1, y - 1);
- adjacents[6] = tileProvider.getTile(x , y - 1);
- adjacents[7] = tileProvider.getTile(x + 1, y - 1);
+ adjacents[0] = getTile(x + 1, y);
+ adjacents[1] = getTile(x + 1, y + 1);
+ adjacents[2] = getTile(x , y + 1);
+ adjacents[3] = getTile(x - 1, y + 1);
+ adjacents[4] = getTile(x - 1, y);
+ adjacents[5] = getTile(x - 1, y - 1);
+ adjacents[6] = getTile(x , y - 1);
+ adjacents[7] = getTile(x + 1, y - 1);
}
@Override public int genKey(int x, int y)
diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
index c90d351..db3cb5d 100644
--- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
+++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java
@@ -26,8 +26,9 @@ public class TriangleBoard implements Board
private static final int [] hAngles = { -1, 30, 90, 150, -1, 210, 270, 330, 30};
private final Tile[] adjacents;
+ private final TileProvider tileProvider;
- public TriangleBoard(int cols, int rows, float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)
+ public TriangleBoard(int cols, int rows, float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation, TileProvider tileProvider)
{
this.cols = cols;
this.rows = rows;
@@ -35,6 +36,7 @@ public class TriangleBoard implements Board
this.x0 = x0;
this.y0 = y0;
this.orientation = boardOrientation;
+ this.tileProvider = tileProvider;
this.d = side / 2f;
this.h = side * 0.866f;
@@ -47,6 +49,12 @@ public class TriangleBoard implements Board
@Override public int size() { return cols * rows; }
+ @Override public Tile getTile(int x, int y)
+ {
+ if (!isOnMap(x, y)) return null;
+ return tileProvider.getTile(x, y);
+ }
+
@Override public int[] getAngles()
{
if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) {
@@ -58,7 +66,7 @@ public class TriangleBoard implements Board
@Override public Tile[] getAdjacents() { return adjacents; }
- @Override public void buildAdjacents(int x, int y, TileProvider tileProvider)
+ @Override public void buildAdjacents(int x, int y)
{
// FIXME
}