/** * Find a tile for a given global tile id * * @param gid The global tile id we're looking for * @return The tileset in which that tile lives or null if the gid is not defined */ public TileSet findTileSet(int gid) { for (int i = 0; i < tileSets.size(); i++) { TileSet set = (TileSet) tileSets.get(i); if (set.contains(gid)) { return set; } } return null; }
/** * Get a propety given to a particular tile. Note that this method will not perform well and * should not be used as part of the default code path in the game loop. * * @param tileID The global ID of the tile to retrieve * @param propertyName The name of the property to retireve * @param def The default value to return * @return The value assigned to the property on the tile (or the default value if none is * supplied) */ public String getTileProperty(int tileID, String propertyName, String def) { if (tileID == 0) { return def; } TileSet set = findTileSet(tileID); Properties props = set.getProperties(tileID); if (props == null) { return def; } return props.getProperty(propertyName, def); }
/** * Gets the Image used to draw the tile at the given x and y coordinates. * * @param x The x coordinate of the tile whose image should be retrieved * @param y The y coordinate of the tile whose image should be retrieved * @param layerIndex The index of the layer on which the tile whose image should be retrieve * exists * @return The image used to draw the specified tile or null if there is no image for the * specified tile. */ public Image getTileImage(int x, int y, int layerIndex) { Layer layer = (Layer) layers.get(layerIndex); int tileSetIndex = layer.data[x][y][0]; if ((tileSetIndex >= 0) && (tileSetIndex < tileSets.size())) { TileSet tileSet = (TileSet) tileSets.get(tileSetIndex); int sheetX = tileSet.getTileX(layer.data[x][y][1]); int sheetY = tileSet.getTileY(layer.data[x][y][1]); return tileSet.tiles.getSprite(sheetX, sheetY); } return null; }
/** * Load a TilED map * * @param in The input stream from which to load the map * @param tileSetsLocation The location from which we can retrieve tileset images * @throws SlickException Indicates a failure to parse the map or find a tileset */ private void load(InputStream in, String tileSetsLocation) throws SlickException { tilesLocation = tileSetsLocation; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new ByteArrayInputStream(new byte[0])); } }); Document doc = builder.parse(in); Element docElement = doc.getDocumentElement(); if (docElement.getAttribute("orientation").equals("orthogonal")) { orientation = ORTHOGONAL; } else { orientation = ISOMETRIC; } width = Integer.parseInt(docElement.getAttribute("width")); height = Integer.parseInt(docElement.getAttribute("height")); tileWidth = Integer.parseInt(docElement.getAttribute("tilewidth")); tileHeight = Integer.parseInt(docElement.getAttribute("tileheight")); // now read the map properties Element propsElement = (Element) docElement.getElementsByTagName("properties").item(0); if (propsElement != null) { NodeList properties = propsElement.getElementsByTagName("property"); if (properties != null) { props = new Properties(); for (int p = 0; p < properties.getLength(); p++) { Element propElement = (Element) properties.item(p); String name = propElement.getAttribute("name"); String value = propElement.getAttribute("value"); props.setProperty(name, value); } } } if (loadTileSets) { TileSet tileSet = null; TileSet lastSet = null; NodeList setNodes = docElement.getElementsByTagName("tileset"); for (int i = 0; i < setNodes.getLength(); i++) { Element current = (Element) setNodes.item(i); tileSet = new TileSet(this, current, !headless); tileSet.index = i; if (lastSet != null) { lastSet.setLimit(tileSet.firstGID - 1); } lastSet = tileSet; tileSets.add(tileSet); } } NodeList layerNodes = docElement.getElementsByTagName("layer"); for (int i = 0; i < layerNodes.getLength(); i++) { Element current = (Element) layerNodes.item(i); Layer layer = new Layer(this, current); layer.index = i; layers.add(layer); } // acquire object-groups NodeList objectGroupNodes = docElement.getElementsByTagName("objectgroup"); for (int i = 0; i < objectGroupNodes.getLength(); i++) { Element current = (Element) objectGroupNodes.item(i); ObjectGroup objectGroup = new ObjectGroup(current, this); objectGroup.index = i; objectGroups.add(objectGroup); } } catch (Exception e) { Log.error(e); throw new SlickException("Failed to parse tilemap", e); } }