Пример #1
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");

            this.FlippedHorizontal = TmxMath.IsTileFlippedHorizontally(gid);
            this.FlippedVertical   = TmxMath.IsTileFlippedVertically(gid);
            uint rawTileId = TmxMath.GetTileIdWithoutFlags(gid);

            this.Tile = tmxMap.Tiles[rawTileId];

            // The tile needs to have a mesh on it.
            // Note: The tile may already be referenced by another TmxObjectTile instance, and as such will have its mesh data already made
            if (this.Tile.Meshes.Count() == 0)
            {
                this.Tile.Meshes = TmxMesh.FromTmxTile(this.Tile, tmxMap);
            }

            // Check properties for layer placement
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingLayerName"))
            {
                this.SortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName");
            }
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                this.SortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }
        }
Пример #2
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Logger.WriteVerbose("Parsing object ...");
            uint attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tid", 0u);

            if (attributeAsUInt != 0 && tmxMap.Templates.TryGetValue(attributeAsUInt, out TgxTemplate value))
            {
                xml    = value.Templatize(xml);
                tmxMap = value.TemplateGroupMap;
            }
            TmxObject tmxObject = null;

            if (xml.Element("ellipse") != null)
            {
                tmxObject = new TmxObjectEllipse();
            }
            else if (xml.Element("polygon") != null)
            {
                tmxObject = new TmxObjectPolygon();
            }
            else if (xml.Element("polyline") != null)
            {
                tmxObject = new TmxObjectPolyline();
            }
            else if (xml.Attribute("gid") != null)
            {
                uint attributeAsUInt2 = TmxHelper.GetAttributeAsUInt(xml, "gid");
                attributeAsUInt2 = TmxMath.GetTileIdWithoutFlags(attributeAsUInt2);
                if (tmxMap.Tiles.ContainsKey(attributeAsUInt2))
                {
                    tmxObject = new TmxObjectTile();
                }
                else
                {
                    Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", attributeAsUInt2, xml.ToString());
                    tmxObject = new TmxObjectRectangle();
                }
            }
            else
            {
                tmxObject = new TmxObjectRectangle();
            }
            tmxObject.Id                = TmxHelper.GetAttributeAsInt(xml, "id", 0);
            tmxObject.Name              = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObject.Type              = TmxHelper.GetAttributeAsString(xml, "type", "");
            tmxObject.Visible           = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1);
            tmxObject.ParentObjectGroup = tmxObjectGroup;
            float attributeAsFloat  = TmxHelper.GetAttributeAsFloat(xml, "x");
            float attributeAsFloat2 = TmxHelper.GetAttributeAsFloat(xml, "y");
            float attributeAsFloat3 = TmxHelper.GetAttributeAsFloat(xml, "width", 0f);
            float attributeAsFloat4 = TmxHelper.GetAttributeAsFloat(xml, "height", 0f);
            float attributeAsFloat5 = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0f);

            tmxObject.Position   = new PointF(attributeAsFloat, attributeAsFloat2);
            tmxObject.Size       = new SizeF(attributeAsFloat3, attributeAsFloat4);
            tmxObject.Rotation   = attributeAsFloat5;
            tmxObject.Properties = TmxProperties.FromXml(xml);
            tmxObject.InternalFromXml(xml, tmxMap);
            return(tmxObject);
        }
Пример #3
0
        private void ParseTileDataAsXml(XElement elemData)
        {
            Logger.WriteLine("Parsing layer data as Xml elements ...");
            var tiles = from t in elemData.Elements("tile")
                        select TmxHelper.GetAttributeAsUInt(t, "gid");

            this.TileIds = tiles.ToArray();
        }
Пример #4
0
        private void ReadTileIds_Xml(XElement xml)
        {
            Logger.WriteVerbose("Parsing layer chunk data as Xml elements ...");
            IEnumerable <uint> source = from t in xml.Elements("tile")
                                        select TmxHelper.GetAttributeAsUInt(t, "gid", 0u);

            TileIds = source.ToList();
        }
