public String getTileProperty(int tileID, String propertyName, String def) { if (tileID == 0) { return def; } TMXTileSet set = findTileSet(tileID); TMXProperty props = set.getProperties(tileID); if (props == null) { return def; } return props.getProperty(propertyName, def); }
/** * 根据TMX地图描述创建一个新层 * * @param map * @param element * @throws RuntimeException */ public TMXLayer(TMXTiledMap map, Element element) throws RuntimeException { this.map = map; name = element.getAttribute("name"); width = Integer.parseInt(element.getAttribute("width")); height = Integer.parseInt(element.getAttribute("height")); data = new int[width][height][3]; // 获得当前图层属性 Element propsElement = (Element) element.getElementsByTagName("properties").item(0); if (propsElement != null) { NodeList properties = propsElement.getElementsByTagName("property"); if (properties != null) { props = new TMXProperty(); 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); } } } Element dataNode = (Element) element.getElementsByTagName("data").item(0); String encoding = dataNode.getAttribute("encoding"); String compression = dataNode.getAttribute("compression"); // 进行base64的压缩解码 if ("base64".equals(encoding) && "gzip".equals(compression)) { try { Node cdata = dataNode.getFirstChild(); char[] enc = cdata.getNodeValue().trim().toCharArray(); byte[] dec = decodeBase64(enc); GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(dec)); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int tileId = 0; tileId |= is.read(); tileId |= is.read() << 8; tileId |= is.read() << 16; tileId |= is.read() << 24; if (tileId == 0) { data[x][y][0] = -1; data[x][y][1] = 0; data[x][y][2] = 0; } else { TMXTileSet set = map.findTileSet(tileId); if (set != null) { data[x][y][0] = set.index; data[x][y][1] = tileId - set.firstGID; } data[x][y][2] = tileId; } } } } catch (IOException e) { throw new RuntimeException("Unable to decode base64 !"); } } else { throw new RuntimeException( "Unsupport tiled map type " + encoding + "," + compression + " only gzip base64 Support !"); } }
private void load(InputStream in, String tileSetsLocation) throws RuntimeException { screenRect = LSystem.screenRect; 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(); String orient = docElement.getAttribute("orientation"); if (!orient.equals("orthogonal")) { throw new RuntimeException("Only orthogonal maps supported, found " + orient); } width = Integer.parseInt(docElement.getAttribute("width")); height = Integer.parseInt(docElement.getAttribute("height")); tileWidth = Integer.parseInt(docElement.getAttribute("tilewidth")); tileHeight = Integer.parseInt(docElement.getAttribute("tileheight")); Element propsElement = (Element) docElement.getElementsByTagName("properties").item(0); if (propsElement != null) { NodeList properties = propsElement.getElementsByTagName("property"); if (properties != null) { props = new TMXProperty(); 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) { TMXTileSet tileSet = null; TMXTileSet lastSet = null; NodeList setNodes = docElement.getElementsByTagName("tileset"); for (int i = 0; i < setNodes.getLength(); i++) { Element current = (Element) setNodes.item(i); tileSet = new TMXTileSet(this, current, true); 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); TMXLayer layer = new TMXLayer(this, current); layer.index = i; layers.add(layer); } NodeList objectGroupNodes = docElement.getElementsByTagName("objectgroup"); for (int i = 0; i < objectGroupNodes.getLength(); i++) { Element current = (Element) objectGroupNodes.item(i); TMXTileGroup objectGroup = new TMXTileGroup(current); objectGroup.index = i; objectGroups.add(objectGroup); } defWidth = screenRect.getWidth() / tileWidth; defHeight = screenRect.getHeight() / tileHeight; } catch (Exception ex) { throw new RuntimeException("Failed to parse map", ex); } }
public String getMapProperty(String propertyName, String def) { if (props == null) return def; return props.getProperty(propertyName, def); }