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); } }
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); } }
/// <summary> /// Create the tile image layer as a child entity /// </summary> /// <param name="tmxImageLayer">The tmx image layer.</param> /// <param name="layerIndex">The layer index</param> private void CreateChildTileImageLayer(TmxImageLayer tmxImageLayer, int layerIndex) { var tag = "TileImageLayer_" + layerIndex; var tmxLayerName = tmxImageLayer.Name; var tiledMapImageLayer = new TiledMapImageLayer(tmxImageLayer, this); Entity layerEntity = null; layerEntity = this.Owner.FindChildrenByTag(tag).FirstOrDefault(); var tileLayerOffset = new Vector2((float)tmxImageLayer.OffsetX, (float)tmxImageLayer.OffsetY); if (layerEntity != null) { var tileMapTransform = layerEntity.FindComponent<Transform2D>(); var sprite = layerEntity.FindComponent<Sprite>(); if (tileMapTransform != null && sprite != null) { tileMapTransform.LocalPosition = tileLayerOffset; sprite.TexturePath = tiledMapImageLayer.ImagePath; layerEntity.Name = tmxLayerName; } else { this.Owner.RemoveChild(layerEntity.Name); layerEntity = null; } } if (layerEntity == null) { layerEntity = new Entity(tmxLayerName) { Tag = tag } .AddComponent(new Sprite(tiledMapImageLayer.ImagePath)) .AddComponent(new Transform2D() { LocalPosition = tileLayerOffset, Origin = this.transform.Origin, Opacity = (float)tmxImageLayer.Opacity }) .AddComponent(new SpriteRenderer()); this.Owner.AddChild(layerEntity); } this.imageLayers.Add(tmxLayerName, tiledMapImageLayer); }