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; }
/** * 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; }
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; }
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); } } }
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."); }
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); } } } }
// ---------------------------------------------------------------------- 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; } }