public TMXTileSet getTileSetByGID(int gid) { for (int i = 0; i < tileSets.size(); i++) { TMXTileSet set = (TMXTileSet) tileSets.get(i); if (set.contains(gid)) { return set; } } return null; }
/** * 渲染当前层画面到LGraphics之上 * * @param g * @param x * @param y * @param sx * @param sy * @param width * @param ty * @param isLine * @param mapTileWidth * @param mapTileHeight */ public void draw( LGraphics g, int x, int y, int sx, int sy, int width, int ty, boolean isLine, int mapTileWidth, int mapTileHeight) { int tileCount = map.getTileSetCount(); int nx, ny, sheetX, sheetY, tileOffsetY; for (int tileset = 0; tileset < tileCount; tileset++) { TMXTileSet set = null; for (int tx = 0; tx < width; tx++) { nx = sx + tx; ny = sy + ty; if ((nx < 0) || (ny < 0)) { continue; } if ((nx >= this.width) || (ny >= this.height)) { continue; } if (data[nx][ny][0] == tileset) { if (set == null) { set = map.getTileSet(tileset); } sheetX = set.getTileX(data[nx][ny][1]); sheetY = set.getTileY(data[nx][ny][1]); tileOffsetY = set.tileHeight - mapTileHeight; set.tiles.draw( g, x + (tx * mapTileWidth), y + (ty * mapTileHeight) - tileOffsetY, sheetX, sheetY); } } if (isLine) { if (set != null) { set = null; } map.rendered(ty, ty + sy, index); } } }
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); }
public LTexture getTileImage(int x, int y, int layerIndex) { TMXLayer layer = (TMXLayer) layers.get(layerIndex); int tileSetIndex = layer.data[x][y][0]; if ((tileSetIndex >= 0) && (tileSetIndex < tileSets.size())) { TMXTileSet tileSet = (TMXTileSet) tileSets.get(tileSetIndex); int sheetX = tileSet.getTileX(layer.data[x][y][1]); int sheetY = tileSet.getTileY(layer.data[x][y][1]); return tileSet.tiles.getSubImage(sheetX, sheetY); } return null; }
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); } }