Exemplo n.º 1
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxMap);

            tmxObjectGroup.Objects = objects.ToList();

            return(tmxObjectGroup);
        }
Exemplo n.º 2
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup();

            // Order within Xml file is import for layer types
            tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap);

            tmxObjectGroup.Objects = objects.ToList();

            // Are we using a unity:layer override?
            tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", "");

            return(tmxObjectGroup);
        }
Exemplo n.º 3
0
        protected void FromXmlInternal(XElement xml)
        {
            // Get common elements amoung layer nodes from xml
            this.Name    = TmxHelper.GetAttributeAsString(xml, "name", "");
            this.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            this.Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1);

            // Get the offset
            PointF offset = new PointF(0, 0);

            offset.X    = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y    = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            this.Offset = offset;

            // Get all the properties
            this.Properties = TmxProperties.FromXml(xml);

            // Set the "ignore" setting on this object group
            this.Ignore = this.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False);

            // Explicit sorting properties
            this.ExplicitSortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                this.ExplicitSortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }

            // Are we using a unity:layer override?
            this.UnityLayerOverrideName = this.Properties.GetPropertyValueAsString("unity:layer", "");

            // Add all our children
            this.LayerNodes = TmxLayerNode.ListFromXml(xml, this, this.TmxMap);
        }
Exemplo n.º 4
0
 private void ParseImageLayerXml(XElement xml)
 {
     if (xml.Element("image") == null)
     {
         Logger.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", base.Name);
         base.Ignore = IgnoreSettings.True;
     }
     else
     {
         Width  = 1;
         Height = 1;
         string  imagePath = TmxHelper.GetAttributeAsFullPath(xml.Element("image"), "source");
         TmxTile value     = base.ParentMap.Tiles.First((KeyValuePair <uint, TmxTile> t) => t.Value.TmxImage.AbsolutePath.ToLower() == imagePath.ToLower()).Value;
         Data = new TmxData(this);
         TmxChunk tmxChunk = new TmxChunk(Data);
         tmxChunk.X      = 0;
         tmxChunk.Y      = 0;
         tmxChunk.Width  = 1;
         tmxChunk.Height = 1;
         tmxChunk.TileIds.Add(value.GlobalId);
         Data.Chunks.Add(tmxChunk);
         PointF offset = base.Offset;
         offset.Y -= (float)base.ParentMap.TileHeight;
         offset.Y += (float)value.TmxImage.Size.Height;
         PointF pointF = TmxMath.TileCornerInScreenCoordinates(base.ParentMap, 0, 0);
         offset.X   -= pointF.X;
         offset.Y   -= pointF.Y;
         offset.X   += TmxHelper.GetAttributeAsFloat(xml, "x", 0f);
         offset.Y   += TmxHelper.GetAttributeAsFloat(xml, "y", 0f);
         base.Offset = offset;
     }
 }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Program.WriteLine("Parsing object ...");
            Program.WriteVerbose(xml.ToString());

            // 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)
            {
                tmxObject = new TmxObjectTile();
            }
            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);
        }
Exemplo n.º 7
0
        protected void FromXmlInternal(XElement xml)
        {
            Name    = TmxHelper.GetAttributeAsString(xml, "name", "");
            Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1);
            Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1f);
            PointF offset = new PointF(0f, 0f);

            offset.X   = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0f);
            offset.Y   = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0f);
            Offset     = offset;
            Properties = TmxProperties.FromXml(xml);
            Ignore     = Properties.GetPropertyValueAsEnum("unity:ignore", IgnoreSettings.False);
            ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            if (Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                ExplicitSortingOrder = Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }
            UnityLayerOverrideName = Properties.GetPropertyValueAsString("unity:layer", "");
            LayerNodes             = ListFromXml(xml, this, ParentMap);
        }
Exemplo n.º 8
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(tmxMap);

            // Order within Xml file is import for layer types
            tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Opacity    = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1);
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            // Set the "ignore" setting on this object group
            tmxObjectGroup.Ignore = tmxObjectGroup.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Logger.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap);

            // The objects are ordered "visually" by Y position
            tmxObjectGroup.Objects = objects.OrderBy(o => TmxMath.ObjectPointFToMapSpace(tmxMap, o.Position).Y).ToList();

            // Are we using a unity:layer override?
            tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", "");

            return(tmxObjectGroup);
        }
