From 783bfb7a8b085f4373cdcc28fbbac53063ccf68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 2 Jun 2020 12:32:42 +0200 Subject: Board,TileStorage : use Tile.OffMap --- .../ch/asynk/gdx/boardgame/boards/HexBoard.java | 23 +++++++++++----------- .../ch/asynk/gdx/boardgame/boards/SquareBoard.java | 5 +++-- .../asynk/gdx/boardgame/boards/TriangleBoard.java | 5 +++-- .../gdx/boardgame/tilestorages/TileStorage.java | 2 +- .../ch/asynk/gdx/boardgame/test/BoardScreen.java | 14 +++++++------ .../src/ch/asynk/gdx/boardgame/test/HexScreen.java | 11 ++++++----- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index 691382b..7c3febb 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -72,6 +72,8 @@ public class HexBoard implements Board } this.adjacents = new Tile[6]; + for (int i = 0; i < 6; i++) + this.adjacents[i] = Tile.OffMap; } @Override public int size() @@ -85,8 +87,7 @@ public class HexBoard implements Board @Override public Tile getTile(int x, int y) { - if (!isOnMap(x, y)) return null; - return tileProvider.getTile(x, y); + return tileProvider.getTile(x, y, isOnMap(x, y)); } @Override public int[] getAngles() @@ -350,7 +351,6 @@ public class HexBoard implements Board int y = y0; - Tile t = null; Tile from = getTile(x0, y0); Tile to = getTile(x1, y1); tiles.add(from); @@ -359,8 +359,8 @@ public class HexBoard implements Board boolean blocked = losBlocked; y += d; - t = getTile(x, y); - if (t != null) { + Tile t = getTile(x, y); + if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; blocked = (blocked || t.blockLos(from, to)); @@ -368,7 +368,7 @@ public class HexBoard implements Board x += d; t = getTile(x, y); - if (t != null) { + if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; blocked = (blocked && t.blockLos(from, to)); @@ -376,7 +376,7 @@ public class HexBoard implements Board y += d; t = getTile(x, y); - if (t != null) { + if (t.isOnMap()) { tiles.add(t); t.blocked = (losBlocked || blocked); losBlocked = (t.blocked || t.blockLos(from, to)); @@ -395,7 +395,6 @@ public class HexBoard implements Board int x = x0; int y = y0; - Tile t = null; Tile from = getTile(x0, y0); Tile to = getTile(x1, y1); tiles.add(from); @@ -404,8 +403,8 @@ public class HexBoard implements Board boolean blocked = losBlocked; x += dx; - t = getTile(x, y); - if (t != null) { + Tile t = getTile(x, y); + if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; blocked = (blocked || t.blockLos(from, to)); @@ -415,7 +414,7 @@ public class HexBoard implements Board if (!sig) x -= dx; t = getTile(x, y); - if (t != null) { + if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; blocked = (blocked && t.blockLos(from, to)); @@ -423,7 +422,7 @@ public class HexBoard implements Board x += dx; t = getTile(x, y); - if (t != null) { + if (t.isOnMap()) { tiles.add(t); t.blocked = (losBlocked || blocked); losBlocked = (t.blocked || t.blockLos(from, to)); diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java index 84eee34..ae3e18b 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java @@ -31,14 +31,15 @@ public class SquareBoard implements Board this.tileProvider = tileProvider; this.adjacents = new Tile[8]; + for (int i = 0; i < 8; i++) + this.adjacents[i] = Tile.OffMap; } @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); + return tileProvider.getTile(x, y, isOnMap(x, y)); } @Override public int[] getAngles() { return angles; } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java index cf3ef07..42bd378 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -46,14 +46,15 @@ public class TriangleBoard implements Board this.h23 = this.h * 0.66666f; this.adjacents = new Tile[3]; + for (int i = 0; i < 3; i++) + this.adjacents[i] = Tile.OffMap; } @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); + return tileProvider.getTile(x, y, isOnMap(x, y)); } @Override public int[] getAngles() diff --git a/core/src/ch/asynk/gdx/boardgame/tilestorages/TileStorage.java b/core/src/ch/asynk/gdx/boardgame/tilestorages/TileStorage.java index 405ef2f..0b1f17d 100644 --- a/core/src/ch/asynk/gdx/boardgame/tilestorages/TileStorage.java +++ b/core/src/ch/asynk/gdx/boardgame/tilestorages/TileStorage.java @@ -19,7 +19,7 @@ public interface TileStorage @FunctionalInterface public interface TileProvider { - public Tile getTile(int x, int y); + public Tile getTile(int x, int y, boolean isOffMap); } Tile getTile(int x, int y, int k, TileFactory tileFactory); diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java index d20e52a..de70ce4 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -73,10 +73,10 @@ public class BoardScreen extends AbstractScreen public boolean touch(float x, float y, boolean down) { board.toBoard(x, y, v); - if (!board.isOnMap((int)v.x, (int)v.y)) + Tile tile = board.getTile((int)v.x, (int)v.y); + if (!tile.isOnMap()) return false; if (down) { - Tile tile = getTile((int)v.x, (int)v.y); if (!dragging && panzer.isOn(tile)) { dragging = true; clearAdjacents(); @@ -122,7 +122,7 @@ public class BoardScreen extends AbstractScreen private void clearAdjacents() { for (Tile tile : board.getAdjacents()) { - if (tile != null) { + if (tile.isOnMap()) { tilesToDraw.remove(tile); tile.enableOverlay(12, false); } @@ -134,16 +134,18 @@ public class BoardScreen extends AbstractScreen clearAdjacents(); board.buildAdjacents((int)v.x, (int)v.y); for (Tile tile : board.getAdjacents()) { - if (tile != null) { + if (tile.isOnMap()) { tilesToDraw.add(tile); tile.enableOverlay(12, true); } } } - private Tile getTile(int x, int y) + private Tile getTile(int x, int y, boolean isOnMap) { - return tileStorage.getTile(x, y, board::genKey, this::buildTile); + if (isOnMap) + return tileStorage.getTile(x, y, board::genKey, this::buildTile); + return Tile.OffMap; } private Tile buildTile(int x, int y) diff --git a/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java b/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java index 2335d4e..ceb601f 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java @@ -149,10 +149,9 @@ public class HexScreen extends AbstractScreen public boolean touch(float x, float y, boolean down) { board.toBoard(x, y, v); - if (!board.isOnMap((int)v.x, (int)v.y)) { - return false; - } Hex hex = getHex((int)v.x, (int)v.y); + if (!hex.isOnMap()) + return false; if (down) { if (!panzer.dragging && panzer.isOn(hex)) { panzer.dragging = true; @@ -223,9 +222,11 @@ public class HexScreen extends AbstractScreen return (Hex) board.getTile(x, y); } - private Tile getTile(int x, int y) + private Tile getTile(int x, int y, boolean isOnMap) { - return tileStorage.getTile(x, y, board::genKey, this::buildTile); + if (isOnMap) + return tileStorage.getTile(x, y, board::genKey, this::buildTile); + return Tile.OffMap; } private Tile buildTile(int x, int y) -- cgit v1.1-2-g2b99