summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/src/ch/asynk/gdx/boardgame/TileStorage.java29
-rw-r--r--core/src/ch/asynk/gdx/boardgame/tilestorages/ArrayTileStorage.java25
-rw-r--r--core/src/ch/asynk/gdx/boardgame/tilestorages/HashTileStorage.java50
3 files changed, 104 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/TileStorage.java b/core/src/ch/asynk/gdx/boardgame/TileStorage.java
new file mode 100644
index 0000000..63840d2
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/TileStorage.java
@@ -0,0 +1,29 @@
+package ch.asynk.gdx.boardgame;
+
+public interface TileStorage
+{
+ @FunctionalInterface
+ public interface TileFactory
+ {
+ public Tile buildTile(int x, int y);
+ }
+
+ @FunctionalInterface
+ public interface TileKeyGenerator
+ {
+ public int genKey(int x, int y);
+ }
+
+ @FunctionalInterface
+ public interface TileProvider
+ {
+ public Tile getTile(int x, int y);
+ }
+
+ Tile getTile(int x, int y, int k, TileFactory tileFactory);
+
+ default Tile getTile(int x, int y, TileKeyGenerator tileKeyGenerator, TileFactory tileFactory)
+ {
+ return getTile(x, y, tileKeyGenerator.genKey(x, y), tileFactory);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/boardgame/tilestorages/ArrayTileStorage.java b/core/src/ch/asynk/gdx/boardgame/tilestorages/ArrayTileStorage.java
new file mode 100644
index 0000000..135d833
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/tilestorages/ArrayTileStorage.java
@@ -0,0 +1,25 @@
+package ch.asynk.gdx.boardgame.tilestorages;
+
+import ch.asynk.gdx.boardgame.Tile;
+import ch.asynk.gdx.boardgame.TileStorage;
+import ch.asynk.gdx.boardgame.TileStorage.TileFactory;
+
+public class ArrayTileStorage implements TileStorage
+{
+ public Tile[] tiles;
+
+ public ArrayTileStorage(int n)
+ {
+ this.tiles = new Tile[n];
+ }
+
+ @Override public Tile getTile(int x, int y, int k, TileFactory tileFactory)
+ {
+ Tile tile = tiles[k];
+ if (tile == null) {
+ tile = tileFactory.buildTile(x, y);
+ tiles[k] = tile;
+ }
+ return tile;
+ }
+}
diff --git a/core/src/ch/asynk/gdx/boardgame/tilestorages/HashTileStorage.java b/core/src/ch/asynk/gdx/boardgame/tilestorages/HashTileStorage.java
new file mode 100644
index 0000000..cf8a386
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/tilestorages/HashTileStorage.java
@@ -0,0 +1,50 @@
+package ch.asynk.gdx.boardgame.tilestorages;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import ch.asynk.gdx.boardgame.Tile;
+import ch.asynk.gdx.boardgame.TileStorage;
+import ch.asynk.gdx.boardgame.TileStorage.TileFactory;
+
+public class HashTileStorage implements TileStorage
+{
+ private Map<Integer, Tile> tiles;
+
+ public HashTileStorage(int x, int y)
+ {
+ this.tiles = new HashMap<Integer, Tile>();
+ }
+
+ @Override public Tile getTile(int x, int y, int k, TileFactory tileFactory)
+ {
+ Tile tile = tiles.get(k);
+ if (tile == null) {
+ tile = tileFactory.buildTile(x, y);
+ tiles.put(k, tile);
+ }
+ return tile;
+ }
+
+ Tile get(int x, int y, TileFactory tileFactory)
+ {
+ return getTile(x, y, hash(x, y), tileFactory);
+ }
+
+ public int hash(int x, int y)
+ {
+ int k = 0;
+ if (x > 0 ) {
+ k = (x & 0x0fff);
+ } else {
+ k = ((-x & 0x0fff) | 0x8000);
+ }
+ k = k << 16;
+ if (y > 0 ) {
+ k = k | (y & 0x0fff);
+ } else {
+ k = k | ((-y & 0x0fff) | 0x8000);
+ }
+ return k;
+ }
+}