From 416490c4382cfde163be376d978d7890da81bcd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 23 Jan 2020 16:55:53 +0100 Subject: add TileStorage, ArrayTileStorage, HashTileStorage --- core/src/ch/asynk/gdx/boardgame/TileStorage.java | 29 +++++++++++++ .../boardgame/tilestorages/ArrayTileStorage.java | 25 +++++++++++ .../boardgame/tilestorages/HashTileStorage.java | 50 ++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 core/src/ch/asynk/gdx/boardgame/TileStorage.java create mode 100644 core/src/ch/asynk/gdx/boardgame/tilestorages/ArrayTileStorage.java create mode 100644 core/src/ch/asynk/gdx/boardgame/tilestorages/HashTileStorage.java 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 tiles; + + public HashTileStorage(int x, int y) + { + this.tiles = new HashMap(); + } + + @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; + } +} -- cgit v1.1-2-g2b99