Exemplo n.º 9
0
        public static TmxLayer FromXml(XElement elem, TmxLayerNode parent, TmxMap tmxMap)
        {
            TmxLayer tmxLayer = new TmxLayer(parent, tmxMap);

            tmxLayer.FromXmlInternal(elem);

            // We can build a layer from a "tile layer" (default) or an "image layer"
            if (elem.Name == "layer")
            {
                tmxLayer.Width  = TmxHelper.GetAttributeAsInt(elem, "width");
                tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height");
                tmxLayer.ParseData(elem.Element("data"));
            }
            else if (elem.Name == "imagelayer")
            {
                XElement xmlImage = elem.Element("image");
                if (xmlImage == null)
                {
                    Logger.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.Name);
                    tmxLayer.Ignore = IgnoreSettings.True;
                    return(tmxLayer);
                }

                // An image layer is sort of like an tile layer but with just one tile
                tmxLayer.Width  = 1;
                tmxLayer.Height = 1;

                // Find the "tile" that matches our image
                string  imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source");
                TmxTile tile      = tmxMap.Tiles.First(t => t.Value.TmxImage.AbsolutePath == imagePath).Value;
                tmxLayer.TileIds = new uint[1] {
                    tile.GlobalId
                };

                // The image layer needs to be tranlated in an interesting way when expressed as a tile layer
                PointF translated = tmxLayer.Offset;

                // Make up for height of a regular tile in the map
                translated.Y -= (float)tmxMap.TileHeight;

                // Make up for the height of this image
                translated.Y += (float)tile.TmxImage.Size.Height;

                // Correct for any orientation effects on the map (like isometric)
                // (We essentially undo the translation via orientation here)
                PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0);
                translated.X -= orientation.X;
                translated.Y -= orientation.Y;

                // Translate by the x and y coordiantes
                translated.X   += TmxHelper.GetAttributeAsFloat(elem, "x", 0);
                translated.Y   += TmxHelper.GetAttributeAsFloat(elem, "y", 0);
                tmxLayer.Offset = translated;
            }

            // Sometimes TMX files have "dead" tiles in them (tiles that were removed but are still referenced)
            // Remove these tiles from the layer by replacing them with zero
            for (int t = 0; t < tmxLayer.TileIds.Length; ++t)
            {
                uint tileId = tmxLayer.TileIds[t];
                tileId = TmxMath.GetTileIdWithoutFlags(tileId);
                if (!tmxMap.Tiles.ContainsKey(tileId))
                {
                    tmxLayer.TileIds[t] = 0;
                }
            }

            // Each layer will be broken down into "meshes" which are collections of tiles matching the same texture or animation
            tmxLayer.Meshes = TmxMesh.ListFromTmxLayer(tmxLayer);

            // Each layer may contain different collision types which are themselves put into "Collison Layers" to be processed later
            tmxLayer.BuildCollisionLayers();

            return(tmxLayer);
        }
Exemplo n.º 10
0
        public static TmxLayer FromXml(XElement elem, TmxMap tmxMap)
        {
            Program.WriteVerbose(elem.ToString());
            TmxLayer tmxLayer = new TmxLayer(tmxMap);

            // Order within Xml file is import for layer types
            tmxLayer.XmlElementIndex = elem.NodesBeforeSelf().Count();

            // Have to decorate layer names in order to force them into being unique
            // Also, can't have whitespace in the name because Unity will add underscores
            tmxLayer.Name = TmxHelper.GetAttributeAsString(elem, "name");

            tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1;
            tmxLayer.Opacity = TmxHelper.GetAttributeAsFloat(elem, "opacity", 1);

            PointF offset = new PointF(0, 0);

            offset.X        = TmxHelper.GetAttributeAsFloat(elem, "offsetx", 0);
            offset.Y        = TmxHelper.GetAttributeAsFloat(elem, "offsety", 0);
            tmxLayer.Offset = offset;

            // Set our properties
            tmxLayer.Properties = TmxProperties.FromXml(elem);

            // Set the "ignore" setting on this layer
            tmxLayer.Ignore = tmxLayer.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False);

            // We can build a layer from a "tile layer" (default) or an "image layer"
            if (elem.Name == "layer")
            {
                tmxLayer.Width  = TmxHelper.GetAttributeAsInt(elem, "width");
                tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height");
                tmxLayer.ParseData(elem.Element("data"));
            }
            else if (elem.Name == "imagelayer")
            {
                XElement xmlImage = elem.Element("image");
                if (xmlImage == null)
                {
                    Program.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.Name);
                    tmxLayer.Ignore = IgnoreSettings.True;
                    return(tmxLayer);
                }

                // An image layer is sort of like an tile layer but with just one tile
                tmxLayer.Width  = 1;
                tmxLayer.Height = 1;

                // Find the "tile" that matches our image
                string  imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source");
                TmxTile tile      = tmxMap.Tiles.First(t => t.Value.TmxImage.AbsolutePath == imagePath).Value;
                tmxLayer.TileIds = new uint[1] {
                    tile.GlobalId
                };

                // The image layer needs to be tranlated in an interesting way when expressed as a tile layer
                PointF translated = tmxLayer.Offset;

                // Make up for height of a regular tile in the map
                translated.Y -= (float)tmxMap.TileHeight;

                // Make up for the height of this image
                translated.Y += (float)tile.TmxImage.Size.Height;

                // Correct for any orientation effects on the map (like isometric)
                // (We essentially undo the translation via orientation here)
                PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0);
                translated.X -= orientation.X;
                translated.Y -= orientation.Y;

                // Translate by the x and y coordiantes
                translated.X   += TmxHelper.GetAttributeAsFloat(elem, "x", 0);
                translated.Y   += TmxHelper.GetAttributeAsFloat(elem, "y", 0);
                tmxLayer.Offset = translated;
            }

            // Each layer will be broken down into "meshes" which are collections of tiles matching the same texture or animation
            tmxLayer.Meshes = TmxMesh.ListFromTmxLayer(tmxLayer);

            return(tmxLayer);
        }
Exemplo n.º 11
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);
        }