List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer getProperties
public MapProperties getProperties()
From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java
License:Apache License
/** * Reads a file describing the collision boundaries that should be set * per-tile and adds static bodies to the boxd world. * /*ww w. j a v a 2s . co m*/ * @param collisionsFile * @param world * @param pixelsPerMeter * the pixels per meter scale used for this world */ public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) { /** * Detect the tiles and dynamically create a representation of the map * layout, for collision detection. Each tile has its own collision * rules stored in an associated file. * * The file contains lines in this format (one line per type of tile): * tileNumber XxY,XxY XxY,XxY * * Ex: * * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31 * * For a 32x32 tileset, the above describes one line segment for tile #3 * and two for tile #4. Tile #3 has a line segment across the top. Tile * #1 has a line segment across most of the top and a line segment from * the top to the bottom, 30 pixels in. */ // FileHandle fh = Gdx.files.internal(collisionsFile); // String collisionFile = fh.readString(); // String lines[] = collisionFile.split("\\r?\\n"); // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new // HashMap<Integer, ArrayList<LineSegment>>(); // /** // * Some locations on the map (perhaps most locations) are "undefined", // * empty space, and will have the tile type 0. This code adds an empty // * list of line segments for this "default" tile. // */ // tileCollisionJoints.put(Integer.valueOf(0), new // ArrayList<LineSegment>()); // for (int n = 0; n < lines.length; n++) { // String cols[] = lines[n].split(" "); // int tileNo = Integer.parseInt(cols[0]); // // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>(); // // for (int m = 1; m < cols.length; m++) { // String coords[] = cols[m].split(","); // // String start[] = coords[0].split("x"); // String end[] = coords[1].split("x"); // // tmp.add(new LineSegment(Integer.parseInt(start[0]), // Integer.parseInt(start[1]), // Integer.parseInt(end[0]), Integer.parseInt(end[1]), "")); // } // // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp); // } ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>(); for (int l = 0; l < getMap().getLayers().getCount(); l++) { MapLayer nextLayer = getMap().getLayers().get(l); if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) { TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer; if (layer.getProperties().containsKey("collide")) { for (int y = 0; y < layer.getHeight(); y++) { for (int x = 0; x < layer.getWidth(); x++) { Cell tile = layer.getCell(x, y); if (tile != null) { int tileID = tile.getTile().getId(); int start = 0; String type = ""; if (tile.getTile().getProperties().containsKey("type")) { type = (String) getMap().getTileSets().getTile(tileID).getProperties() .get("type"); } if (layer.getProperties().containsKey("WALLS")) { type = "WALLS"; addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32, collisionLineSegments, type); } else if (layer.getProperties().containsKey("STAIRS")) { if (!type.equals("LEFTSTAIRS")) { type = "STAIRS"; addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } else { addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32, collisionLineSegments, type); } } else if (layer.getProperties().containsKey("PLATFORMS")) { type = "PLATFORMS"; addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } } } } } } else { MapObjects objects = nextLayer.getObjects(); for (MapObject o : objects) { RectangleMapObject rect = (RectangleMapObject) o; if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) { Rectangle shape = rect.getRectangle(); BodyDef nodeBodyDef = new BodyDef(); nodeBodyDef.type = BodyDef.BodyType.StaticBody; nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX, (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX); Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef); if (!nextLayer.getName().equals("DOORS")) { String set = (String) rect.getProperties().get("set"); nodeBody.setUserData(new AINode(Integer.parseInt(set))); } else { if (rect.getProperties().containsKey("to_level") && rect.getProperties().containsKey("exitX") && rect.getProperties().containsKey("exitY")) { String to_level = (String) rect.getProperties().get("to_level"); String xPos = (String) rect.getProperties().get("exitX"); String yPos = (String) rect.getProperties().get("exitY"); nodeBody.setUserData(new Door(to_level, xPos, yPos)); } else { nodeBody.setUserData(new Door("9999", "1024", "256")); } } PolygonShape nodeShape = new PolygonShape(); nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX, shape.height * 0.5f * Util.PIXEL_TO_BOX); FixtureDef fixture = new FixtureDef(); fixture.shape = nodeShape; fixture.isSensor = true; fixture.density = 0; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; nodeBody.createFixture(fixture); nodeShape.dispose(); } } } } int platnum = 0; for (LineSegment lineSegment : collisionLineSegments) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; groundBodyDef.position.set(0, 0); Body groundBody = GamePlayManager.world.createBody(groundBodyDef); if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) { groundBody.setUserData( new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end())); } else if (lineSegment.type.equals("WALLS")) { groundBody.setUserData(new Entity().setType(EntityType.WALL)); } else { Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()); groundBody.setUserData(plat); if (GamePlayManager.lowestPlatInLevel == null || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) { GamePlayManager.lowestPlatInLevel = plat; } } EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter), lineSegment.end().scl(1 / pixelsPerMeter)); FixtureDef fixture = new FixtureDef(); fixture.shape = environmentShape; fixture.isSensor = true; fixture.density = 1f; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES; groundBody.createFixture(fixture); environmentShape.dispose(); } /** * Drawing a boundary around the entire map. We can't use a box because * then the world objects would be inside and the physics engine would * try to push them out. */ TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3); BodyDef bodydef = new BodyDef(); bodydef.type = BodyType.StaticBody; bodydef.position.set(0, 0); Body body = GamePlayManager.world.createBody(bodydef); // left wall EdgeShape mapBounds = new EdgeShape(); if (level == 1) { mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new Entity().setType(EntityType.WALL)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } else { mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f), new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new LevelWall(level - 1)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } // right wall body = GamePlayManager.world.createBody(bodydef); body.setUserData(new LevelWall(level + 1)); EdgeShape mapBounds2 = new EdgeShape(); mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX), new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); Fixture fixture = body.createFixture(mapBounds2, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); // roof body = GamePlayManager.world.createBody(bodydef); body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32), new Vector2(layer.getWidth() * 32, layer.getHeight()))); EdgeShape mapBounds3 = new EdgeShape(); mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX), new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); fixture = body.createFixture(mapBounds3, 0); fixture.setFilterData(filter); mapBounds.dispose(); mapBounds2.dispose(); mapBounds3.dispose(); }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TmxMapWriter.java
License:Apache License
/** @param layer the {@link TiledMapTileLayer} to write in TMX format * @param format the {@link Format} to use * @return this {@link TmxMapWriter} */ public TmxMapWriter tmx(TiledMapTileLayer layer, Format format) throws IOException { element("layer"); attribute("name", layer.getName()); attribute("width", layer.getWidth()); attribute("height", layer.getHeight()); attribute("visible", layer.isVisible() ? 1 : 0); attribute("opacity", layer.getOpacity()); tmx(layer.getProperties()); element("data"); if (format == XML) { attribute("encoding", "xml"); for (int y = layer.getHeight() - 1; y > -1; y--) for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); if (cell != null) { TiledMapTile tile = cell.getTile(); if (tile == null) continue; element("tile"); attribute("gid", tile.getId()); pop();// w w w . j a va2 s .c om } } } else if (format == CSV) { attribute("encoding", "csv"); StringBuilder csv = new StringBuilder(); for (int y = layer.getHeight() - 1; y > -1; y--) { for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); TiledMapTile tile = cell != null ? cell.getTile() : null; csv.append(tile != null ? tile.getId() : 0); if (x + 1 < layer.getWidth() || y - 1 > -1) csv.append(','); } csv.append('\n'); } append('\n').append(csv); } else if (format == Base64 || format == Base64Zlib || format == Base64Gzip) { attribute("encoding", "base64"); if (format == Base64Zlib) attribute("compression", "zlib"); else if (format == Base64Gzip) attribute("compression", "gzip"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream out = format == Base64Zlib ? new DeflaterOutputStream(baos) : format == Base64Gzip ? new GZIPOutputStream(baos) : baos; final short LAST_BYTE = 0xFF; for (int y = layer.getHeight() - 1; y > -1; y--) for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); TiledMapTile tile = cell != null ? cell.getTile() : null; int gid = tile != null ? tile.getId() : 0; out.write(gid & LAST_BYTE); out.write(gid >> 8 & LAST_BYTE); out.write(gid >> 16 & LAST_BYTE); out.write(gid >> 24 & LAST_BYTE); } if (out instanceof DeflaterOutputStream) ((DeflaterOutputStream) out).finish(); out.close(); baos.close(); append('\n').append(String.valueOf(Base64Coder.encode(baos.toByteArray()))).append('\n'); } pop(); pop(); return this; }
From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java
License:Apache License
protected void loadTileLayer(TiledMap map, Element element) { if (element.getName().equals("layer")) { String name = element.getAttribute("name", null); int width = element.getIntAttribute("width", 0); int height = element.getIntAttribute("height", 0); int tileWidth = element.getParent().getIntAttribute("tilewidth", 0); int tileHeight = element.getParent().getIntAttribute("tileheight", 0); boolean visible = element.getIntAttribute("visible", 1) == 1; float opacity = element.getFloatAttribute("opacity", 1.0f); TiledMapTileLayer layer = new TiledMapTileLayer(width, height, tileWidth, tileHeight); layer.setVisible(visible);// w w w. j a v a2 s .co m layer.setOpacity(opacity); layer.setName(name); TiledMapTileSets tilesets = map.getTileSets(); Element data = element.getChildByName("data"); String encoding = data.getAttribute("encoding", null); String compression = data.getAttribute("compression", null); if (encoding == null) { // no 'encoding' attribute means that the encoding is XML throw new GdxRuntimeException("Unsupported encoding (XML) for TMX Layer Data"); } if (encoding.equals("csv")) { String[] array = data.getText().split(","); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int id = (int) Long.parseLong(array[y * width + x].trim()); final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0); final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0); final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0); id = id & ~MASK_CLEAR; tilesets.getTile(id); TiledMapTile tile = tilesets.getTile(id); if (tile != null) { Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally); cell.setTile(tile); layer.setCell(x, yUp ? height - 1 - y : y, cell); } } } } else { if (encoding.equals("base64")) { byte[] bytes = Base64Coder.decode(data.getText()); if (compression == null) { int read = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int id = unsignedByteToInt(bytes[read++]) | unsignedByteToInt(bytes[read++]) << 8 | unsignedByteToInt(bytes[read++]) << 16 | unsignedByteToInt(bytes[read++]) << 24; final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0); final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0); final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0); id = id & ~MASK_CLEAR; tilesets.getTile(id); TiledMapTile tile = tilesets.getTile(id); if (tile != null) { Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally); cell.setTile(tile); layer.setCell(x, yUp ? height - 1 - y : y, cell); } } } } else if (compression.equals("gzip")) { GZIPInputStream GZIS = null; try { GZIS = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length); } catch (IOException e) { throw new GdxRuntimeException( "Error Reading TMX Layer Data - IOException: " + e.getMessage()); } byte[] temp = new byte[4]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { try { GZIS.read(temp, 0, 4); int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8 | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24; final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0); final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0); final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0); id = id & ~MASK_CLEAR; tilesets.getTile(id); TiledMapTile tile = tilesets.getTile(id); if (tile != null) { Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally); cell.setTile(tile); layer.setCell(x, yUp ? height - 1 - y : y, cell); } } catch (IOException e) { throw new GdxRuntimeException("Error Reading TMX Layer Data.", e); } } } } else if (compression.equals("zlib")) { Inflater zlib = new Inflater(); byte[] temp = new byte[4]; zlib.setInput(bytes, 0, bytes.length); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { try { zlib.inflate(temp, 0, 4); int id = unsignedByteToInt(temp[0]) | unsignedByteToInt(temp[1]) << 8 | unsignedByteToInt(temp[2]) << 16 | unsignedByteToInt(temp[3]) << 24; final boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0); final boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0); final boolean flipDiagonally = ((id & FLAG_FLIP_DIAGONALLY) != 0); id = id & ~MASK_CLEAR; tilesets.getTile(id); if (id != 0) { String inset = "NONE"; if (tilesets.getTileSet(0).getTile(id) != null) { inset = tilesets.getTileSet(0).getName(); } else if (tilesets.getTileSet(1).getTile(id) != null) { inset = tilesets.getTileSet(1).getName(); } System.out.println("need tile " + id + inset); } TiledMapTile tile = tilesets.getTile(id); if (tile != null) { Cell cell = createTileLayerCell(flipHorizontally, flipVertically, flipDiagonally); cell.setTile(tile); layer.setCell(x, yUp ? height - 1 - y : y, cell); } } catch (DataFormatException e) { throw new GdxRuntimeException("Error Reading TMX Layer Data.", e); } } } } } else { // any other value of 'encoding' is one we're not aware of, probably a feature of a future version of Tiled // or another editor throw new GdxRuntimeException("Unrecognised encoding (" + encoding + ") for TMX Layer Data"); } } Element properties = element.getChildByName("properties"); if (properties != null) { loadProperties(layer.getProperties(), properties); } map.getLayers().add(layer); } }
From source file:de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter.java
License:Apache License
/** @param layer the {@link com.badlogic.gdx.maps.tiled.TiledMapTileLayer} to write in TMX format * @param format the {@link de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter.Format} to use * @return this {@link de.fhkoeln.game.utils.libgdx.maps.TmxMapWriter} */ public TmxMapWriter tmx(TiledMapTileLayer layer, Format format) throws IOException { element("layer"); attribute("name", layer.getName()); attribute("width", layer.getWidth()); attribute("height", layer.getHeight()); attribute("visible", layer.isVisible() ? 1 : 0); attribute("opacity", layer.getOpacity()); tmx(layer.getProperties()); element("data"); if (format == Format.XML) { attribute("encoding", "xml"); for (int y = layer.getHeight() - 1; y > -1; y--) for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); if (cell != null) { TiledMapTile tile = cell.getTile(); if (tile == null) continue; element("tile"); attribute("gid", tile.getId()); pop();/* w w w . j a v a 2 s . c om*/ } } } else if (format == Format.CSV) { attribute("encoding", "csv"); StringBuilder csv = new StringBuilder(); for (int y = layer.getHeight() - 1; y > -1; y--) { for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); TiledMapTile tile = cell != null ? cell.getTile() : null; csv.append(tile != null ? tile.getId() : 0); if (x + 1 < layer.getWidth() || y - 1 > -1) csv.append(','); } csv.append('\n'); } append('\n').append(csv); } else if (format == Format.Base64 || format == Format.Base64Zlib || format == Format.Base64Gzip) { attribute("encoding", "base64"); if (format == Format.Base64Zlib) attribute("compression", "zlib"); else if (format == Format.Base64Gzip) attribute("compression", "gzip"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream out = format == Format.Base64Zlib ? new DeflaterOutputStream(baos) : format == Format.Base64Gzip ? new GZIPOutputStream(baos) : baos; final short LAST_BYTE = 0xFF; for (int y = layer.getHeight() - 1; y > -1; y--) for (int x = 0; x < layer.getWidth(); x++) { Cell cell = layer.getCell(x, y); TiledMapTile tile = cell != null ? cell.getTile() : null; int gid = tile != null ? tile.getId() : 0; out.write(gid & LAST_BYTE); out.write(gid >> 8 & LAST_BYTE); out.write(gid >> 16 & LAST_BYTE); out.write(gid >> 24 & LAST_BYTE); } if (out instanceof DeflaterOutputStream) ((DeflaterOutputStream) out).finish(); out.close(); baos.close(); append('\n').append(String.valueOf(Base64Coder.encode(baos.toByteArray()))).append('\n'); } pop(); pop(); return this; }