Example #1
0
        public static TmxObjectGroup LoadTmxObjectGroup(this TmxObjectGroup group, TmxMap map, XElement xObjectGroup)
        {
            group.Map     = map;
            group.Name    = (string)xObjectGroup.Attribute("name") ?? string.Empty;
            group.Color   = TiledMapLoader.ParseColor(xObjectGroup.Attribute("color"));
            group.Opacity = (float?)xObjectGroup.Attribute("opacity") ?? 1.0f;
            group.Visible = (bool?)xObjectGroup.Attribute("visible") ?? true;
            group.OffsetX = (float?)xObjectGroup.Attribute("offsetx") ?? 0.0f;
            group.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)
            {
                group.DrawOrder = drawOrderDict[drawOrderValue];
            }

            group.Objects = new TmxList <TmxObject>();
            foreach (var e in xObjectGroup.Elements("object"))
            {
                group.Objects.Add(new TmxObject().LoadTmxObject(map, e));
            }

            group.Properties = ParsePropertyDict(xObjectGroup.Element("properties"));

            return(group);
        }
Example #2
0
        /// <summary>
        /// parses all the layers in xEle putting them in the container
        /// </summary>
        public static void ParseLayers(object container, XElement xEle, TmxMap map, int width, int height, string tmxDirectory)
        {
            foreach (var e in xEle.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().LoadTmxLayer(map, e, width, height);
                    layer = tileLayer;

                    if (container is TmxMap m)
                    {
                        m.TileLayers.Add(tileLayer);
                    }
                    else if (container is TmxGroup g)
                    {
                        g.TileLayers.Add(tileLayer);
                    }
                    break;

                case "objectgroup":
                    var objectgroup = new TmxObjectGroup().LoadTmxObjectGroup(map, e);
                    layer = objectgroup;

                    if (container is TmxMap mm)
                    {
                        mm.ObjectGroups.Add(objectgroup);
                    }
                    else if (container is TmxGroup gg)
                    {
                        gg.ObjectGroups.Add(objectgroup);
                    }
                    break;

                case "imagelayer":
                    var imagelayer = new TmxImageLayer().LoadTmxImageLayer(map, e, tmxDirectory);
                    layer = imagelayer;

                    if (container is TmxMap mmm)
                    {
                        mmm.ImageLayers.Add(imagelayer);
                    }
                    else if (container is TmxGroup ggg)
                    {
                        ggg.ImageLayers.Add(imagelayer);
                    }
                    break;

                case "group":
                    var newGroup = new TmxGroup().LoadTmxGroup(map, e, width, height, tmxDirectory);
                    layer = newGroup;

                    if (container is TmxMap mmmm)
                    {
                        mmmm.Groups.Add(newGroup);
                    }
                    else if (container is TmxGroup gggg)
                    {
                        gggg.Groups.Add(newGroup);
                    }
                    break;

                default:
                    throw new InvalidOperationException();
                }

                if (container is TmxMap mmmmm)
                {
                    mmmmm.Layers.Add(layer);
                }
                else if (container is TmxGroup g)
                {
                    g.Layers.Add(layer);
                }
            }
        }