diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java index 85f6f2c..884fe90 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java @@ -1,8 +1,10 @@ package net.simon987.cubotplugin; import net.simon987.server.assembly.Status; +import net.simon987.server.game.item.Item; import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.world.Tile; import org.bson.Document; public class CubotDrill extends CubotHardwareModule { @@ -43,9 +45,13 @@ public class CubotDrill extends CubotHardwareModule { if (cubot.spendEnergy(1400)) { if (cubot.getCurrentAction() == Action.IDLE) { - //TODO: Get Tile instance and call onDig() - //int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); - //cubot.setCurrentAction(Action.DIGGING); + Tile tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); + + Item newItem = tile.drill(); + if (newItem != null) { + cubot.setCurrentAction(Action.DIGGING); + cubot.giveItem(newItem); + } } } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java index b2e3369..63aecae 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java @@ -4,6 +4,8 @@ import net.simon987.npcplugin.event.CpuInitialisationListener; import net.simon987.npcplugin.event.VaultCompleteListener; import net.simon987.npcplugin.event.VaultWorldUpdateListener; import net.simon987.npcplugin.event.WorldCreationListener; +import net.simon987.npcplugin.world.TileVaultFloor; +import net.simon987.npcplugin.world.TileVaultWall; import net.simon987.server.ServerConfiguration; import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.logging.LogManager; @@ -37,6 +39,9 @@ public class NpcPlugin extends ServerPlugin { registry.registerHardware(RadioReceiverHardware.class); + registry.registerTile(TileVaultFloor.ID, TileVaultFloor.class); + registry.registerTile(TileVaultWall.ID, TileVaultWall.class); + radioTowers = new ArrayList<>(32); LogManager.LOGGER.info("Initialised NPC plugin"); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java index d7a6865..a4c1f48 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java @@ -1,10 +1,10 @@ package net.simon987.npcplugin; +import net.simon987.npcplugin.world.TileVaultFloor; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; import net.simon987.server.game.objects.Direction; import net.simon987.server.game.world.Location; -import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import org.bson.types.ObjectId; @@ -140,7 +140,7 @@ public class VaultDimension { //4. Choose a random world from the last layer and create the vault box there (objective) World objectiveWorld = lastLayerWorlds.get(random.nextInt(lastLayerWorlds.size())); - Point exitPortalPt = objectiveWorld.getRandomTileWithAdjacent(8, TileMap.VAULT_FLOOR); + Point exitPortalPt = objectiveWorld.getRandomTileWithAdjacent(8, TileVaultFloor.ID); if (exitPortalPt != null) { @@ -159,7 +159,7 @@ public class VaultDimension { } //5. Create an exit portal in the home World - Point homePortalPt = homeWorld.getRandomTileWithAdjacent(8, TileMap.VAULT_FLOOR); + Point homePortalPt = homeWorld.getRandomTileWithAdjacent(8, TileVaultFloor.ID); if (homePortalPt != null) { Portal homePortal = new Portal(); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java index 12c055f..204db43 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java @@ -2,6 +2,7 @@ package net.simon987.npcplugin; import net.simon987.server.game.objects.Direction; import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.TileVoid; import net.simon987.server.game.world.World; import java.awt.*; @@ -216,7 +217,7 @@ public class VaultWorldGenerator { for (int x = 0; x < worldSize; x++) { for (int y = 0; y < worldSize; y++) { - if (map.getTileAt(x, y) != floorTile && hasTileAdjacent(x, y, map, floorTile)) { + if (map.getTileIdAt(x, y) != floorTile && hasTileAdjacent(x, y, map, floorTile)) { map.setTileAt(wallTile, x, y); } } @@ -226,8 +227,8 @@ public class VaultWorldGenerator { for (int x = 0; x < worldSize; x++) { for (int y = 0; y < worldSize; y++) { - if (map.getTileAt(x, y) != floorTile && map.getTileAt(x, y) != wallTile) { - map.setTileAt(TileMap.VOID, x, y); + if (map.getTileIdAt(x, y) != floorTile && map.getTileIdAt(x, y) != wallTile) { + map.setTileAt(new TileVoid(), x, y); } } } @@ -241,7 +242,7 @@ public class VaultWorldGenerator { for (int dX = -1; dX <= 1; dX++) { for (int dY = -1; dY <= 1; dY++) { - if (map.getTileAt(x + dX, y + dY) == tile) { + if (map.getTileIdAt(x + dX, y + dY) == tile) { return true; } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java index 8b29101..ee7c038 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldUtils.java @@ -1,5 +1,6 @@ package net.simon987.npcplugin; +import net.simon987.npcplugin.world.TileVaultFloor; import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.World; import org.bson.types.ObjectId; @@ -19,12 +20,12 @@ public class VaultWorldUtils { //Count number of floor tiles. If there is less plain tiles than desired amount of boxes, //set the desired amount of blobs to the plain tile count - int[][] tiles = world.getTileMap().getTiles(); + TileMap m = world.getTileMap(); int floorCount = 0; for (int y = 0; y < world.getWorldSize(); y++) { for (int x = 0; x < world.getWorldSize(); x++) { - if (tiles[x][y] == TileMap.VAULT_FLOOR) { + if (m.getTileIdAt(x, y) == TileVaultFloor.ID) { floorCount++; } } @@ -37,14 +38,14 @@ public class VaultWorldUtils { outerLoop: for (int i = 0; i < boxesCount; i++) { - Point p = world.getTileMap().getRandomTile(TileMap.VAULT_FLOOR); + Point p = m.getRandomTile(TileVaultFloor.ID); if (p != null) { //Don't block worlds int counter = 0; while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || world.getGameObjectsAt(p.x, p.y).size() != 0) { - p = world.getTileMap().getRandomTile(TileMap.VAULT_FLOOR); + p = m.getRandomTile(TileVaultFloor.ID); counter++; if (counter > 25) { diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java index 2120ad0..8821bd4 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java @@ -7,7 +7,7 @@ import net.simon987.npcplugin.VaultDoor; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.event.WorldGenerationEvent; -import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.TilePlain; import net.simon987.server.game.world.World; import org.bson.types.ObjectId; @@ -69,7 +69,7 @@ public class WorldCreationListener implements GameEventListener { } //Also spawn a radio tower in the same World - Point p = world.getRandomTileWithAdjacent(8, TileMap.PLAIN_TILE); + Point p = world.getRandomTileWithAdjacent(8, TilePlain.ID); if (p != null) { while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0) { p = world.getRandomPassableTile(); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultFloor.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultFloor.java new file mode 100644 index 0000000..194c13b --- /dev/null +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultFloor.java @@ -0,0 +1,13 @@ +package net.simon987.npcplugin.world; + +import net.simon987.server.game.world.Tile; + +public class TileVaultFloor extends Tile { + + public static final int ID = 4; + + @Override + public int getId() { + return ID; + } +} diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultWall.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultWall.java new file mode 100644 index 0000000..7d661a3 --- /dev/null +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/world/TileVaultWall.java @@ -0,0 +1,24 @@ +package net.simon987.npcplugin.world; + +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.world.Tile; + +public class TileVaultWall extends Tile { + + public static final int ID = 5; + + @Override + public int getId() { + return ID; + } + + @Override + public boolean walk(GameObject object) { + return false; //always blocked + } + + @Override + public boolean isBlocked() { + return true; + } +} diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java index 8704d2d..8d05d74 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java @@ -1,6 +1,7 @@ package net.simon987.biomassplugin; import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.TilePlain; import net.simon987.server.game.world.World; import org.bson.types.ObjectId; @@ -21,12 +22,12 @@ public class WorldUtils { //Count number of plain tiles. If there is less plain tiles than desired amount of blobs, //set the desired amount of blobs to the plain tile count - int[][] tiles = world.getTileMap().getTiles(); + TileMap m = world.getTileMap(); int plainCount = 0; for (int y = 0; y < world.getWorldSize(); y++) { for (int x = 0; x < world.getWorldSize(); x++) { - if (tiles[x][y] == TileMap.PLAIN_TILE) { + if (m.getTileIdAt(x, y) == TilePlain.ID) { plainCount++; } } @@ -39,14 +40,14 @@ public class WorldUtils { outerLoop: for (int i = 0; i < blobCount; i++) { - Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); + Point p = m.getRandomTile(TilePlain.ID); if (p != null) { //Don't block worlds int counter = 0; while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || world.getGameObjectsAt(p.x, p.y).size() != 0) { - p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); + p = m.getRandomTile(TilePlain.ID); counter++; if (counter > 25) { diff --git a/Server/src/main/java/net/simon987/server/GameServer.java b/Server/src/main/java/net/simon987/server/GameServer.java index 93e19d9..b817f1e 100644 --- a/Server/src/main/java/net/simon987/server/GameServer.java +++ b/Server/src/main/java/net/simon987/server/GameServer.java @@ -15,8 +15,7 @@ import net.simon987.server.game.debug.*; import net.simon987.server.game.item.ItemCopper; import net.simon987.server.game.item.ItemIron; import net.simon987.server.game.objects.GameRegistry; -import net.simon987.server.game.world.DayNightCycle; -import net.simon987.server.game.world.World; +import net.simon987.server.game.world.*; import net.simon987.server.logging.LogManager; import net.simon987.server.plugin.PluginManager; import net.simon987.server.user.User; @@ -122,6 +121,12 @@ public class GameServer implements Runnable { gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class); gameRegistry.registerItem(ItemIron.ID, ItemIron.class); + + gameRegistry.registerTile(TileVoid.ID, TileVoid.class); + gameRegistry.registerTile(TilePlain.ID, TilePlain.class); + gameRegistry.registerTile(TileWall.ID, TileWall.class); + gameRegistry.registerTile(TileCopper.ID, TileCopper.class); + gameRegistry.registerTile(TileIron.ID, TileIron.class); } public GameUniverse getGameUniverse() { diff --git a/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java b/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java index 5d3ce18..d7c6bef 100644 --- a/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java +++ b/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java @@ -5,7 +5,7 @@ import org.bson.Document; public class ItemCopper extends Item { - public static final int ID = 0x0003; + public static final int ID = 0x0004; @Override public int getId() { diff --git a/Server/src/main/java/net/simon987/server/game/item/ItemIron.java b/Server/src/main/java/net/simon987/server/game/item/ItemIron.java index 4033101..3b27b3a 100644 --- a/Server/src/main/java/net/simon987/server/game/item/ItemIron.java +++ b/Server/src/main/java/net/simon987/server/game/item/ItemIron.java @@ -4,7 +4,7 @@ import org.bson.Document; public class ItemIron extends Item { - public static final int ID = 0x0004; + public static final int ID = 0x0003; @Override public int getId() { diff --git a/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java index 3c863ec..add111d 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java +++ b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java @@ -2,6 +2,7 @@ package net.simon987.server.game.objects; import net.simon987.server.assembly.HardwareModule; import net.simon987.server.game.item.Item; +import net.simon987.server.game.world.Tile; import net.simon987.server.logging.LogManager; import org.bson.Document; @@ -13,12 +14,14 @@ public class GameRegistry { private HashMap> gameObjects; private HashMap> hardware; private HashMap> items; + private HashMap> tiles; public GameRegistry() { gameObjects = new HashMap<>(); hardware = new HashMap<>(); items = new HashMap<>(); + tiles = new HashMap<>(); } public void registerGameObject(Class clazz) { @@ -33,6 +36,10 @@ public class GameRegistry { items.put(id, clazz); } + public void registerTile(int id, Class clazz) { + tiles.put(id, clazz); + } + public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) { String type = document.getString("type"); @@ -119,6 +126,23 @@ public class GameRegistry { } } + public Tile makeTile(int tileId) { + if (tiles.containsKey(tileId)) { + + try { + return tiles.get(tileId).getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException + | InvocationTargetException e) { + e.printStackTrace(); + return null; + } + + } else { + LogManager.LOGGER.severe("Trying to create an unknown Tile type: " + tileId); + return null; + } + } + public boolean isGameObjectRegistered(String type) { return gameObjects.containsKey(type); } diff --git a/Server/src/main/java/net/simon987/server/game/world/Tile.java b/Server/src/main/java/net/simon987/server/game/world/Tile.java index 776c517..a3604a8 100644 --- a/Server/src/main/java/net/simon987/server/game/world/Tile.java +++ b/Server/src/main/java/net/simon987/server/game/world/Tile.java @@ -1,21 +1,37 @@ package net.simon987.server.game.world; import net.simon987.server.game.item.Item; +import net.simon987.server.game.objects.GameObject; public abstract class Tile { - protected boolean blocked; - /** * @return Unique id of the tile */ public abstract int getId(); - public Item onDrill() { + /** + * Called when an object attempts to drill this tile + * + * @return The item obtained by drilling, return null for no item + */ + public Item drill() { return null; } + /** + * Called when a player attempts to walk on this tile + * + * @return true if the object can walk on this tile, false if blocked + */ + public boolean walk(GameObject object) { + return true; + } + + /** + * Check if a tile should be considered 'blocked' (by the LiDAR, for example) + */ public boolean isBlocked() { - return blocked; + return false; } } diff --git a/Server/src/main/java/net/simon987/server/game/world/TileCopper.java b/Server/src/main/java/net/simon987/server/game/world/TileCopper.java new file mode 100644 index 0000000..53acbd4 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/TileCopper.java @@ -0,0 +1,19 @@ +package net.simon987.server.game.world; + +import net.simon987.server.game.item.Item; +import net.simon987.server.game.item.ItemCopper; + +public class TileCopper extends Tile { + + public static final int ID = 3; + + @Override + public int getId() { + return ID; + } + + @Override + public Item drill() { + return new ItemCopper(); + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/TileIron.java b/Server/src/main/java/net/simon987/server/game/world/TileIron.java new file mode 100644 index 0000000..6b555f9 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/TileIron.java @@ -0,0 +1,19 @@ +package net.simon987.server.game.world; + +import net.simon987.server.game.item.Item; +import net.simon987.server.game.item.ItemIron; + +public class TileIron extends Tile { + + public static final int ID = 2; + + @Override + public int getId() { + return ID; + } + + @Override + public Item drill() { + return new ItemIron(); + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/TileMap.java b/Server/src/main/java/net/simon987/server/game/world/TileMap.java index 642d4c0..3058156 100755 --- a/Server/src/main/java/net/simon987/server/game/world/TileMap.java +++ b/Server/src/main/java/net/simon987/server/game/world/TileMap.java @@ -1,38 +1,27 @@ package net.simon987.server.game.world; +import net.simon987.server.GameServer; +import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.io.JSONSerialisable; import net.simon987.server.io.MongoSerializable; import org.bson.Document; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.awt.*; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.util.ArrayList; -import java.util.Base64; import java.util.Random; -import java.util.zip.Deflater; -import java.util.zip.DeflaterOutputStream; /** * A 2D map of Tile objects of size width*height */ public class TileMap implements JSONSerialisable, MongoSerializable { - public static final int VOID = -1; - public static final int PLAIN_TILE = 0; - public static final int WALL_TILE = 1; - public static final int IRON_TILE = 2; - public static final int COPPER_TILE = 3; - public static final int VAULT_FLOOR = 4; - public static final int VAULT_WALL = 5; - - /** * The map of tile */ - private int[][] tiles; + private Tile[][] tiles; /** * width, in tiles @@ -51,10 +40,10 @@ public class TileMap implements JSONSerialisable, MongoSerializable { this.width = width; this.height = height; - tiles = new int[width][height]; + tiles = new Tile[width][height]; } - public TileMap(int[][] tiles, int size) { + public TileMap(Tile[][] tiles, int size) { this.width = size; this.height = size; @@ -63,40 +52,62 @@ public class TileMap implements JSONSerialisable, MongoSerializable { /** * Change the tile at a specified position - * Sets the modified flag * * @param tileId id of the new Tile * @param x X coordinate of the tile to set * @param y Y coordinate of the tile to set */ public void setTileAt(int tileId, int x, int y) { + setTileAt(GameServer.INSTANCE.getRegistry().makeTile(tileId), x, y); + } + + /** + * Change the tile at a specified position + * + * @param tile new Tile + * @param x X coordinate of the tile to set + * @param y Y coordinate of the tile to set + */ + public void setTileAt(Tile tile, int x, int y) { try { - tiles[x][y] = tileId; + tiles[x][y] = tile; } catch (ArrayIndexOutOfBoundsException e) { //Shouldn't happen e.printStackTrace(); } } + /** + * Get the tile id at a specific position + * + * @param x X coordinate of the tile to get + * @param y Y coordinate of the tile to get + * @return the tile id at the specified position, -1 if out of bounds + */ + public int getTileIdAt(int x, int y) { + try { + return tiles[x][y].getId(); + } catch (ArrayIndexOutOfBoundsException e) { + return -1; + } + } + /** * Get the tile at a specific position * * @param x X coordinate of the tile to get * @param y Y coordinate of the tile to get - * @return the tile at the specified position, -1 if out of bounds + * @return the tile id at the specified position, null if out of bounds */ - public int getTileAt(int x, int y) { + public Tile getTileAt(int x, int y) { try { return tiles[x][y]; } catch (ArrayIndexOutOfBoundsException e) { - return -1; + return null; } } - public int[][] getTiles() { - return tiles; - } public int getWidth() { @@ -111,28 +122,15 @@ public class TileMap implements JSONSerialisable, MongoSerializable { public JSONObject jsonSerialise() { JSONObject json = new JSONObject(); - byte[] terrain = new byte[width * width]; + JSONArray terrain = new JSONArray(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { - terrain[x * width + y] = (byte) tiles[x][y]; + terrain.add(tiles[y][x].getId()); } } - try { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true); - DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(stream, compressor); - deflaterOutputStream.write(terrain); - - deflaterOutputStream.close(); - byte[] compressedBytes = stream.toByteArray(); - - json.put("z", new String(Base64.getEncoder().encode(compressedBytes))); - - } catch (IOException e) { - e.printStackTrace(); - } + json.put("terrain", terrain); return json; } @@ -152,7 +150,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable { for (int x = 0; x < this.width; x++) { for (int y = 0; y < this.height; y++) { - bsonTiles.add(tiles[x][y]); + bsonTiles.add(tiles[x][y].getId()); } } @@ -165,19 +163,19 @@ public class TileMap implements JSONSerialisable, MongoSerializable { public static TileMap deserialize(Document object, int size) { ArrayList terrain = (ArrayList) object.get("tiles"); + GameRegistry reg = GameServer.INSTANCE.getRegistry(); - int[][] tiles = new int[size][size]; + Tile[][] tiles = new Tile[size][size]; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { - tiles[x][y] = terrain.get(x * size + y); + tiles[x][y] = reg.makeTile(terrain.get(x * size + y)); } } return new TileMap(tiles, size); - } - public Point getRandomTile(int tile) { + public Point getRandomTile(int tileId) { Random random = new Random(); @@ -193,7 +191,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable { int rx = random.nextInt(width); int ry = random.nextInt(height); - if (tiles[rx][ry] == tile) { + if (tiles[rx][ry].getId() == tileId) { return new Point(rx, ry); } } diff --git a/Server/src/main/java/net/simon987/server/game/world/TilePlain.java b/Server/src/main/java/net/simon987/server/game/world/TilePlain.java new file mode 100644 index 0000000..7ad073d --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/TilePlain.java @@ -0,0 +1,11 @@ +package net.simon987.server.game.world; + +public class TilePlain extends Tile { + + public static final int ID = 0; + + @Override + public int getId() { + return ID; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/TileVoid.java b/Server/src/main/java/net/simon987/server/game/world/TileVoid.java new file mode 100644 index 0000000..92872a9 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/TileVoid.java @@ -0,0 +1,23 @@ +package net.simon987.server.game.world; + +import net.simon987.server.game.objects.GameObject; + +public class TileVoid extends Tile { + + public static final int ID = -1; + + @Override + public int getId() { + return ID; + } + + @Override + public boolean walk(GameObject object) { + return false; //Shouldn't happen! + } + + @Override + public boolean isBlocked() { + return true; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/TileWall.java b/Server/src/main/java/net/simon987/server/game/world/TileWall.java new file mode 100644 index 0000000..e451d5a --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/TileWall.java @@ -0,0 +1,23 @@ +package net.simon987.server.game.world; + +import net.simon987.server.game.objects.GameObject; + +public class TileWall extends Tile { + + public static final int ID = 1; + + @Override + public int getId() { + return ID; + } + + @Override + public boolean walk(GameObject object) { + return false; //always blocked + } + + @Override + public boolean isBlocked() { + return true; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java index 9b2186b..38c98b3 100644 --- a/Server/src/main/java/net/simon987/server/game/world/World.java +++ b/Server/src/main/java/net/simon987/server/game/world/World.java @@ -25,6 +25,7 @@ public class World implements MongoSerializable { */ private int worldSize; + //TODO: This info should be pulled from the Tile class private static final char INFO_BLOCKED = 0x8000; private static final char INFO_IRON = 0x0200; private static final char INFO_COPPER = 0x0100; @@ -68,11 +69,7 @@ public class World implements MongoSerializable { * Check if a tile is blocked, either by a game object or an impassable tile type */ public boolean isTileBlocked(int x, int y) { - - int tile = tileMap.getTileAt(x, y); - - return getGameObjectsBlockingAt(x, y).size() > 0 || tile == TileMap.WALL_TILE || - tile == TileMap.VAULT_WALL || tile == TileMap.VOID; + return getGameObjectsBlockingAt(x, y).size() > 0 || tileMap.getTileAt(x, y).isBlocked(); } /** @@ -213,11 +210,10 @@ public class World implements MongoSerializable { public String toString() { StringBuilder str = new StringBuilder("World (" + x + ", " + y + ")\n"); - int[][] tileMap = this.tileMap.getTiles(); for (int x = 0; x < worldSize; x++) { for (int y = 0; y < worldSize; y++) { - str.append(tileMap[x][y]).append(" "); + str.append(tileMap.getTileIdAt(x, y)).append(" "); } str.append("\n"); } @@ -260,22 +256,22 @@ public class World implements MongoSerializable { public char[][] getMapInfo() { char[][] mapInfo = new char[worldSize][worldSize]; - int[][] tiles = tileMap.getTiles(); //Tile for (int y = 0; y < worldSize; y++) { for (int x = 0; x < worldSize; x++) { - if (tiles[x][y] == TileMap.PLAIN_TILE) { + if (tileMap.getTileIdAt(x, y) == TilePlain.ID) { mapInfo[x][y] = 0; - } else if (tiles[x][y] == TileMap.WALL_TILE || tiles[x][y] == TileMap.VAULT_WALL) { + } else if (tileMap.getTileAt(x, y).isBlocked()) { mapInfo[x][y] = INFO_BLOCKED; - } else if (tiles[x][y] == TileMap.COPPER_TILE) { + //TODO: Tiles should have their .getMapInfo() method + } else if (tileMap.getTileIdAt(x, y) == TileCopper.ID) { mapInfo[x][y] = INFO_COPPER; - } else if (tiles[x][y] == TileMap.IRON_TILE) { + } else if (tileMap.getTileIdAt(x, y) == TileIron.ID) { mapInfo[x][y] = INFO_IRON; } } diff --git a/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java b/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java index 2f0d74c..108b2b4 100755 --- a/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java +++ b/Server/src/main/java/net/simon987/server/game/world/WorldGenerator.java @@ -159,7 +159,7 @@ public class WorldGenerator { } - world.getTileMap().getTiles()[x][y] = tile; + world.getTileMap().setTileAt(tile, x, y); } } @@ -169,18 +169,18 @@ public class WorldGenerator { for (int i = 0; i < ironCount; i++) { - Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); + Point p = world.getTileMap().getRandomTile(TilePlain.ID); if (p != null) { - world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE; + world.getTileMap().setTileAt(new TileIron(), p.x, p.y); } } for (int i = 0; i < copperCount; i++) { - Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); + Point p = world.getTileMap().getRandomTile(TilePlain.ID); if (p != null) { - world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE; + world.getTileMap().setTileAt(new TileCopper(), p.x, p.y); } } diff --git a/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java b/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java index 92c7750..8036815 100644 --- a/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/TerrainRequestHandler.java @@ -3,7 +3,6 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.io.IOException; @@ -30,20 +29,10 @@ public class TerrainRequestHandler implements MessageHandler { //todo It might be a good idea to cache this if (world != null) { - JSONObject response = new JSONObject(); - - JSONArray terrain = new JSONArray(); - - int[][] tiles = world.getTileMap().getTiles(); - for (int x = 0; x < world.getWorldSize(); x++) { - for (int y = 0; y < world.getWorldSize(); y++) { - terrain.add(tiles[y][x]); - } - } + JSONObject response = world.getTileMap().jsonSerialise(); response.put("t", "terrain"); response.put("ok", true); - response.put("terrain", terrain); response.put("size", world.getWorldSize()); user.getWebSocket().getRemote().sendString(response.toJSONString()); diff --git a/Server/src/main/resources/static/js/mar.js b/Server/src/main/resources/static/js/mar.js index db8e881..bda0f9b 100644 --- a/Server/src/main/resources/static/js/mar.js +++ b/Server/src/main/resources/static/js/mar.js @@ -1,7 +1,14 @@ var __extends = (this && this.__extends) || (function () { - var extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({__proto__: []} instanceof Array && function (d, b) { + d.__proto__ = b; + }) || + function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + }; + return extendStatics(d, b); + } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; }