Пример #5
0
        public static TmxFrame FromXml(XElement xml, uint globalStartId)
        {
            TmxFrame tmxFrame        = new TmxFrame();
            uint     attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tileid");

            tmxFrame.GlobalTileId = attributeAsUInt + globalStartId;
            tmxFrame.DurationMs   = TmxHelper.GetAttributeAsInt(xml, "duration", 100);
            return(tmxFrame);
        }
Пример #6
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");

            this.Tile = tmxMap.Tiles[gid];

            // Our size is given by the tile
            this.Size = this.Tile.TileSize;
        }
Пример #7
0
        public static TgxTemplate FromXml(XElement xml, uint firstId, TmxMap map)
        {
            TgxTemplate tgxTemplate = new TgxTemplate(map);

            tgxTemplate.Name      = TmxHelper.GetAttributeAsString(xml, "name");
            tgxTemplate.LocalId   = TmxHelper.GetAttributeAsUInt(xml, "id");
            tgxTemplate.GlobalId  = firstId + tgxTemplate.LocalId;
            tgxTemplate.ObjectXml = xml.Element("object");
            return(tgxTemplate);
        }
Пример #8
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");

            this.FlippedHorizontal = TmxMath.IsTileFlippedHorizontally(gid);
            this.FlippedVertical   = TmxMath.IsTileFlippedVertically(gid);
            uint rawTileId = TmxMath.GetTileIdWithoutFlags(gid);

            this.Tile = tmxMap.Tiles[rawTileId];

            // The tile needs to have a mesh on it.
            // Note: The tile may already be referenced by another TmxObjectTile instance, and as such will have its mesh data already made
            if (this.Tile.Meshes.Count() == 0)
            {
                this.Tile.Meshes = TmxMesh.FromTmxTile(this.Tile, tmxMap);
            }
        }
Пример #9
0
 public void ParseTemplateGroupXml(XElement xml)
 {
     Name           = TmxHelper.GetAttributeAsString(xml, "name");
     NextTemplateId = TmxHelper.GetAttributeAsUInt(xml, "nexttemplateid");
     TemplateMap    = ParentMap.MakeTemplate(Name);
     foreach (XElement item in xml.Descendants("tileset"))
     {
         TsxTileset.FromXml(item, TemplateMap);
     }
     foreach (XElement item2 in xml.Descendants("template"))
     {
         TgxTemplate tgxTemplate = TgxTemplate.FromXml(item2, FirstTemplateId, TemplateMap);
         if (tgxTemplate != null)
         {
             Templates.Add(tgxTemplate);
         }
     }
 }
Пример #10
0
        private void ParseSingleTileset(XElement elem)
        {
            // Parse the tileset data and populate the tiles from it
            uint firstId = TmxHelper.GetAttributeAsUInt(elem, "firstgid");

            // Does the element contain all tileset data or reference an external tileset?
            XAttribute attrSource = elem.Attribute("source");

            if (attrSource == null)
            {
                ParseInternalTileset(elem, firstId);
            }
            else
            {
                // Need to load the tileset data from an external file first
                // Then we'll parse it as if it's internal data
                ParseExternalTileset(attrSource.Value, firstId);
            }
        }
Пример #11
0
        protected override void InternalFromXml(XElement xml, TmxMap tmxMap)
        {
            uint attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "gid");

            FlippedHorizontal = TmxMath.IsTileFlippedHorizontally(attributeAsUInt);
            FlippedVertical   = TmxMath.IsTileFlippedVertically(attributeAsUInt);
            uint tileIdWithoutFlags = TmxMath.GetTileIdWithoutFlags(attributeAsUInt);

            Tile = tmxMap.Tiles[tileIdWithoutFlags];
            if (Tile.Meshes.Count() == 0)
            {
                Tile.Meshes = TmxMesh.FromTmxTile(Tile, tmxMap);
            }
            ExplicitSortingLayerName = base.Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            if (base.Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                ExplicitSortingOrder = base.Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }
        }
