public TmxMap(string filename) { XDocument xDoc = ReadXml(filename); var xMap = xDoc.Element("map"); Version = (string)xMap.Attribute("version"); Orientation = (OrientationType)Enum.Parse( typeof(OrientationType), xMap.Attribute("orientation").Value, true); Width = (int)xMap.Attribute("width"); Height = (int)xMap.Attribute("height"); TileWidth = (int)xMap.Attribute("tilewidth"); TileHeight = (int)xMap.Attribute("tileheight"); Tilesets = new TmxList(); foreach (var e in xMap.Elements("tileset")) { Tilesets.Add(new TmxTileset(e)); } Layers = new TmxList(); foreach (var e in xMap.Elements("layer")) { Layers.Add(new TmxLayer(e, Width, Height)); } ObjectGroups = new TmxList(); foreach (var e in xMap.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(e)); } Properties = new PropertyDict(xMap.Element("properties")); }
public TmxObjectGroup(XElement xObjectGroup) { Name = (string)xObjectGroup.Attribute("name") ?? String.Empty; Color = new TmxColor(xObjectGroup.Attribute("color")); Opacity = (double?)xObjectGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xObjectGroup.Attribute("visible") ?? true; OffsetX = (double?)xObjectGroup.Attribute("offsetx") ?? 0.0; OffsetY = (double?)xObjectGroup.Attribute("offsety") ?? 0.0; var drawOrderDict = new Dictionary <string, DrawOrderType> { { "unknown", DrawOrderType.UnknownOrder }, { "topdown", DrawOrderType.TopDown }, { "index", DrawOrderType.IndexOrder } }; 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(e)); } Properties = new PropertyDict(xObjectGroup.Element("properties")); }
public TmxGroup(XElement xGroup, int width, int height, string tmxDirectory) { Name = (string)xGroup.Attribute("name") ?? string.Empty; Opacity = (double?)xGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xGroup.Attribute("visible") ?? true; OffsetX = (double?)xGroup.Attribute("offsetx") ?? 0.0; OffsetY = (double?)xGroup.Attribute("offsety") ?? 0.0; Properties = new PropertyDict(xGroup.Element("properties")); Layers = new TmxList <TmxLayer>(); foreach (var e in xGroup.Elements("layer")) { Layers.Add(new TmxLayer(e, width, height)); } ObjectGroups = new TmxList <TmxObjectGroup>(); foreach (var e in xGroup.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(e)); } ImageLayers = new TmxList <TmxImageLayer>(); foreach (var e in xGroup.Elements("imagelayer")) { ImageLayers.Add(new TmxImageLayer(e, tmxDirectory)); } Groups = new TmxList <TmxGroup>(); foreach (var e in xGroup.Elements("group")) { Groups.Add(new TmxGroup(e, width, height, tmxDirectory)); } }
private void ProcessTileset(XElement xTileset, string tmxDir = "") { 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")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList <TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { 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(xTile, Terrains, tmxDir); Tiles[tile.Id] = tile; } Properties = new PropertyDict(xTileset.Element("properties")); }
public void AddTilesetsAsXContainers(List <XElement> tileXcontainers) { Tilesets = new TmxList <TmxTileset>(); int index = 0; foreach (var e in xMap.Elements("tileset")) { Tilesets.Add(new TmxTileset(e, tileXcontainers[index], "")); index += 1; } }
public TmxTilesetTile(XElement xTile, TmxList <TmxTerrain> Terrains, string tmxDir = "") { Id = (int)xTile.Attribute("id"); TerrainEdges = new Collection <TmxTerrain>(); int result; TmxTerrain edge; var strTerrain = (string)xTile.Attribute("terrain") ?? ",,,"; foreach (var v in strTerrain.Split(',')) { var success = int.TryParse(v, out result); if (success) { edge = Terrains[result]; } else { edge = null; } TerrainEdges.Add(edge); // TODO: Assert that TerrainEdges length is 4 } Probability = (double?)xTile.Attribute("probability") ?? 1.0; Type = (string)xTile.Attribute("type"); Image = new TmxImage(xTile.Element("image"), tmxDir); ObjectGroups = new TmxList <TmxObjectGroup>(); int objectGroupIndex = 0; foreach (var e in xTile.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(e, objectGroupIndex++)); } AnimationFrames = new Collection <TmxAnimationFrame>(); if (xTile.Element("animation") != null) { foreach (var e in xTile.Element("animation").Elements("frame")) { AnimationFrames.Add(new TmxAnimationFrame(e)); } } Properties = new PropertyDict(xTile.Element("properties")); }
public TmxGroup(XElement xGroup, int width, int height, string tmxDirectory) { Name = (string)xGroup.Attribute("name") ?? String.Empty; Opacity = (double?)xGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xGroup.Attribute("visible") ?? true; OffsetX = (double?)xGroup.Attribute("offsetx") ?? 0.0; OffsetY = (double?)xGroup.Attribute("offsety") ?? 0.0; Properties = new PropertyDict(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(e, width, height); layer = tileLayer; TileLayers.Add(tileLayer); break; case "objectgroup": var objectgroup = new TmxObjectGroup(e); layer = objectgroup; ObjectGroups.Add(objectgroup); break; case "imagelayer": var imagelayer = new TmxImageLayer(e, tmxDirectory); layer = imagelayer; ImageLayers.Add(imagelayer); break; case "group": var group = new TmxGroup(e, width, height, tmxDirectory); layer = group; Groups.Add(group); break; default: throw new InvalidOperationException(); } Layers.Add(layer); } }
public TmxObjectGroup(XElement xObjectGroup) { Name = (string)xObjectGroup.Attribute("name"); Color = new TmxColor(xObjectGroup.Attribute("color")); Opacity = (double?)xObjectGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xObjectGroup.Attribute("visible") ?? true; Objects = new TmxList <TmxObject>(); foreach (var e in xObjectGroup.Elements("object")) { Objects.Add(new TmxObject(e)); } Properties = new PropertyDict(xObjectGroup.Element("properties")); }
public TmxMap(string filename) { XDocument xDoc = ReadXml(filename); var xMap = xDoc.Element("map"); Version = (string)xMap.Attribute("version"); Orientation = (OrientationType)Enum.Parse( typeof(OrientationType), xMap.Attribute("orientation").Value, true); RenderOrder = (RenderOrderType)Enum.Parse( typeof(RenderOrderType), xMap.Attribute("renderorder").Value.Replace('-', '_'), true); Width = (int)xMap.Attribute("width"); Height = (int)xMap.Attribute("height"); TileWidth = (int)xMap.Attribute("tilewidth"); TileHeight = (int)xMap.Attribute("tileheight"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Tilesets = new TmxList <TmxTileset>(); foreach (var e in xMap.Elements("tileset")) { Tilesets.Add(new TmxTileset(e, TmxDirectory)); } Layers = new TmxList <TmxLayer>(); foreach (var e in xMap.Elements("layer")) { Layers.Add(new TmxLayer(e, Width, Height)); } ObjectGroups = new TmxList <TmxObjectGroup>(); foreach (var e in xMap.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(e)); } ImageLayers = new TmxList <TmxImageLayer>(); foreach (var e in xMap.Elements("imagelayer")) { ImageLayers.Add(new TmxImageLayer(e, TmxDirectory)); } Properties = new PropertyDict(xMap.Element("properties")); }
public TmxObjectGroup(XElement xObjectGroup) { Name = (string)xObjectGroup.Attribute("name"); var xColor = (string)xObjectGroup.Attribute("color"); if (xColor != null) { xColor = xColor.TrimStart("#".ToCharArray()); Color = UInt32.Parse(xColor, NumberStyles.HexNumber); } var xOpacity = xObjectGroup.Attribute("opacity"); if (xOpacity == null) { Opacity = 1.0; } else { Opacity = (double)xOpacity; } var xVisible = xObjectGroup.Attribute("visible"); if (xVisible == null) { Visible = true; } else { Visible = (bool)xVisible; } Objects = new TmxList(); foreach (var e in xObjectGroup.Elements("object")) { Objects.Add(new TmxObject(e)); } Properties = new PropertyDict(xObjectGroup.Element("properties")); }
public TmxGroup(XElement xGroup, int width, int height, string tmxDirectory) { Name = (string)xGroup.Attribute("name") ?? String.Empty; Opacity = (double?)xGroup.Attribute("opacity") ?? 1.0; Visible = (bool?)xGroup.Attribute("visible") ?? true; OffsetX = (double?)xGroup.Attribute("offsetx") ?? 0.0; OffsetY = (double?)xGroup.Attribute("offsety") ?? 0.0; Properties = new PropertyDict(xGroup.Element("properties")); Layers = new TmxList <TmxLayer>(); ObjectGroups = new TmxList <TmxObjectGroup>(); ImageLayers = new TmxList <TmxImageLayer>(); Groups = new TmxList <TmxGroup>(); int elementIndex = 0; foreach (var e in xGroup.Elements()) { switch (e.Name.ToString()) { case "layer": Layers.Add(new TmxLayer(e, elementIndex, width, height)); break; case "objectgroup": ObjectGroups.Add(new TmxObjectGroup(e, elementIndex)); break; case "imagelayer": ImageLayers.Add(new TmxImageLayer(e, elementIndex, tmxDirectory)); break; case "group": Groups.Add(new TmxGroup(e, width, height, tmxDirectory)); break; } elementIndex++; } }
private void Load(XDocument xDoc) { var xMap = xDoc.Element("map"); Version = (string)xMap.Attribute("version"); Width = (int)xMap.Attribute("width"); Height = (int)xMap.Attribute("height"); TileWidth = (int)xMap.Attribute("tilewidth"); TileHeight = (int)xMap.Attribute("tileheight"); HexSideLength = (int?)xMap.Attribute("hexsidelength"); // Map orientation type var orientDict = new Dictionary <string, OrientationType> { { "unknown", OrientationType.Unknown }, { "orthogonal", OrientationType.Orthogonal }, { "isometric", OrientationType.Isometric }, { "staggered", OrientationType.Staggered }, { "hexagonal", OrientationType.Hexagonal }, }; var orientValue = (string)xMap.Attribute("orientation"); if (orientValue != null) { Orientation = orientDict[orientValue]; } // Hexagonal stagger axis var staggerAxisDict = new Dictionary <string, StaggerAxisType> { { "x", StaggerAxisType.X }, { "y", StaggerAxisType.Y }, }; var staggerAxisValue = (string)xMap.Attribute("staggeraxis"); if (staggerAxisValue != null) { StaggerAxis = staggerAxisDict[staggerAxisValue]; } // Hexagonal stagger index var staggerIndexDict = new Dictionary <string, StaggerIndexType> { { "odd", StaggerIndexType.Odd }, { "even", StaggerIndexType.Even }, }; var staggerIndexValue = (string)xMap.Attribute("staggerindex"); if (staggerIndexValue != null) { StaggerIndex = staggerIndexDict[staggerIndexValue]; } // Tile render order var renderDict = new Dictionary <string, RenderOrderType> { { "right-down", RenderOrderType.RightDown }, { "right-up", RenderOrderType.RightUp }, { "left-down", RenderOrderType.LeftDown }, { "left-up", RenderOrderType.LeftUp } }; var renderValue = (string)xMap.Attribute("renderorder"); if (renderValue != null) { RenderOrder = renderDict[renderValue]; } NextObjectID = (int?)xMap.Attribute("nextobjectid"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Properties = new PropertyDict(xMap.Element("properties")); Tilesets = new TmxList <TmxTileset>(); foreach (var e in xMap.Elements("tileset")) { Tilesets.Add(new TmxTileset(e, TmxDirectory)); } Layers = new TmxList <TmxLayer>(); foreach (var e in xMap.Elements("layer")) { Layers.Add(new TmxLayer(e, Width, Height)); } ObjectGroups = new TmxList <TmxObjectGroup>(); foreach (var e in xMap.Elements("objectgroup")) { ObjectGroups.Add(new TmxObjectGroup(e)); } ImageLayers = new TmxList <TmxImageLayer>(); foreach (var e in xMap.Elements("imagelayer")) { ImageLayers.Add(new TmxImageLayer(e, TmxDirectory)); } }
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "", Func <string, Stream> readerFunc = null) { var xFirstGid = xTileset.Attribute("firstgid"); var source = (string)xTileset.Attribute("source"); if (source != null) { // source is always preceded by firstgid FirstGid = (int)xFirstGid; // Everything else is in the TSX file // Prepend the parent TMX directory if necessary XDocument xDocTileset; if (readerFunc == null) { source = Path.Combine(tmxDir, source); xDocTileset = ReadXml(source); } else { using (Stream inputStream = readerFunc(source)) { xDocTileset = XDocument.Load(inputStream); } } var ts = new TmxTileset(xDocTileset, TmxDirectory); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; Columns = ts.Columns; TileCount = ts.TileCount; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) { FirstGid = (int)xFirstGid; } 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")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList <TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { 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(xTile, Terrains, tmxDir); Tiles[tile.Id] = tile; } Properties = new PropertyDict(xTileset.Element("properties")); } }
private void Load(XDocument xDoc) { var xMap = xDoc.Element("map"); Version = (string)xMap.Attribute("version"); TiledVersion = (string)xMap.Attribute("tiledversion"); Width = (int)xMap.Attribute("width"); Height = (int)xMap.Attribute("height"); TileWidth = (int)xMap.Attribute("tilewidth"); TileHeight = (int)xMap.Attribute("tileheight"); HexSideLength = (int?)xMap.Attribute("hexsidelength"); // Map orientation type var orientDict = new Dictionary <string, OrientationType> { { "unknown", OrientationType.Unknown }, { "orthogonal", OrientationType.Orthogonal }, { "isometric", OrientationType.Isometric }, { "staggered", OrientationType.Staggered }, { "hexagonal", OrientationType.Hexagonal }, }; var orientValue = (string)xMap.Attribute("orientation"); if (orientValue != null) { Orientation = orientDict[orientValue]; } // Hexagonal stagger axis var staggerAxisDict = new Dictionary <string, StaggerAxisType> { { "x", StaggerAxisType.X }, { "y", StaggerAxisType.Y }, }; var staggerAxisValue = (string)xMap.Attribute("staggeraxis"); if (staggerAxisValue != null) { StaggerAxis = staggerAxisDict[staggerAxisValue]; } // Hexagonal stagger index var staggerIndexDict = new Dictionary <string, StaggerIndexType> { { "odd", StaggerIndexType.Odd }, { "even", StaggerIndexType.Even }, }; var staggerIndexValue = (string)xMap.Attribute("staggerindex"); if (staggerIndexValue != null) { StaggerIndex = staggerIndexDict[staggerIndexValue]; } // Tile render order var renderDict = new Dictionary <string, RenderOrderType> { { "right-down", RenderOrderType.RightDown }, { "right-up", RenderOrderType.RightUp }, { "left-down", RenderOrderType.LeftDown }, { "left-up", RenderOrderType.LeftUp } }; var renderValue = (string)xMap.Attribute("renderorder"); if (renderValue != null) { RenderOrder = renderDict[renderValue]; } NextObjectID = (int?)xMap.Attribute("nextobjectid"); BackgroundColor = new TmxColor(xMap.Attribute("backgroundcolor")); Properties = new PropertyDict(xMap.Element("properties")); Tilesets = new TmxList <TmxTileset>(); foreach (var e in xMap.Elements("tileset")) { Tilesets.Add(new TmxTileset(e, TmxDirectory, CustomLoader)); } Layers = new TmxList <ITmxLayer>(); TileLayers = new TmxList <TmxLayer>(); ObjectGroups = new TmxList <TmxObjectGroup>(); ImageLayers = new TmxList <TmxImageLayer>(); Groups = new TmxList <TmxGroup>(); foreach (var e in xMap.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(e, Width, Height); layer = tileLayer; TileLayers.Add(tileLayer); break; case "objectgroup": var objectgroup = new TmxObjectGroup(e); layer = objectgroup; ObjectGroups.Add(objectgroup); break; case "imagelayer": var imagelayer = new TmxImageLayer(e, TmxDirectory); layer = imagelayer; ImageLayers.Add(imagelayer); break; case "group": var group = new TmxGroup(e, Width, Height, TmxDirectory); layer = group; Groups.Add(group); break; default: throw new InvalidOperationException(); } Layers.Add(layer); } }
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "") { var xFirstGid = xTileset.Attribute("firstgid"); var source = (string)xTileset.Attribute("source"); if (source != null) { // Prepend the parent TMX directory if necessary source = Path.Combine(tmxDir, source); // source is always preceded by firstgid FirstGid = (int)xFirstGid; // Everything else is in the TSX file var xDocTileset = ReadXml(source); var ts = new TmxTileset(xDocTileset, TmxDirectory); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) { FirstGid = (int)xFirstGid; } 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; TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList <TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { foreach (var e in xTerrainType.Elements("terrain")) { Terrains.Add(new TmxTerrain(e)); } } Tiles = new List <TmxTilesetTile>(); foreach (var xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(xTile, Terrains, tmxDir); Tiles.Add(tile); } Properties = new PropertyDict(xTileset.Element("properties")); } }