Example #1
0
 public TmxText(XMLReader xText)
 {
     FontFamily = xText.Attribute("fontfamily") ?? "sans-serif";
     PixelSize  = xText.AttributeIntN("pixelsize") ?? 16;
     Wrap       = xText.AttributeBool("wrap");
     Color      = TmxHelpers.ParseTmxColor(xText.Attribute("color"));
     Bold       = xText.AttributeBool("bold");
     Italic     = xText.AttributeBool("italic");
     Underline  = xText.AttributeBool("underline");
     Strikeout  = xText.AttributeBool("strikeout");
     Kerning    = xText.AttributeBoolN("kerning") ?? true;
     Alignment  = new TmxAlignment(xText.Attribute("halign"), xText.Attribute("valign"));
     Value      = xText.CurrentContents();
 }
 public TmxObjectLayer(XMLReader xObjectGroup) : base(xObjectGroup, 0, 0)
 {
     Color     = TmxHelpers.ParseTmxColor(xObjectGroup.Attribute("color"));
     DrawOrder = xObjectGroup.AttributeEnum <DrawOrder>("draworder");
     Objects   = new TmxList <TmxObject>();
     foreach (XMLReader e in xObjectGroup.Elements("object"))
     {
         Objects.Add(new TmxObject(e));
     }
 }
        public static Dictionary <string, string> GetPropertyDict(XMLReader containingElement)
        {
            var attributes = new Dictionary <string, string>();

            if (containingElement == null)
            {
                return(attributes);
            }
            List <XMLReader> properties = containingElement.Elements("property");

            for (var i = 0; i < properties.Count; i++)
            {
                XMLReader p     = properties[i];
                string    value = p.Attribute("value") ?? p.CurrentContents();
                attributes.Add(p.Attribute("name"), value);
            }

            return(attributes);
        }
Example #4
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;
            }
        }
Example #5
0
        public static TmxProperties GetPropertyDict(XMLReader containingElement)
        {
            if (containingElement == null)
            {
                return(new TmxProperties(null));
            }
            var attributes = new Dictionary <string, string>();
            List <XMLReader> properties = containingElement.Elements("property");

            for (var i = 0; i < properties.Count; i++)
            {
                XMLReader p    = properties[i];
                string    name = p.Attribute("name");
                if (name == null)
                {
                    continue;
                }
                string value = p.Attribute("value") ?? p.CurrentContents();
                attributes.Add(name, value);
            }

            return(new TmxProperties(attributes));
        }
        public List <Vector2> ParsePoints(XMLReader xPoints)
        {
            var points = new List <Vector2>();

            string pointString = xPoints.Attribute("points");

            string[] pointStringPair = pointString.Split(' ');
            foreach (string s in pointStringPair)
            {
                Vector2 pt = TmxHelpers.GetObjectPoint(s);

                // Point coordinates are actually relative to the object's X and Y
                pt.X = (float)X + pt.X;
                pt.Y = (float)Y + pt.Y;

                points.Add(pt);
            }

            return(points);
        }
Example #7
0
        public TmxTileset(XMLReader xTileset)
        {
            FirstGid   = xTileset.AttributeInt("firstgid");
            Name       = xTileset.Attribute("name");
            TileWidth  = xTileset.AttributeInt("tilewidth");
            TileHeight = xTileset.AttributeInt("tileheight");
            Spacing    = xTileset.AttributeIntN("spacing") ?? 0;
            Margin     = xTileset.AttributeInt("margin");
            Columns    = xTileset.AttributeIntN("columns");
            TileCount  = xTileset.AttributeIntN("tilecount");
            TileOffset = TmxHelpers.GetVector2(xTileset.Element("tileoffset"));

            XMLReader image = xTileset.Element("image");

            if (image != null)
            {
                Source = image.Attribute("source");
            }

            Terrains = new TmxList <TmxTerrain>();
            XMLReader xTerrainType = xTileset.Element("terraintypes");

            if (xTerrainType != null)
            {
                foreach (XMLReader e in xTerrainType.Elements("terrain"))
                {
                    Terrains.Add(new TmxTerrain(e));
                }
            }

            Tiles = new Dictionary <int, TmxTilesetTile>();
            foreach (XMLReader xTile in xTileset.Elements("tile"))
            {
                var tile = new TmxTilesetTile(xTile, Terrains);
                Tiles[tile.Id] = tile;
            }

            Properties = TmxHelpers.GetPropertyDict(xTileset.Element("properties"));
        }
 public TmxTerrain(XMLReader xTerrain)
 {
     Name       = xTerrain.Attribute("name");
     Tile       = xTerrain.AttributeInt("tile");
     Properties = TmxHelpers.GetPropertyDict(xTerrain.Element("properties"));
 }
Example #9
0
        public TmxMap(XMLReader reader)
        {
            XMLReader xMap = reader.Element("map");

            Version      = xMap.Attribute("version");
            TiledVersion = xMap.Attribute("tiledversion");

            Width           = xMap.AttributeInt("width");
            Height          = xMap.AttributeInt("height");
            TileWidth       = xMap.AttributeInt("tilewidth");
            TileHeight      = xMap.AttributeInt("tileheight");
            HexSideLength   = xMap.AttributeIntN("hexsidelength");
            Orientation     = xMap.AttributeEnum <Orientation>("orientation");
            StaggerAxis     = xMap.AttributeEnum <StaggerAxis>("staggeraxis");
            StaggerIndex    = xMap.AttributeEnum <StaggerIndex>("staggerindex");
            RenderOrder     = xMap.AttributeEnum <RenderOrder>("renderorder");
            NextObjectId    = xMap.AttributeIntN("nextobjectid");
            BackgroundColor = TmxHelpers.ParseTmxColor(xMap.Attribute("backgroundcolor"));

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

            Tilesets = new TmxList <TmxTileset>();
            foreach (XMLReader e in xMap.Elements("tileset"))
            {
                Tilesets.Add(new TmxTileset(e));
            }

            Layers       = new TmxList <TmxLayer>();
            TileLayers   = new TmxList <TmxLayer>();
            ObjectLayers = new TmxList <TmxObjectLayer>();
            ImageLayers  = new TmxList <TmxImageLayer>();
            Groups       = new TmxList <TmxGroupedLayers>();
            foreach (XMLReader e in xMap.Elements().Where(x => x.Name == "layer" || x.Name == "objectgroup" || x.Name == "imagelayer" || x.Name == "group"))
            {
                TmxLayer layer;
                switch (e.Name)
                {
                case "layer":
                    var tileLayer = new TmxLayer(e, Width, Height);
                    layer = tileLayer;
                    TileLayers.Add(tileLayer);
                    break;

                case "objectgroup":
                    var objectLayer = new TmxObjectLayer(e);
                    layer = objectLayer;
                    ObjectLayers.Add(objectLayer);
                    break;

                case "imagelayer":
                    var imageLayer = new TmxImageLayer(e);
                    layer = imageLayer;
                    ImageLayers.Add(imageLayer);
                    break;

                case "group":
                    var group = new TmxGroupedLayers(e, Width, Height);
                    layer = group;
                    Groups.Add(group);
                    break;

                default:
                    Engine.Log.Warning($"Unknown TMX layer type {e.Name}.", MessageSource.TMX);
                    continue;
                }

                Layers.Add(layer);
            }
        }
        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"));
        }