/** * Adds the specified tile to the list of tiles in this layer. This method enforces that no * duplicate tiles are added * * @param tile the tile to add * @param x the x grid coordinate * @param y the y grid coordinate * @return the tile that was passed in, or the duplicate existing tile if no tile was created */ protected Tile addTile(Tile tile, int x, int y) { for (Tile existingTile : tiles[x][y]) { if (existingTile.getTileID().equals(tile.getTileID())) return existingTile; } tiles[x][y].add(tile); return tile; }
/** * saves this grid to JSON format for creating area files * * @return this grid JSON data */ public JSONOrderedObject writeToJSON() { JSONOrderedObject data = new JSONOrderedObject(); Map<String, List<int[]>> out = new LinkedHashMap<String, List<int[]>>(); for (int x = 0; x < tiles.length; x++) { for (int y = 0; y < tiles[x].length; y++) { // important to create the array here each time so it isn't overwritten later int[] coords = new int[2]; coords[0] = x; coords[1] = y; TileList list = tiles[x][y]; for (Tile tile : list) { String id = tile.getTileID(); if (!out.containsKey(id)) { out.put(id, new ArrayList<int[]>()); } out.get(id).add(coords); } } } for (String tileID : out.keySet()) { data.put(tileID, out.get(tileID).toArray()); } return data; }