// ---------------------------------------------------------------------- public static AbstractRoomGenerator load(Element xml) { Class<AbstractRoomGenerator> c = ClassMap.get(xml.getName().toUpperCase()); AbstractRoomGenerator type = null; try { type = ClassReflection.newInstance(c); } catch (Exception e) { System.err.println(xml.getName()); e.printStackTrace(); } type.parse(xml); return type; }
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); } }
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; }
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); }
protected TiledMap loadMap( Element root, FileHandle tmxFile, AtlasResolver resolver, AtlasTiledMapLoaderParameters parameter) { TiledMap map = new TiledMap(); String mapOrientation = root.getAttribute("orientation", null); int mapWidth = root.getIntAttribute("width", 0); int mapHeight = root.getIntAttribute("height", 0); int tileWidth = root.getIntAttribute("tilewidth", 0); int tileHeight = root.getIntAttribute("tileheight", 0); String mapBackgroundColor = root.getAttribute("backgroundcolor", null); MapProperties mapProperties = map.getProperties(); if (mapOrientation != null) { mapProperties.put("orientation", mapOrientation); } mapProperties.put("width", mapWidth); mapProperties.put("height", mapHeight); mapProperties.put("tilewidth", tileWidth); mapProperties.put("tileheight", tileHeight); if (mapBackgroundColor != null) { mapProperties.put("backgroundcolor", mapBackgroundColor); } mapWidthInPixels = mapWidth * tileWidth; mapHeightInPixels = mapHeight * tileHeight; for (int i = 0, j = root.getChildCount(); i < j; i++) { Element element = root.getChild(i); String elementName = element.getName(); if (elementName.equals("properties")) { loadProperties(map.getProperties(), element); } else if (elementName.equals("tileset")) { loadTileset(map, element, tmxFile, resolver, parameter); } else if (elementName.equals("layer")) { loadTileLayer(map, element); } else if (elementName.equals("objectgroup")) { loadObjectGroup(map, element); } } return map; }
public Matrix4 getMatrix(Element matrix) { Matrix4 m = new Matrix4(); // read elements into data[] String[] tokens = matrix.getText().split("\\s+"); for (int i = 0; i < tokens.length; i++) { m.val[i] = Float.parseFloat(tokens[i]); } m.tra(); return m; }
/** * 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; }
@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; }
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; }
@Override public void parse(Element xmlElement) { String path = xmlElement.getAttribute("Path"); XmlReader importXml = new XmlReader(); Element importXmlElement = null; try { importXmlElement = importXml.parse(Gdx.files.internal("AI/" + path)); } catch (IOException e) { e.printStackTrace(); } try { Class<BehaviourTreeNode> c = ClassMap.get(importXmlElement.getName().toUpperCase()); BehaviourTreeNode node = (BehaviourTreeNode) ClassReflection.newInstance(c); setNode(node); node.parse(importXmlElement); } catch (ReflectionException e) { e.printStackTrace(); } }
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); } } }
/** * 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; }
public Element getElement(ObjectType type, String id) { Element temp; switch (type) { case MAP: temp = ROOT.getChildByName("maps"); return temp.getChildByName(id); case ITEM: temp = ROOT.getChildByName("items"); return temp.getChildByName(id); case INTERACTION: temp = ROOT.getChildByName("interactions"); return temp.getChildByName(id); default: return null; } }
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); } } } }
public Array<Element> getList(String tag) { return ROOT.getChildrenByNameRecursively(tag); }
public Element getElement(String id) { if (id.equalsIgnoreCase("maps") || id.equalsIgnoreCase("items") || id.equalsIgnoreCase("entities")) return ROOT; else return ROOT.getChildByNameRecursive(id); }
protected void loadObject(MapLayer layer, Element element) { if (element.getName().equals("object")) { MapObject object = null; int x = element.getIntAttribute("x", 0); int y = (yUp ? mapHeightInPixels - element.getIntAttribute("y", 0) : element.getIntAttribute("y", 0)); int width = element.getIntAttribute("width", 0); int height = element.getIntAttribute("height", 0); if (element.getChildCount() > 0) { Element child = null; if ((child = element.getChildByName("polygon")) != null) { String[] points = child.getAttribute("points").split(" "); float[] vertices = new float[points.length * 2]; for (int i = 0; i < points.length; i++) { String[] point = points[i].split(","); vertices[i * 2] = Integer.parseInt(point[0]); vertices[i * 2 + 1] = Integer.parseInt(point[1]); if (yUp) { vertices[i * 2 + 1] *= -1; } } Polygon polygon = new Polygon(vertices); polygon.setPosition(x, y); object = new PolygonMapObject(polygon); } else if ((child = element.getChildByName("polyline")) != null) { String[] points = child.getAttribute("points").split(" "); float[] vertices = new float[points.length * 2]; for (int i = 0; i < points.length; i++) { String[] point = points[i].split(","); vertices[i * 2] = Integer.parseInt(point[0]); vertices[i * 2 + 1] = Integer.parseInt(point[1]); if (yUp) { vertices[i * 2 + 1] *= -1; } } Polyline polyline = new Polyline(vertices); polyline.setPosition(x, y); object = new PolylineMapObject(polyline); } else if ((child = element.getChildByName("ellipse")) != null) { object = new EllipseMapObject(x, yUp ? y - height : y, width, height); } } if (object == null) { object = new RectangleMapObject(x, yUp ? y - height : y, width, height); } object.setName(element.getAttribute("name", null)); String type = element.getAttribute("type", null); if (type != null) { object.getProperties().put("type", type); } int gid = element.getIntAttribute("gid", -1); if (gid != -1) { object.getProperties().put("gid", gid); } object.getProperties().put("x", x); object.getProperties().put("y", yUp ? y - height : y); object.setVisible(element.getIntAttribute("visible", 1) == 1); Element properties = element.getChildByName("properties"); if (properties != null) { loadProperties(object.getProperties(), properties); } layer.getObjects().add(object); } }
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); } }
public void parseLevel(FileHandle file) throws Exception { Element root = new XmlReader().parse(file); this.pathway = Pathway.parsePathway(root); // Loading game objects Element entities = root.getChildByName("entities"); this.gameObjects = new ArrayList<GameObject>(); if (entities != null) { for (int i = 0; i < entities.getChildCount(); i++) { Element gameObject = entities.getChild(i); this.gameObjects.add(GameObject.parseGameObject(gameObject)); } } // Loading universal objects Element universalObjects = root.getChildByName("drawings"); this.universalObjects = new ArrayList<GameObject>(); if (universalObjects != null) { for (int i = 0; i < universalObjects.getChildCount(); i++) { Element universalObject = universalObjects.getChild(i); this.universalObjects.add(GameObject.parseGameObject(universalObject)); } } // Loading car Element car = root.getChildByName(Car.name); this.car = Car.parseCar(car); // Loading start Element start = root.getChildByName(Start.name); this.start = (Start) GameObject.parseGameObject(start); // Loading finish Element finish = root.getChildByName(Finish.name); this.finish = (Finish) GameObject.parseGameObject(finish); // Background Element background = root.getChildByName("background"); this.background = LevelBackground.parseLevelBackground(background); // Time limit this.timeLimit = root.getFloat("timeLimit"); Element difficulty = root.getChildByName("difficulty"); this.setDifficulty(Difficulty.valueOf(difficulty.getText())); System.out.println("Loading level \"" + file.name() + "\" done."); }
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); } }
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); } }
// ---------------------------------------------------------------------- protected void baseInternalLoad(Element xml) { name = xml.get("Name", name); size = xml.getInt("Size", size); if (size < 1) { size = 1; } tile = new GameTile[size][size]; quality = xml.getInt("Quality", quality); Element spriteElement = xml.getChildByName("Sprite"); if (spriteElement != null) { sprite = AssetManager.loadSprite(xml.getChildByName("Sprite")); } if (sprite != null) { sprite.size[0] = size; sprite.size[1] = size; } Element raisedSpriteElement = xml.getChildByName("TilingSprite"); if (raisedSpriteElement != null) { tilingSprite = TilingSprite.load(raisedSpriteElement); } if (tilingSprite != null) { // for (Sprite sprite : tilingSprite.sprites) // { // sprite.size = size; // } } Element lightElement = xml.getChildByName("Light"); if (lightElement != null) { light = Roguelike.Lights.Light.load(lightElement); } Element statElement = xml.getChildByName("Statistics"); if (statElement != null) { Statistic.load(statElement, statistics); HP = getMaxHP(); statistics.put(Statistic.WALK, 1); // statistics.put( Statistic.ENTITY, 1 ); } Element inventoryElement = xml.getChildByName("Inventory"); if (inventoryElement != null) { inventory.load(inventoryElement); } Element immuneElement = xml.getChildByName("Immune"); if (immuneElement != null) { immune = immuneElement.getText().toLowerCase().split(","); } canTakeDamage = xml.getBoolean("CanTakeDamage", canTakeDamage); UID = getClass().getSimpleName() + " " + name + ": ID " + hashCode(); }
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); } }
public Animation read(FileHandle handle) { XmlReader rdr = new XmlReader(); try { Element root = rdr.parse(handle); int frameRate = Integer.parseInt(root.getChildByName("FrameRate").getText()); int loopFrame = Integer.parseInt(root.getChildByName("LoopFrame").getText()); Animation anim = new Animation(); anim.FrameRate = frameRate; anim.LoopFrame = loopFrame; anim.Textures = new ArrayList<TextureEntry>(); anim.Loop = loopFrame != -1; anim.Keyframes = new ArrayList<Keyframe>(); for (int i = 0; i < root.getChildCount(); ++i) { Element ch = root.getChild(i); if (ch.getName().equals("Texture")) { TextureRegion reg = imgSrc.getImage(ch.getText()); if (reg != null) { TextureAtlas.AtlasRegion r = (TextureAtlas.AtlasRegion) reg; anim.Textures.add( new TextureEntry( reg, new TextureBounds( new Rectangle(0, 0, r.originalWidth, r.originalHeight), new Vector2(r.originalWidth / 2, r.originalHeight / 2)))); } else { throw new Exception("Unable to resolve image: " + ch.getText()); } } } for (int i = 0; i < root.getChildCount(); ++i) { Element ch = root.getChild(i); if (ch.getName().equals("Keyframe")) { Keyframe frame = new Keyframe(); frame.Bones = new ArrayList<Bone>(); frame.FrameNumber = ch.getIntAttribute("frame"); frame.Trigger = ch.getAttribute("trigger", ""); frame.FlipHorizontally = ch.getAttribute("hflip", "False").equals("True"); frame.FlipVertically = ch.getAttribute("vflip", "False").equals("True"); for (int j = 0; j < ch.getChildCount(); ++j) { Element bone = ch.getChild(j); if (bone.getName().equals("Bone")) { Element posElem = bone.getChildByName("Position"); Element sclElem = bone.getChildByName("Scale"); Vector2 pos = new Vector2(); Vector2 scl = new Vector2(); pos.x = Float.parseFloat(posElem.getChildByName("X").getText()); pos.y = Float.parseFloat(posElem.getChildByName("Y").getText()); scl.x = Float.parseFloat(sclElem.getChildByName("X").getText()); scl.y = Float.parseFloat(sclElem.getChildByName("Y").getText()); Bone b = new Bone(); b.Hidden = bone.getChildByName("Hidden").getText().equals("True"); b.Name = bone.getAttribute("name"); b.TextureFlipHorizontal = bone.getChildByName("TextureFlipHorizontal").getText().equals("True"); b.TextureFlipVertical = bone.getChildByName("TextureFlipVertical").getText().equals("True"); b.ParentIndex = Integer.parseInt(bone.getChildByName("ParentIndex").getText()); b.TextureIndex = Integer.parseInt(bone.getChildByName("TextureIndex").getText()); b.Rotation = Float.parseFloat(bone.getChildByName("Rotation").getText()); b.Position = pos; b.Scale = scl; b.SelfIndex = j; frame.Bones.add(b); } } frame.SortBones(); anim.Keyframes.add(frame); } float fr = 1.0f / anim.FrameRate; anim.LoopTime = anim.LoopFrame * anim.FrameRate; anim.Loop = anim.LoopFrame != -1; for (Keyframe kf : anim.Keyframes) { kf.FrameTime = fr * kf.FrameNumber; } } return anim; } catch (Exception ex) { Gdx.app.log("AnimationReader", "read", ex); return null; } }