Пример #1
0
  protected void loadTileset(
      TiledMap map,
      Element element,
      FileHandle tmxFile,
      AtlasResolver resolver,
      AtlasTiledMapLoaderParameters parameter) {
    if (element.getName().equals("tileset")) {
      String name = element.get("name", null);
      int firstgid = element.getIntAttribute("firstgid", 1);
      int tilewidth = element.getIntAttribute("tilewidth", 0);
      int tileheight = element.getIntAttribute("tileheight", 0);
      int spacing = element.getIntAttribute("spacing", 0);
      int margin = element.getIntAttribute("margin", 0);
      String source = element.getAttribute("source", null);

      String imageSource = "";
      int imageWidth = 0, imageHeight = 0;

      FileHandle image = null;
      if (source != null) {
        FileHandle tsx = getRelativeFileHandle(tmxFile, source);
        try {
          element = xml.parse(tsx);
          name = element.get("name", null);
          tilewidth = element.getIntAttribute("tilewidth", 0);
          tileheight = element.getIntAttribute("tileheight", 0);
          spacing = element.getIntAttribute("spacing", 0);
          margin = element.getIntAttribute("margin", 0);
          imageSource = element.getChildByName("image").getAttribute("source");
          imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
          imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
        } catch (IOException e) {
          throw new GdxRuntimeException("Error parsing external tileset.");
        }
      } else {
        imageSource = element.getChildByName("image").getAttribute("source");
        imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
        imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
      }

      // get the TextureAtlas for this tileset
      TextureAtlas atlas = null;
      String regionsName = "";
      if (map.getProperties().containsKey("atlas")) {
        FileHandle atlasHandle =
            getRelativeFileHandle(tmxFile, map.getProperties().get("atlas", String.class));
        atlasHandle = resolve(atlasHandle.path());
        atlas = resolver.getAtlas(atlasHandle.path());
        regionsName = atlasHandle.nameWithoutExtension();

        if (parameter != null && parameter.forceTextureFilters) {
          for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
          }
        }
      }

      TiledMapTileSet tileset = new TiledMapTileSet();
      MapProperties props = tileset.getProperties();
      tileset.setName(name);
      props.put("firstgid", firstgid);
      props.put("imagesource", imageSource);
      props.put("imagewidth", imageWidth);
      props.put("imageheight", imageHeight);
      props.put("tilewidth", tilewidth);
      props.put("tileheight", tileheight);
      props.put("margin", margin);
      props.put("spacing", spacing);

      Array<AtlasRegion> regions = atlas.findRegions(regionsName);
      for (AtlasRegion region : regions) {
        // handle unused tile ids
        if (region != null) {
          StaticTiledMapTile tile = new StaticTiledMapTile(region);

          if (!yUp) {
            region.flip(false, true);
          }

          int tileid = firstgid + region.index;
          tile.setId(tileid);
          tileset.putTile(tileid, tile);
        }
      }

      Array<Element> tileElements = element.getChildrenByName("tile");

      for (Element tileElement : tileElements) {
        int localtid = tileElement.getIntAttribute("id", 0);
        TiledMapTile tile = tileset.getTile(firstgid + localtid);
        if (tile != null) {
          String terrain = tileElement.getAttribute("terrain", null);
          if (terrain != null) {
            tile.getProperties().put("terrain", terrain);
          }
          String probability = tileElement.getAttribute("probability", null);
          if (probability != null) {
            tile.getProperties().put("probability", probability);
          }
          Element properties = tileElement.getChildByName("properties");
          if (properties != null) {
            loadProperties(tile.getProperties(), properties);
          }
        }
      }

      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }
      map.getTileSets().addTileSet(tileset);
    }
  }
Пример #2
0
  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);
    }
  }