Пример #12
0
        public static TsxTileset FromXml(XElement xml, TmxMap tmxMap)
        {
            TsxTileset tsxTileset = new TsxTileset(tmxMap);

            tsxTileset.FirstGlobalId = TmxHelper.GetAttributeAsUInt(xml, "firstgid");
            XAttribute xAttribute = xml.Attribute("source");

            if (xAttribute == null)
            {
                ParseTilesetXml(xml, tsxTileset);
            }
            else
            {
                ParseTilesetSource(xAttribute.Value, tsxTileset);
            }
            tsxTileset.Tiles.ForEach(delegate(TmxTile t)
            {
                tmxMap.Tiles.Add(t.GlobalId, t);
            });
            return(tsxTileset);
        }
Пример #13
0
        public static TgxTemplateGroup FromXml(XElement xml, TmxMap map)
        {
            TgxTemplateGroup tgxTemplateGroup = new TgxTemplateGroup(map);

            tgxTemplateGroup.FirstTemplateId = TmxHelper.GetAttributeAsUInt(xml, "firsttid");
            tgxTemplateGroup.Source          = Path.GetFullPath(TmxHelper.GetAttributeAsString(xml, "source"));
            if (File.Exists(tgxTemplateGroup.Source))
            {
                using (new ChDir(tgxTemplateGroup.Source))
                {
                    XDocument xDocument = TmxMap.LoadDocument(tgxTemplateGroup.Source);
                    tgxTemplateGroup.ParseTemplateGroupXml(xDocument.Root);
                }
            }
            else
            {
                Logger.WriteError("Template group file does not exist: {0}", tgxTemplateGroup.Source);
            }
            tgxTemplateGroup.Templates.ForEach(delegate(TgxTemplate t)
            {
                map.Templates.Add(t.GlobalId, t);
            });
            return(tgxTemplateGroup);
        }
