public TmxObjectGroup(TmxMap map, XElement xObjectGroup) { Map = map; Name = (string)xObjectGroup.Attribute("name") ?? string.Empty; Color = TmxColor.ParseColor(xObjectGroup.Attribute("color")); Opacity = (float?)xObjectGroup.Attribute("opacity") ?? 1.0f; Visible = (bool?)xObjectGroup.Attribute("visible") ?? true; OffsetX = (float?)xObjectGroup.Attribute("offsetx") ?? 0.0f; OffsetY = (float?)xObjectGroup.Attribute("offsety") ?? 0.0f; var drawOrderDict = new Dictionary <string, DrawOrderType> { { "unknown", DrawOrderType.UnknownOrder }, { "topdown", DrawOrderType.IndexOrder }, { "index", DrawOrderType.TopDown } }; var drawOrderValue = (string)xObjectGroup.Attribute("draworder"); if (drawOrderValue != null) { DrawOrder = drawOrderDict[drawOrderValue]; } Objects = new TmxList <TmxObject>(); foreach (var e in xObjectGroup.Elements("object")) { Objects.Add(new TmxObject(map, e)); } Properties = PropertyDict.ParsePropertyDict(xObjectGroup.Element("properties")); }
public TmxTilesetTile(TmxTileset tileset, XElement xTile, TmxList <TmxTerrain> Terrains, string tmxDir = "") { Tileset = tileset; Id = (int)xTile.Attribute("id"); var strTerrain = (string)xTile.Attribute("terrain"); if (strTerrain != null) { TerrainEdges = new TmxTerrain[4]; var index = 0; foreach (var v in strTerrain.Split(',')) { var success = int.TryParse(v, out int result); TmxTerrain edge; if (success) { edge = Terrains[result]; } else { edge = null; } TerrainEdges[index++] = edge; } } Probability = (double?)xTile.Attribute("probability") ?? 1.0; Type = (string)xTile.Attribute("type"); var xImage = xTile.Element("image"); if (xImage != null) { Image = new TmxImage(xImage, tmxDir); } ObjectGroups = new TmxList <TmxObjectGroup>(); foreach (var e in xTile.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(tileset.Map, e)); } AnimationFrames = new List <TmxAnimationFrame>(); if (xTile.Element("animation") != null) { foreach (var e in xTile.Element("animation").Elements("frame")) { AnimationFrames.Add(new TmxAnimationFrame(e)); } } Properties = PropertyDict.ParsePropertyDict(xTile.Element("properties")); if (Properties != null) { ProcessProperties(); } }
public TmxObject(TmxMap map, XElement xObject) { Id = (int?)xObject.Attribute("id") ?? 0; Name = (string)xObject.Attribute("name") ?? string.Empty; X = (float)xObject.Attribute("x"); Y = (float)xObject.Attribute("y"); Width = (float?)xObject.Attribute("width") ?? 0.0f; Height = (float?)xObject.Attribute("height") ?? 0.0f; Type = (string)xObject.Attribute("type") ?? string.Empty; Visible = (bool?)xObject.Attribute("visible") ?? true; Rotation = (float?)xObject.Attribute("rotation") ?? 0.0f; // Assess object type and assign appropriate content var xGid = xObject.Attribute("gid"); var xEllipse = xObject.Element("ellipse"); var xPolygon = xObject.Element("polygon"); var xPolyline = xObject.Element("polyline"); var xText = xObject.Element("text"); var xPoint = xObject.Element("point"); if (xGid != null) { Tile = new TmxLayerTile(map, (uint)xGid, Convert.ToInt32(Math.Round(X)), Convert.ToInt32(Math.Round(X))); ObjectType = TmxObjectType.Tile; } else if (xEllipse != null) { ObjectType = TmxObjectType.Ellipse; } else if (xPolygon != null) { Points = ParsePoints(xPolygon); ObjectType = TmxObjectType.Polygon; } else if (xPolyline != null) { Points = ParsePoints(xPolyline); ObjectType = TmxObjectType.Polyline; } else if (xText != null) { Text = new TmxText(xText); ObjectType = TmxObjectType.Text; } else if (xPoint != null) { ObjectType = TmxObjectType.Point; } else { ObjectType = TmxObjectType.Basic; } Properties = PropertyDict.ParsePropertyDict(xObject.Element("properties")); }
public TmxGroup(TmxMap map, XElement xGroup, int width, int height, string tmxDirectory) { this.map = map; Name = (string)xGroup.Attribute("name") ?? string.Empty; Opacity = (float?)xGroup.Attribute("opacity") ?? 1.0f; Visible = (bool?)xGroup.Attribute("visible") ?? true; OffsetX = (float?)xGroup.Attribute("offsetx") ?? 0.0f; OffsetY = (float?)xGroup.Attribute("offsety") ?? 0.0f; Properties = PropertyDict.ParsePropertyDict(xGroup.Element("properties")); layers = new TmxList <ITmxLayer>(); tileLayers = new TmxList <TmxLayer>(); objectGroups = new TmxList <TmxObjectGroup>(); imageLayers = new TmxList <TmxImageLayer>(); groups = new TmxList <TmxGroup>(); foreach (var e in xGroup.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group")) { ITmxLayer layer; switch (e.Name.LocalName) { case "layer": var tileLayer = new TmxLayer(map, e, width, height); layer = tileLayer; tileLayers.Add(tileLayer); break; case "objectgroup": var objectgroup = new TmxObjectGroup(map, e); layer = objectgroup; objectGroups.Add(objectgroup); break; case "imagelayer": var imagelayer = new TmxImageLayer(map, e, tmxDirectory); layer = imagelayer; imageLayers.Add(imagelayer); break; case "group": var group = new TmxGroup(map, e, width, height, tmxDirectory); layer = group; groups.Add(group); break; default: throw new InvalidOperationException(); } layers.Add(layer); } }
public TmxImageLayer(TmxMap map, XElement xImageLayer, string tmxDir = "") { this.Map = map; Name = (string)xImageLayer.Attribute("name"); Width = (int?)xImageLayer.Attribute("width"); Height = (int?)xImageLayer.Attribute("height"); Visible = (bool?)xImageLayer.Attribute("visible") ?? true; Opacity = (float?)xImageLayer.Attribute("opacity") ?? 1.0f; OffsetX = (float?)xImageLayer.Attribute("offsetx") ?? 0.0f; OffsetY = (float?)xImageLayer.Attribute("offsety") ?? 0.0f; var xImage = xImageLayer.Element("image"); if (xImage != null) { Image = new TmxImage(xImage, tmxDir); } Properties = PropertyDict.ParsePropertyDict(xImageLayer.Element("properties")); }
public TmxLayer(TmxMap map, XElement xLayer, int width, int height) { Map = map; Name = (string)xLayer.Attribute("name"); Opacity = (float?)xLayer.Attribute("opacity") ?? 1.0f; Visible = (bool?)xLayer.Attribute("visible") ?? true; OffsetX = (float?)xLayer.Attribute("offsetx") ?? 0.0f; OffsetY = (float?)xLayer.Attribute("offsety") ?? 0.0f; // TODO: does the width/height passed in ever differ from the TMX layer XML? Width = (int)xLayer.Attribute("width"); Height = (int)xLayer.Attribute("height"); var xData = xLayer.Element("data"); var encoding = (string)xData.Attribute("encoding"); Tiles = new TmxLayerTile[width * height]; if (encoding == "base64") { var decodedStream = new TmxBase64Data(xData); var stream = decodedStream.Data; var index = 0; using (var br = new BinaryReader(stream)) for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { var gid = br.ReadUInt32(); Tiles[index++] = gid != 0 ? new TmxLayerTile(map, gid, i, j) : null; } } } else if (encoding == "csv") { var csvData = (string)xData.Value; int k = 0; foreach (var s in csvData.Split(',')) { var gid = uint.Parse(s.Trim()); var x = k % width; var y = k / width; Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null; } } else if (encoding == null) { int k = 0; foreach (var e in xData.Elements("tile")) { var gid = (uint?)e.Attribute("gid") ?? 0; var x = k % width; var y = k / width; Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null; } } else { throw new Exception("TmxLayer: Unknown encoding."); } Properties = PropertyDict.ParsePropertyDict(xLayer.Element("properties")); }
public TmxTileset(TmxMap map, XElement xTileset, int firstGid, string tmxDir) { Map = map; FirstGid = firstGid; Name = (string)xTileset.Attribute("name"); TileWidth = (int)xTileset.Attribute("tilewidth"); TileHeight = (int)xTileset.Attribute("tileheight"); Spacing = (int?)xTileset.Attribute("spacing") ?? 0; Margin = (int?)xTileset.Attribute("margin") ?? 0; Columns = (int?)xTileset.Attribute("columns"); TileCount = (int?)xTileset.Attribute("tilecount"); TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); var xImage = xTileset.Element("image"); if (xImage != null) { Image = new TmxImage(xImage, tmxDir); } var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { Terrains = new TmxList <TmxTerrain>(); foreach (var e in xTerrainType.Elements("terrain")) { Terrains.Add(new TmxTerrain(e)); } } Tiles = new Dictionary <int, TmxTilesetTile>(); foreach (var xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(this, xTile, Terrains, tmxDir); Tiles[tile.Id] = tile; } Properties = PropertyDict.ParsePropertyDict(xTileset.Element("properties")); // cache our source rects for each tile so we dont have to calculate them every time we render. If we have // an image this is a normal tileset, else its an image tileset TileRegions = new Dictionary <int, RectangleF>(); if (Image != null) { var id = firstGid; for (var y = Margin; y < Image.Height - Margin; y += TileHeight + Spacing) { var column = 0; for (var x = Margin; x < Image.Width - Margin; x += TileWidth + Spacing) { TileRegions.Add(id++, new RectangleF(x, y, TileWidth, TileHeight)); if (++column >= Columns) { break; } } } } else { // it seems that firstGid is always 0 for image tilesets so we can access them like an array here var id = firstGid; for (var i = 0; i < Tiles.Count; i++) { var tile = Tiles[i]; TileRegions.Add(id++, new RectangleF(0, 0, tile.Image.Width, tile.Image.Height)); } } }
public TmxTerrain(XElement xTerrain) { Name = (string)xTerrain.Attribute("name"); Tile = (int)xTerrain.Attribute("tile"); Properties = PropertyDict.ParsePropertyDict(xTerrain.Element("properties")); }