protected FileHandle loadAtlas(Element root, FileHandle tmxFile) throws IOException {

    Element e = root.getChildByName("properties");
    Array<FileHandle> atlases = new Array<FileHandle>();

    if (e != null) {
      for (Element property : e.getChildrenByName("property")) {
        String name = property.getAttribute("name", null);
        String value = property.getAttribute("value", null);
        if (name.equals("atlas")) {
          if (value == null) {
            value = property.getText();
          }

          if (value == null || value.length() == 0) {
            // keep trying until there are no more atlas properties
            continue;
          }

          return getRelativeFileHandle(tmxFile, value);
        }
      }
    }

    return null;
  }
Exemple #2
0
 /**
  * Loads the map data, given the XML root element and an {@link ImageResolver} used to return the
  * tileset Textures
  *
  * @param root the XML root element
  * @param tmxFile the Filehandle of the tmx file
  * @param imageResolver the {@link ImageResolver}
  * @return the {@link TiledMap}
  */
 private TiledMap loadMap(Element root, FileHandle tmxFile, ImageResolver imageResolver) {
   TiledMap map = new TiledMap();
   Element properties = root.getChildByName("properties");
   if (properties != null) {
     loadProperties(map.getProperties(), properties);
   }
   Element tilesheets = root.getChildByName("TileSheets");
   for (Element tilesheet : tilesheets.getChildrenByName("TileSheet")) {
     loadTileSheet(map, tilesheet, tmxFile, imageResolver);
   }
   Element layers = root.getChildByName("Layers");
   for (Element layer : layers.getChildrenByName("Layer")) {
     loadLayer(map, layer);
   }
   return map;
 }
  @Override
  public Array<AssetDescriptor> getDependencies(
      String fileName, FileHandle fileHandle, AtlasTiledMapLoaderParameters parameter) {
    Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
    FileHandle tmxFile = resolve(fileName);
    try {
      root = xml.parse(tmxFile);

      Element properties = root.getChildByName("properties");
      if (properties != null) {
        for (Element property : properties.getChildrenByName("property")) {
          String name = property.getAttribute("name");
          String value = property.getAttribute("value");
          if (name.startsWith("atlas")) {
            FileHandle atlasHandle = getRelativeFileHandle(tmxFile, value);
            atlasHandle = resolve(atlasHandle.path());
            dependencies.add(new AssetDescriptor(atlasHandle.path(), TextureAtlas.class));
          }
        }
      }
    } catch (IOException e) {
      throw new GdxRuntimeException("Unable to parse .tmx file.");
    }
    return dependencies;
  }
Exemple #4
0
  public Joint(Element animationElement) {
    Array<Element> nodes = animationElement.getChildrenByName("node");

    Element jointNode = null;
    for (int i = 0; i < nodes.size; i++) {
      Element e = nodes.get(i);
      Array<Element> subNodes = e.getChildrenByName("node");
      if (subNodes.size > 0 && subNodes.get(0).getAttribute("type").equalsIgnoreCase("JOINT")) {
        jointNode = e;
        break;
      }
    }

    if (jointNode == null) {
      throw new GdxRuntimeException("no Joint element in scene");
    }

    joint = getJoint(jointNode, true);
  }
Exemple #5
0
 /**
  * Loads the tilesets
  *
  * @param root the root XML element
  * @return a list of filenames for images containing tiles
  * @throws IOException
  */
 private Array<FileHandle> loadTileSheets(Element root, FileHandle tideFile) throws IOException {
   Array<FileHandle> images = new Array<FileHandle>();
   Element tilesheets = root.getChildByName("TileSheets");
   for (Element tileset : tilesheets.getChildrenByName("TileSheet")) {
     Element imageSource = tileset.getChildByName("ImageSource");
     FileHandle image = getRelativeFileHandle(tideFile, imageSource.getText());
     images.add(image);
   }
   return images;
 }
 protected void loadProperties(MapProperties properties, Element element) {
   if (element.getName().equals("properties")) {
     for (Element property : element.getChildrenByName("property")) {
       String name = property.getAttribute("name", null);
       String value = property.getAttribute("value", null);
       if (value == null) {
         value = property.getText();
       }
       properties.put(name, value);
     }
   }
 }
  protected void loadObjectGroup(TiledMap map, Element element) {
    if (element.getName().equals("objectgroup")) {
      String name = element.getAttribute("name", null);
      MapLayer layer = new MapLayer();
      layer.setName(name);
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(layer.getProperties(), properties);
      }

      for (Element objectElement : element.getChildrenByName("object")) {
        loadObject(layer, objectElement);
      }

      map.getLayers().add(layer);
    }
  }
Exemple #8
0
  private void loadProperties(MapProperties properties, Element element) {
    if (element.getName().equals("Properties")) {
      for (Element property : element.getChildrenByName("Property")) {
        String key = property.getAttribute("Key", null);
        String type = property.getAttribute("Type", null);
        String value = property.getText();

        if (type.equals("Int32")) {
          properties.put(key, Integer.parseInt(value));
        } else if (type.equals("String")) {
          properties.put(key, value);
        } else if (type.equals("Boolean")) {
          properties.put(key, value.equalsIgnoreCase("true"));
        } else {
          properties.put(key, value);
        }
      }
    }
  }
Exemple #9
0
  SkeletonJoint getJoint(Element jointNode, boolean isRootNode) {
    SkeletonJoint joint = new SkeletonJoint();
    joint.name = jointNode.getAttribute("id");
    if (!isRootNode) {
      Element matrixElement = jointNode.getChildByName("matrix");
      if (matrixElement != null) {
        Matrix4 m = getMatrix(matrixElement);
        m.getTranslation(joint.position);
        m.getRotation(joint.rotation);
        // TODO: get scale from matrix
      }
    }

    Array<Element> nodes = jointNode.getChildrenByName("node");
    for (int i = 0; i < nodes.size; i++) {
      SkeletonJoint child = getJoint(nodes.get(i), false);
      if (!isRootNode) {
        child.parent = joint;
      }
      joint.children.add(child);
    }

    return joint;
  }
  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);
    }
  }
Exemple #11
0
  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);
    }
  }