Пример #14
0
        // This method is called eventually for external tilesets too
        // Only the gid attribute has been consumed at this point for the tileset
        private void ParseInternalTileset(XElement elemTileset, uint firstId)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemTileset, "name");

            Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tilesetName, firstId);

            int tileWidth  = TmxHelper.GetAttributeAsInt(elemTileset, "tilewidth");
            int tileHeight = TmxHelper.GetAttributeAsInt(elemTileset, "tileheight");
            int spacing    = TmxHelper.GetAttributeAsInt(elemTileset, "spacing", 0);
            int margin     = TmxHelper.GetAttributeAsInt(elemTileset, "margin", 0);

            PointF   tileOffset    = PointF.Empty;
            XElement xmlTileOffset = elemTileset.Element("tileoffset");

            if (xmlTileOffset != null)
            {
                tileOffset.X = TmxHelper.GetAttributeAsInt(xmlTileOffset, "x");
                tileOffset.Y = TmxHelper.GetAttributeAsInt(xmlTileOffset, "y");
            }

            IList <TmxTile> tilesToAdd = new List <TmxTile>();

            // Get proerties on tileset
            TmxProperties properties = TmxProperties.FromXml(elemTileset);
            string        prefix     = properties.GetPropertyValueAsString("unity:namePrefix", "");
            string        postfix    = properties.GetPropertyValueAsString("unity:namePostfix", "");

            // Tilesets may have an image for all tiles within it, or it may have an image per tile
            if (elemTileset.Element("image") != null)
            {
                TmxImage tmxImage = TmxImage.FromXml(elemTileset.Element("image"), prefix, postfix);

                // Create all the tiles
                // This is a bit complicated because of spacing and margin
                // (Margin is ignored from Width and Height)
                for (int end_y = margin + tileHeight; end_y <= tmxImage.Size.Height; end_y += spacing + tileHeight)
                {
                    for (int end_x = margin + tileWidth; end_x <= tmxImage.Size.Width; end_x += spacing + tileWidth)
                    {
                        uint    localId  = (uint)tilesToAdd.Count();
                        uint    globalId = firstId + localId;
                        TmxTile tile     = new TmxTile(this, globalId, localId, tilesetName, tmxImage);
                        tile.Offset = tileOffset;
                        tile.SetTileSize(tileWidth, tileHeight);
                        tile.SetLocationOnSource(end_x - tileWidth, end_y - tileHeight);
                        tilesToAdd.Add(tile);
                    }
                }
            }
            else
            {
                // Each tile will have it's own image
                foreach (var t in elemTileset.Elements("tile"))
                {
                    TmxImage tmxImage = TmxImage.FromXml(t.Element("image"), prefix, postfix);

                    uint localId = (uint)tilesToAdd.Count();

                    // Local Id can be overridden by the tile element
                    // This is because tiles can be removed from the tileset, so we won'd always have a zero-based index
                    localId = TmxHelper.GetAttributeAsUInt(t, "id", localId);

                    uint    globalId = firstId + localId;
                    TmxTile tile     = new TmxTile(this, globalId, localId, tilesetName, tmxImage);
                    tile.Offset = tileOffset;
                    tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
                    tile.SetLocationOnSource(0, 0);
                    tilesToAdd.Add(tile);
                }
            }

            StringBuilder builder = new StringBuilder();

            foreach (TmxTile tile in tilesToAdd)
            {
                builder.AppendFormat("{0}", tile.ToString());
                if (tile != tilesToAdd.Last())
                {
                    builder.Append("\n");
                }
                this.Tiles[tile.GlobalId] = tile;
            }
            Logger.WriteVerbose("Added {0} tiles", tilesToAdd.Count);

            // Add any extra data to tiles
            foreach (var elemTile in elemTileset.Elements("tile"))
            {
                int localTileId = TmxHelper.GetAttributeAsInt(elemTile, "id");
                var tiles       = from t in this.Tiles
                                  where t.Value.GlobalId == localTileId + firstId
                                  select t.Value;

                // Note that some old tile data may be sticking around
                if (tiles.Count() == 0)
                {
                    Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tilesetName, elemTile.ToString());
                }
                else
                {
                    tiles.First().ParseTileXml(elemTile, this, firstId);
                }
            }
        }
Пример #15
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Logger.WriteLine("Parsing object ...");

            // What kind of TmxObject are we creating?
            TmxObject tmxObject = null;

            if (xml.Element("ellipse") != null)
            {
                tmxObject = new TmxObjectEllipse();
            }
            else if (xml.Element("polygon") != null)
            {
                tmxObject = new TmxObjectPolygon();
            }
            else if (xml.Element("polyline") != null)
            {
                tmxObject = new TmxObjectPolyline();
            }
            else if (xml.Attribute("gid") != null)
            {
                uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");
                gid = TmxMath.GetTileIdWithoutFlags(gid);
                if (tmxMap.Tiles.ContainsKey(gid))
                {
                    tmxObject = new TmxObjectTile();
                }
                else
                {
                    // For some reason, the tile is not in any of our tilesets
                    // Warn the user and use a rectangle
                    Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", gid, xml.ToString());
                    tmxObject = new TmxObjectRectangle();
                }
            }
            else
            {
                // Just a rectangle
                tmxObject = new TmxObjectRectangle();
            }

            // Data found on every object type
            tmxObject.Name              = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObject.Type              = TmxHelper.GetAttributeAsString(xml, "type", "");
            tmxObject.Visible           = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObject.ParentObjectGroup = tmxObjectGroup;

            float x = TmxHelper.GetAttributeAsFloat(xml, "x");
            float y = TmxHelper.GetAttributeAsFloat(xml, "y");
            float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0);
            float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0);
            float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0);

            tmxObject.Position = new System.Drawing.PointF(x, y);
            tmxObject.Size     = new System.Drawing.SizeF(w, h);
            tmxObject.Rotation = r;

            tmxObject.Properties = TmxProperties.FromXml(xml);

            tmxObject.InternalFromXml(xml, tmxMap);

            return(tmxObject);
        }
