diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-01-23 16:55:53 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-01-23 16:55:53 +0100 |
commit | 416490c4382cfde163be376d978d7890da81bcd5 (patch) | |
tree | 04caa146a3fc572f2fcbff8ec9a4454dbd00dd03 /core | |
parent | 903db05854ebf145fbb5dff2713753172ae56fda (diff) | |
download | gdx-boardgame-416490c4382cfde163be376d978d7890da81bcd5.zip gdx-boardgame-416490c4382cfde163be376d978d7890da81bcd5.tar.gz |
add TileStorage, ArrayTileStorage, HashTileStorage
Diffstat (limited to 'core')
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; + } +} |