Esempio n. 1
0
        public TmxLayer(XMLReader xLayer, int width, int height)
        {
            Name   = xLayer.Attribute("name") ?? "";
            Width  = width;
            Height = height;

            Opacity = xLayer.AttributeFloatN("opacity") ?? 1.0f;
            Visible = xLayer.AttributeBoolN("visible") ?? true;
            OffsetX = xLayer.AttributeFloat("offsetx");
            OffsetY = xLayer.AttributeFloat("offsety");

            Properties = TmxHelpers.GetPropertyDict(xLayer.Element("properties"));

            // Not a layer which contains tiles.
            if (width == 0)
            {
                return;
            }
            XMLReader xData    = xLayer.Element("data");
            string    encoding = xData.Attribute("encoding");

            Tiles = new Collection <TmxLayerTile>();
            switch (encoding)
            {
            case "csv":
            {
                string csvData = xData.CurrentContents();
                foreach (string s in csvData.Split(','))
                {
                    uint gid = uint.Parse(s.Trim());
                    Tiles.Add(new TmxLayerTile(gid));
                }

                break;
            }

            case null:
            {
                foreach (XMLReader e in xData.Elements("tile"))
                {
                    uint gid = e.AttributeUInt("gid");
                    Tiles.Add(new TmxLayerTile(gid));
                }

                break;
            }

            default:
                Engine.Log.Warning($"Unknown tmx layer encoding {encoding}", MessageSource.TMX);
                return;
            }
        }
        public TmxObject(XMLReader xObject)
        {
            Id       = xObject.AttributeInt("id");
            Name     = xObject.Attribute("name") ?? string.Empty;
            X        = xObject.AttributeDouble("x");
            Y        = xObject.AttributeDouble("y");
            Width    = xObject.AttributeDouble("width");
            Height   = xObject.AttributeDouble("height");
            Type     = xObject.Attribute("type") ?? string.Empty;
            Visible  = xObject.AttributeBoolN("visible") ?? true;
            Rotation = xObject.AttributeDouble("rotation");

            // Assess object type and assign appropriate content
            uint?rawGid = xObject.AttributeUIntN("gid");

            if (rawGid != null)
            {
                Gid = TmxHelpers.GetGidFlags((uint)rawGid, out HorizontalFlip, out VerticalFlip, out DiagonalFlip);
            }

            XMLReader xEllipse  = xObject.Element("ellipse");
            XMLReader xPolygon  = xObject.Element("polygon");
            XMLReader xPolyline = xObject.Element("polyline");

            if (Gid != null)
            {
                ObjectType = TmxObjectType.Image;
                // In Tiled an image's X,Y coordinates represent the bottom-left corner of the image
                Y -= Height;
            }
            else if (xEllipse != null)
            {
                ObjectType = TmxObjectType.Ellipse;
            }
            else if (xPolygon != null)
            {
                Points     = ParsePoints(xPolygon);
                ObjectType = TmxObjectType.Polygon;
            }
            else if (xPolyline != null)
            {
                List <Vector2> points = ParsePoints(xPolyline);
                Lines = new List <LineSegment>(points.Count / 2);
                for (var i = 0; i < points.Count; i++)
                {
                    if (i + 1 < points.Count)
                    {
                        Lines.Add(new LineSegment(points[i], points[i + 1]));
                    }
                }
                ObjectType = TmxObjectType.Polyline;
            }
            else
            {
                ObjectType = TmxObjectType.Basic;
            }

            XMLReader xText = xObject.Element("text");

            if (xText != null)
            {
                Text = new TmxText(xText);
            }

            Properties = TmxHelpers.GetPropertyDict(xObject.Element("properties"));
        }