コード例 #1
0
ファイル: TideMapLoader.java プロジェクト: acr21/libgdx
  private void loadLayer(TiledMap map, Element element) {
    if (element.getName().equals("Layer")) {
      String id = element.getAttribute("Id");
      String visible = element.getAttribute("Visible");

      Element dimensions = element.getChildByName("Dimensions");
      String layerSize = dimensions.getAttribute("LayerSize");
      String tileSize = dimensions.getAttribute("TileSize");

      String[] layerSizeParts = layerSize.split(" x ");
      int layerSizeX = Integer.parseInt(layerSizeParts[0]);
      int layerSizeY = Integer.parseInt(layerSizeParts[1]);

      String[] tileSizeParts = tileSize.split(" x ");
      int tileSizeX = Integer.parseInt(tileSizeParts[0]);
      int tileSizeY = Integer.parseInt(tileSizeParts[1]);

      TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
      Element tileArray = element.getChildByName("TileArray");
      Array<Element> rows = tileArray.getChildrenByName("Row");
      TiledMapTileSets tilesets = map.getTileSets();
      TiledMapTileSet currentTileSet = null;
      int firstgid = 0;
      int x, y;
      for (int row = 0, rowCount = rows.size; row < rowCount; row++) {
        Element currentRow = rows.get(row);
        y = rowCount - 1 - row;
        x = 0;
        for (int child = 0, childCount = currentRow.getChildCount(); child < childCount; child++) {
          Element currentChild = currentRow.getChild(child);
          String name = currentChild.getName();
          if (name.equals("TileSheet")) {
            currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
            firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
          } else if (name.equals("Null")) {
            x += currentChild.getIntAttribute("Count");
          } else if (name.equals("Static")) {
            Cell cell = new Cell();
            cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
            layer.setCell(x++, y, cell);
          } else if (name.equals("Animated")) {
            // Create an AnimatedTile
            int interval = currentChild.getInt("Interval");
            Element frames = currentChild.getChildByName("Frames");
            Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>();
            for (int frameChild = 0, frameChildCount = frames.getChildCount();
                frameChild < frameChildCount;
                frameChild++) {
              Element frame = frames.getChild(frameChild);
              String frameName = frame.getName();
              if (frameName.equals("TileSheet")) {
                currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
                firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
              } else if (frameName.equals("Static")) {
                frameTiles.add(
                    (StaticTiledMapTile)
                        currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
              }
            }
            Cell cell = new Cell();
            cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
            layer.setCell(x++, y, cell); // TODO: Reuse existing animated tiles
          }
        }
      }
      map.getLayers().add(layer);
    }
  }
コード例 #2
0
  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);
      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")) {
            throw new GdxRuntimeException("GZIP compression not supported in GWT backend");
          } else if (compression.equals("zlib")) {
            throw new GdxRuntimeException("ZLIB compression not supported in GWT backend");
          }
        } 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);
    }
  }
コード例 #3
0
ファイル: TideMapLoader.java プロジェクト: acr21/libgdx
  private void loadTileSheet(
      TiledMap map, Element element, FileHandle tideFile, ImageResolver imageResolver) {
    if (element.getName().equals("TileSheet")) {
      String id = element.getAttribute("Id");
      String description = element.getChildByName("Description").getText();
      String imageSource = element.getChildByName("ImageSource").getText();

      Element alignment = element.getChildByName("Alignment");
      String sheetSize = alignment.getAttribute("SheetSize");
      String tileSize = alignment.getAttribute("TileSize");
      String margin = alignment.getAttribute("Margin");
      String spacing = alignment.getAttribute("Spacing");

      String[] sheetSizeParts = sheetSize.split(" x ");
      int sheetSizeX = Integer.parseInt(sheetSizeParts[0]);
      int sheetSizeY = Integer.parseInt(sheetSizeParts[1]);

      String[] tileSizeParts = tileSize.split(" x ");
      int tileSizeX = Integer.parseInt(tileSizeParts[0]);
      int tileSizeY = Integer.parseInt(tileSizeParts[1]);

      String[] marginParts = margin.split(" x ");
      int marginX = Integer.parseInt(marginParts[0]);
      int marginY = Integer.parseInt(marginParts[1]);

      String[] spacingParts = margin.split(" x ");
      int spacingX = Integer.parseInt(spacingParts[0]);
      int spacingY = Integer.parseInt(spacingParts[1]);

      FileHandle image = getRelativeFileHandle(tideFile, imageSource);
      TextureRegion texture = imageResolver.getImage(image.path());

      // TODO: Actually load the tilesheet
      // Need to make global ids as Tide doesn't have global ids.
      TiledMapTileSets tilesets = map.getTileSets();
      int firstgid = 1;
      for (TiledMapTileSet tileset : tilesets) {
        firstgid += tileset.size();
      }

      TiledMapTileSet tileset = new TiledMapTileSet();
      tileset.setName(id);
      tileset.getProperties().put("firstgid", firstgid);
      int gid = firstgid;

      int stopWidth = texture.getRegionWidth() - tileSizeX;
      int stopHeight = texture.getRegionHeight() - tileSizeY;

      for (int y = marginY; y <= stopHeight; y += tileSizeY + spacingY) {
        for (int x = marginX; x <= stopWidth; x += tileSizeX + spacingX) {
          TiledMapTile tile =
              new StaticTiledMapTile(new TextureRegion(texture, x, y, tileSizeX, tileSizeY));
          tile.setId(gid);
          tileset.putTile(gid++, tile);
        }
      }

      Element properties = element.getChildByName("Properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }

      tilesets.addTileSet(tileset);
    }
  }