List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer isVisible
public boolean isVisible()
From source file:com.prisonbreak.game.MapControlRenderer.java
@Override public void render() { if (state == STATE.ONGOING) { player.update();//from www . j a v a2 s . com moveCamera(); // check if Player is detected by the Guards for (Guard guard : guards) { if (guard instanceof PatrolGuard) { ((PatrolGuard) guard).update(); } if (guard.detectPlayer()) { // Gdx.app.log("Game over", ""); loseGame(); break; } } // search for the door in contact for (Object o : doorObjects) { RectangleMapObject r = null; if (o instanceof RectangleMapObject) { r = (RectangleMapObject) o; } if (r != null && r.getRectangle().overlaps(player.getSprite().getBoundingRectangle())) { currentDoor = r; break; } } // the door "disappears" when Player steps in // and "showed" again when Player steps out if (currentDoor != null) { int x = currentDoor.getProperties().get("lowerLeftX", Integer.class); int y = currentDoor.getProperties().get("lowerLeftY", Integer.class); String name = currentDoor.getProperties().get("tileLayerName", String.class); TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(name); // if the door has not been hidden, and Player steps in -> hide the door if (!currentDoor.getProperties().get("locked", Boolean.class) && !doorHidden && currentDoor.getRectangle().overlaps(player.getSprite().getBoundingRectangle())) { // extract the tiles tile1 = layer.getCell(x, y).getTile(); // lower left tile2 = layer.getCell(x + 1, y).getTile(); // lower right tile3 = layer.getCell(x, y + 1).getTile(); // upper left tile4 = layer.getCell(x + 1, y + 1).getTile(); // upper right // "hide" the door when Player steps in layer.getCell(x, y).setTile(null); layer.getCell(x + 1, y).setTile(null); layer.getCell(x, y + 1).setTile(null); layer.getCell(x + 1, y + 1).setTile(null); doorHidden = true; // set the flag } // if the door is currently hidden, and Player steps out -> show it if (!currentDoor.getProperties().get("locked", Boolean.class) && doorHidden && !currentDoor.getRectangle().overlaps(player.getSprite().getBoundingRectangle())) { // "show" the door when Player steps out layer.getCell(x, y).setTile(tile1); layer.getCell(x + 1, y).setTile(tile2); layer.getCell(x, y + 1).setTile(tile3); layer.getCell(x + 1, y + 1).setTile(tile4); doorHidden = false; } } // update winning state if (winGame()) state = STATE.WIN; } shapeRenderer.setProjectionMatrix(camera.combined); beginRender(); int currentLayer = 0; for (MapLayer layer : map.getLayers()) { if (layer.isVisible()) { // tile layer if (layer instanceof TiledMapTileLayer) { renderTileLayer((TiledMapTileLayer) layer); ++currentLayer; // layer to draw characters and guards if (currentLayer == drawSpritesAfterLayer) { // draw player player.getSprite().draw(this.getBatch()); // draw all the guards for (Guard guard : guards) { guard.getSprite().draw(this.getBatch()); } } // layer to draw guard's detection area // note: even though detection area is RECTANGLE // , draw a POLYGON instead (POLYGON still inside the RECTANGLE) if (currentLayer == drawDetectionAreaAfterLayer) { for (Guard guard : guards) { endRender(); // first, determine the four vertices float nearLeftX, nearLeftY, nearRightX, nearRightY, // "nearer" points in Guard's POV farLeftX, farLeftY, farRightX, farRightY; // "further" points in Guard's POV if (guard.getCurrentDirection().equalsIgnoreCase("up")) { nearLeftX = guard.getDetectArea().x + guard.getDetectArea().width / 4; nearRightX = guard.getDetectArea().x + guard.getDetectArea().width * 3 / 4; nearLeftY = nearRightY = guard.getDetectArea().y; farLeftX = guard.getDetectArea().x; farRightX = guard.getDetectArea().x + guard.getDetectArea().width; farLeftY = farRightY = guard.getDetectArea().y + guard.getDetectArea().height; } else if (guard.getCurrentDirection().equalsIgnoreCase("down")) { nearLeftX = guard.getDetectArea().x + guard.getDetectArea().width * 3 / 4; nearRightX = guard.getDetectArea().x + guard.getDetectArea().width / 4; nearLeftY = nearRightY = guard.getDetectArea().y + guard.getDetectArea().height; farLeftX = guard.getDetectArea().x + guard.getDetectArea().width; farRightX = guard.getDetectArea().x; farLeftY = farRightY = guard.getDetectArea().y; } else if (guard.getCurrentDirection().equalsIgnoreCase("right")) { nearLeftY = guard.getDetectArea().y + guard.getDetectArea().height * 3 / 4; nearRightY = guard.getDetectArea().y + guard.getDetectArea().height / 4; nearLeftX = nearRightX = guard.getDetectArea().x; farLeftY = guard.getDetectArea().y + guard.getDetectArea().height; farRightY = guard.getDetectArea().y; farLeftX = farRightX = guard.getDetectArea().x + guard.getDetectArea().width; } else if (guard.getCurrentDirection().equalsIgnoreCase("left")) { nearLeftY = guard.getDetectArea().y + guard.getDetectArea().height / 4; nearRightY = guard.getDetectArea().y + guard.getDetectArea().height * 3 / 4; nearLeftX = nearRightX = guard.getDetectArea().x + guard.getDetectArea().width; farLeftY = guard.getDetectArea().y; farRightY = guard.getDetectArea().y + guard.getDetectArea().height; farLeftX = farRightX = guard.getDetectArea().x; } else { nearLeftX = nearLeftY = nearRightX = nearRightY = 0; farLeftX = farLeftY = farRightY = farRightX = 0; } // draw detection area Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.setColor(1, 0, 0, 0.5f); if (guard.getCurrentDirection().equalsIgnoreCase("up")) { // the body (rectangle) of the polygon shapeRenderer.rect(nearLeftX, nearLeftY, nearRightX - nearLeftX, farLeftY - nearLeftY); // the left side (triangle) of the polygon shapeRenderer.triangle(nearLeftX, nearLeftY, farLeftX, farLeftY, nearLeftX, farLeftY); // the right side (triangle) of the polygon shapeRenderer.triangle(nearRightX, nearRightY, farRightX, farRightY, nearRightX, farRightY); } else if (guard.getCurrentDirection().equalsIgnoreCase("down")) { // the body (rectangle) of the polygon shapeRenderer.rect(nearRightX, farRightY, nearLeftX - nearRightX, nearRightY - farRightY); // the left side (triangle) of the polygon shapeRenderer.triangle(nearLeftX, nearLeftY, farLeftX, farLeftY, nearLeftX, farLeftY); // the right side (triangle) of the polygon shapeRenderer.triangle(nearRightX, nearRightY, farRightX, farRightY, nearRightX, farRightY); } else if (guard.getCurrentDirection().equalsIgnoreCase("right")) { // the body (rectangle) of the polygon shapeRenderer.rect(nearRightX, nearRightY, farRightX - nearRightX, nearLeftY - nearRightY); // the left side (triangle) of the polygon shapeRenderer.triangle(nearLeftX, nearLeftY, farLeftX, farLeftY, farLeftX, nearLeftY); // the right side (triangle) of the polygon shapeRenderer.triangle(nearRightX, nearRightY, farRightX, farRightY, farRightX, nearRightY); } else if (guard.getCurrentDirection().equalsIgnoreCase("left")) { // the body (rectangle) of the polygon shapeRenderer.rect(farLeftX, nearLeftY, nearLeftX - farLeftX, nearRightY - nearLeftY); // the left side (triangle) of the polygon shapeRenderer.triangle(nearLeftX, nearLeftY, farLeftX, farLeftY, farLeftX, nearLeftY); // the right side (triangle) of the polygon shapeRenderer.triangle(nearRightX, nearRightY, farRightX, farRightY, farRightX, nearRightY); } shapeRenderer.end(); Gdx.gl.glDisable(GL20.GL_BLEND); beginRender(); } } } // object layer else { for (MapObject object : layer.getObjects()) { renderObject(object); } } } } endRender(); stage.act(); stage.draw(); }
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());/*from ww w . j a v a2s .co m*/ 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(); } } } 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.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());/* w w w. ja v a 2 s . co m*/ 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(); } } } 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; }