Пример #16
0
        private static void ParseTilesetXml(XElement xml, TsxTileset tileset)
        {
            tileset.Name = TmxHelper.GetAttributeAsString(xml, "name");
            Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tileset.Name, tileset.FirstGlobalId);
            tileset.TileWidth  = TmxHelper.GetAttributeAsInt(xml, "tilewidth");
            tileset.TileHeight = TmxHelper.GetAttributeAsInt(xml, "tileheight");
            tileset.Spacing    = TmxHelper.GetAttributeAsInt(xml, "spacing", 0);
            tileset.Margin     = TmxHelper.GetAttributeAsInt(xml, "margin", 0);
            PointF   empty    = PointF.Empty;
            XElement xElement = xml.Element("tileoffset");

            if (xElement != null)
            {
                empty.X = (float)TmxHelper.GetAttributeAsInt(xElement, "x");
                empty.Y = (float)TmxHelper.GetAttributeAsInt(xElement, "y");
            }
            tileset.TileOffset = empty;
            List <TmxTile> list                   = new List <TmxTile>();
            TmxProperties  tmxProperties          = TmxProperties.FromXml(xml);
            string         propertyValueAsString  = tmxProperties.GetPropertyValueAsString("unity:namePrefix", "");
            string         propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", "");

            if (xml.Element("image") != null)
            {
                TmxImage tmxImage = TmxImage.FromXml(xml.Element("image"), propertyValueAsString, propertyValueAsString2);
                for (int i = tileset.Margin + tileset.TileHeight; i <= tmxImage.Size.Height; i += tileset.Spacing + tileset.TileHeight)
                {
                    for (int j = tileset.Margin + tileset.TileWidth; j <= tmxImage.Size.Width; j += tileset.Spacing + tileset.TileWidth)
                    {
                        uint    num      = (uint)list.Count();
                        uint    globalId = tileset.FirstGlobalId + num;
                        TmxTile tmxTile  = new TmxTile(tileset.TmxMap, globalId, num, tileset.Name, tmxImage);
                        tmxTile.Offset = empty;
                        tmxTile.SetTileSize(tileset.TileWidth, tileset.TileHeight);
                        tmxTile.SetLocationOnSource(j - tileset.TileWidth, i - tileset.TileHeight);
                        list.Add(tmxTile);
                    }
                }
            }
            else
            {
                foreach (XElement item in xml.Elements("tile"))
                {
                    TmxImage tmxImage2    = TmxImage.FromXml(item.Element("image"), propertyValueAsString, propertyValueAsString2);
                    uint     defaultValue = (uint)list.Count();
                    defaultValue = TmxHelper.GetAttributeAsUInt(item, "id", defaultValue);
                    uint    globalId2 = tileset.FirstGlobalId + defaultValue;
                    TmxTile tmxTile2  = new TmxTile(tileset.TmxMap, globalId2, defaultValue, tileset.Name, tmxImage2);
                    tmxTile2.Offset = empty;
                    tmxTile2.SetTileSize(tmxImage2.Size.Width, tmxImage2.Size.Height);
                    tmxTile2.SetLocationOnSource(0, 0);
                    list.Add(tmxTile2);
                }
            }
            tileset.Tiles.AddRange(list);
            Logger.WriteVerbose("Added {0} tiles", list.Count);
            foreach (XElement item2 in xml.Elements("tile"))
            {
                int localTileId = TmxHelper.GetAttributeAsInt(item2, "id");
                IEnumerable <TmxTile> source = from t in tileset.Tiles
                                               where t.GlobalId == localTileId + tileset.FirstGlobalId
                                               select t;
                if (source.Count() == 0)
                {
                    Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tileset.Name, item2.ToString());
                }
                else
                {
                    source.First().ParseTileXml(item2, tileset.TmxMap, tileset.FirstGlobalId);
                }
            }
        }