Beispiel #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);
        }
Beispiel #2
0
        public static TmxProperties FromXml(XElement elem)
        {
            TmxProperties tmxProps = new TmxProperties();

            var props = from elem1 in elem.Elements("properties")
                        from elem2 in elem1.Elements("property")
                        select new
            {
                Name  = TmxHelper.GetAttributeAsString(elem2, "name"),
                Value = TmxHelper.GetAttributeAsString(elem2, "value"),
            };

            if (props.Count() > 0)
            {
                Program.WriteLine("Parse properites ...");
                Program.WriteVerbose(elem.Element("properties").ToString());
            }

            foreach (var p in props)
            {
                tmxProps.PropertyMap[p.Name] = p.Value;
            }

            return(tmxProps);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
        public static TmxProperties FromXml(XElement elem)
        {
            TmxProperties tmxProps = new TmxProperties();

            var props = from elem1 in elem.Elements("properties")
                        from elem2 in elem1.Elements("property")
                        select new
                        {
                            Name = TmxHelper.GetAttributeAsString(elem2, "name"),
                            Type = TmxHelper.GetAttributeAsEnum(elem2, "type", TmxPropertyType.String),

                            // Value may be attribute or inner text
                            Value = TmxHelper.GetAttributeAsString(elem2, "value", null) ?? elem2.Value,
                        };

            if (props.Count() > 0)
            {
                Program.WriteLine("Parse properites ...");
                Program.WriteVerbose(elem.Element("properties").ToString());
            }

            foreach (var p in props)
            {
                tmxProps.PropertyMap[p.Name] = new TmxProperty { Name = p.Name, Type = p.Type, Value = p.Value };
            }

            return tmxProps;
        }
Beispiel #6
0
        public static TmxProperties GetPropertiesWithTypeDefaults(TmxHasProperties hasProperties, TmxObjectTypes objectTypes)
        {
            TmxProperties tmxProperties = new TmxProperties();
            string        key           = null;

            if (hasProperties is TmxObject)
            {
                key = (hasProperties as TmxObject).Type;
            }
            TmxObjectType valueOrNull = objectTypes.GetValueOrNull(key);

            if (valueOrNull != null)
            {
                foreach (TmxObjectTypeProperty value in valueOrNull.Properties.Values)
                {
                    tmxProperties.PropertyMap[value.Name] = new TmxProperty
                    {
                        Name  = value.Name,
                        Type  = value.Type,
                        Value = value.Default
                    };
                }
            }
            foreach (TmxProperty value2 in hasProperties.Properties.PropertyMap.Values)
            {
                tmxProperties.PropertyMap[value2.Name] = value2;
            }
            return(tmxProperties);
        }
        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);
        }
        public static TmxProperties FromXml(XElement elem)
        {
            TmxProperties tmxProps = new TmxProperties();

            var props = from elem1 in elem.Elements("properties")
                        from elem2 in elem1.Elements("property")
                        select new
                        {
                            Name = TmxHelper.GetAttributeAsString(elem2, "name"),
                            Value = TmxHelper.GetAttributeAsString(elem2, "value"),
                        };

            if (props.Count() > 0)
            {
                Program.WriteLine("Parse properites ...");
                Program.WriteVerbose(elem.Element("properties").ToString());
            }

            foreach (var p in props)
            {
                tmxProps.PropertyMap[p.Name] = p.Value;
            }

            return tmxProps;
        }
Beispiel #9
0
        public static TsxTileset FromImageLayerXml(XElement xml, TmxMap tmxMap)
        {
            TsxTileset tsxTileset = new TsxTileset(tmxMap);

            tsxTileset.Name = TmxHelper.GetAttributeAsString(xml, "name");
            XElement xElement = xml.Element("image");

            if (xElement == null)
            {
                Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tsxTileset.Name);
                return(null);
            }
            TmxProperties tmxProperties          = TmxProperties.FromXml(xml);
            string        propertyValueAsString  = tmxProperties.GetPropertyValueAsString("unity:namePrefix", "");
            string        propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", "");
            TmxImage      tmxImage = TmxImage.FromXml(xElement, propertyValueAsString, propertyValueAsString2);
            uint          num      = 1u;

            if (tmxMap.Tiles.Count > 0)
            {
                num = tmxMap.Tiles.Max((KeyValuePair <uint, TmxTile> t) => t.Key) + 1;
            }
            uint    num2     = 1u;
            uint    globalId = num + num2;
            TmxTile tmxTile  = new TmxTile(tmxMap, globalId, num2, tsxTileset.Name, tmxImage);

            tmxTile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
            tmxTile.SetLocationOnSource(0, 0);
            tsxTileset.Tiles.Add(tmxTile);
            tsxTileset.Tiles.ForEach(delegate(TmxTile t)
            {
                tmxMap.Tiles.Add(t.GlobalId, t);
            });
            return(tsxTileset);
        }
        public static TmxProperties FromXml(XElement elem)
        {
            TmxProperties tmxProps = new TmxProperties();

            var props = from elem1 in elem.Elements("properties")
                        from elem2 in elem1.Elements("property")
                        select new
            {
                Name = TmxHelper.GetAttributeAsString(elem2, "name"),
                Type = TmxHelper.GetAttributeAsEnum(elem2, "type", TmxPropertyType.String),

                // Value may be attribute or inner text
                Value = TmxHelper.GetAttributeAsString(elem2, "value", null) ?? elem2.Value,
            };

            if (props.Count() > 0)
            {
                Logger.WriteVerbose("Parse properites ...");
            }

            foreach (var p in props)
            {
                tmxProps.PropertyMap[p.Name] = new TmxProperty {
                    Name = p.Name, Type = p.Type, Value = p.Value
                };
            }

            return(tmxProps);
        }
        private void ParseTilesetFromImageLayer(XElement elemImageLayer)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name");

            XElement xmlImage = elemImageLayer.Element("image");

            if (xmlImage == null)
            {
                Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName);
                return;
            }

            TmxProperties properties = TmxProperties.FromXml(elemImageLayer);
            string        prefix     = properties.GetPropertyValueAsString("unity:namePrefix", "");
            string        postfix    = properties.GetPropertyValueAsString("unity:namePostfix", "");

            TmxImage tmxImage = TmxImage.FromXml(xmlImage, prefix, postfix);

            // The "firstId" is is always one more than all the tiles that we've already parsed (which may be zero)
            uint firstId = 1;

            if (this.Tiles.Count > 0)
            {
                firstId = this.Tiles.Max(t => t.Key) + 1;
            }

            uint localId  = 1;
            uint globalId = firstId + localId;

            TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage);

            tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
            tile.SetLocationOnSource(0, 0);
            this.Tiles[tile.GlobalId] = tile;
        }
Beispiel #12
0
        private void ParseMapXml(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            //Program.WriteVerbose(doc.ToString()); // Some TMX files are far too big (cause out of memory exception) so don't do this
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Beispiel #13
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsString(map, "orientation");
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // We only support orthogonal maps
            if (this.Orientation != "orthogonal")
            {
                TmxException.ThrowFormat("Only orthogonal maps are supported. This map is set to \"{0}\"", this.Orientation);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
Beispiel #14
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);
        }
Beispiel #15
0
        public static TmxProperties FromXml(XElement elem)
        {
            TmxProperties tmxProperties = new TmxProperties();
            var           enumerable    = from elem1 in elem.Elements("properties")
                                          from elem2 in elem1.Elements("property")
                                          select new
            {
                Name  = TmxHelper.GetAttributeAsString(elem2, "name"),
                Type  = TmxHelper.GetAttributeAsEnum(elem2, "type", TmxPropertyType.String),
                Value = (TmxHelper.GetAttributeAsString(elem2, "value", null) ?? elem2.Value)
            };

            if (enumerable.Count() > 0)
            {
                Logger.WriteVerbose("Parse properties ...");
            }
            foreach (var item in enumerable)
            {
                tmxProperties.PropertyMap[item.Name] = new TmxProperty
                {
                    Name  = item.Name,
                    Type  = item.Type,
                    Value = item.Value
                };
            }
            return(tmxProperties);
        }
Beispiel #16
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");
            XElement xElement = doc.Element("map");

            try
            {
                Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(xElement, "orientation");
                StaggerAxis         = TmxHelper.GetAttributeAsEnum(xElement, "staggeraxis", MapStaggerAxis.Y);
                StaggerIndex        = TmxHelper.GetAttributeAsEnum(xElement, "staggerindex", MapStaggerIndex.Odd);
                HexSideLength       = TmxHelper.GetAttributeAsInt(xElement, "hexsidelength", 0);
                DrawOrderHorizontal = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("right") ? 1 : (-1));
                DrawOrderVertical   = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("down") ? 1 : (-1));
                Width           = TmxHelper.GetAttributeAsInt(xElement, "width");
                Height          = TmxHelper.GetAttributeAsInt(xElement, "height");
                TileWidth       = TmxHelper.GetAttributeAsInt(xElement, "tilewidth");
                TileHeight      = TmxHelper.GetAttributeAsInt(xElement, "tileheight");
                BackgroundColor = TmxHelper.GetAttributeAsColor(xElement, "backgroundcolor", new Color32(128, 128, 128, 255));
            }
            catch (Exception inner)
            {
                TmxException.FromAttributeException(inner, xElement);
            }
            Properties = TmxProperties.FromXml(xElement);
            IsResource = Properties.GetPropertyValueAsBoolean("unity:resource", false);
            IsResource = (IsResource || !string.IsNullOrEmpty(Properties.GetPropertyValueAsString("unity:resourcePath", null)));
            ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            ParseAllTilesets(doc);
            ParseAllTemplates(doc);
            LayerNodes      = TmxLayerNode.ListFromXml(xElement, null, this);
            MapSizeInPixels = CalculateMapSizeInPixels();
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            Visit(visitor);
        }
Beispiel #17
0
 public TmxMap()
 {
     IsLoaded                 = false;
     Tilesets                 = new List <TsxTileset>();
     TemplateGroups           = new List <TgxTemplateGroup>();
     Properties               = new TmxProperties();
     ExplicitSortingLayerName = "";
     LayerNodes               = new List <TmxLayerNode>();
 }
Beispiel #18
0
 public TmxTile(TmxMap tmxMap, uint globalId, uint localId, string tilesetName, TmxImage tmxImage)
 {
     TmxMap      = TmxMap;
     GlobalId    = globalId;
     LocalId     = localId;
     TmxImage    = tmxImage;
     Properties  = new TmxProperties();
     ObjectGroup = new TmxObjectGroup(null, TmxMap);
     Animation   = TmxAnimation.FromTileId(globalId);
     Meshes      = new List <TmxMesh>();
 }
Beispiel #19
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);
        }
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);

            // Get all our child layer nodes
            this.LayerNodes = TmxLayerNode.ListFromXml(map, null, this);

            // Calcuate the size of the map. Isometric and hex maps make this more complicated than a simple width and height thing.
            this.MapSizeInPixels = CalculateMapSizeInPixels();

            // Visit each node in the map to assign display order
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            this.Visit(visitor);
        }
Beispiel #21
0
        public void ParseTileXml(XElement elem, TmxMap tmxMap, uint firstId)
        {
            Logger.WriteVerbose("Parse tile data (gid = {0}, id {1}) ...", GlobalId, LocalId);
            Properties = TmxProperties.FromXml(elem);
            XElement xElement = elem.Element("objectgroup");

            if (xElement != null)
            {
                ObjectGroup = TmxObjectGroup.FromXml(xElement, null, tmxMap);
                FixTileColliderObjects(tmxMap);
            }
            XElement xElement2 = elem.Element("animation");

            if (xElement2 != null)
            {
                Animation = TmxAnimation.FromXml(xElement2, firstId);
            }
        }
Beispiel #22
0
        public static TmxLayer FromXml(XElement elem, int layerIndex)
        {
            Program.WriteVerbose(elem.ToString());
            TmxLayer tmxLayer = new TmxLayer();

            // 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.DefaultName = TmxHelper.GetAttributeAsString(elem, "name");
            tmxLayer.UniqueName  = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_");

            tmxLayer.Visible    = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1;
            tmxLayer.Width      = TmxHelper.GetAttributeAsInt(elem, "width");
            tmxLayer.Height     = TmxHelper.GetAttributeAsInt(elem, "height");
            tmxLayer.Properties = TmxProperties.FromXml(elem);

            tmxLayer.ParseData(elem.Element("data"));

            return(tmxLayer);
        }
Beispiel #23
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);
        }
Beispiel #24
0
        public static TmxProperties GetPropertiesWithTypeDefaults(TmxHasProperties hasProperties, TmxObjectTypes objectTypes)
        {
            TmxProperties tmxProperties = new TmxProperties();

            // Fill in all the default properties first
            // (Note: At the moment, only TmxObject has default properties it inherits from TmxObjectType)
            string objectTypeName = null;

            if (hasProperties is TmxObject)
            {
                TmxObject tmxObject = hasProperties as TmxObject;
                objectTypeName = tmxObject.Type;
            }
            else if (hasProperties is TmxLayer)
            {
                TmxLayer tmxLayer = hasProperties as TmxLayer;
                objectTypeName = tmxLayer.Name;
            }

            // If an object type has been found then copy over all the default values for properties
            TmxObjectType tmxObjectType = objectTypes.GetValueOrNull(objectTypeName);

            if (tmxObjectType != null)
            {
                foreach (TmxObjectTypeProperty tmxTypeProp in tmxObjectType.Properties.Values)
                {
                    tmxProperties.PropertyMap[tmxTypeProp.Name] = new TmxProperty()
                    {
                        Name = tmxTypeProp.Name, Type = tmxTypeProp.Type, Value = tmxTypeProp.Default
                    };
                }
            }

            // Now add all the object properties (which may override some of the default properties)
            foreach (TmxProperty tmxProp in hasProperties.Properties.PropertyMap.Values)
            {
                tmxProperties.PropertyMap[tmxProp.Name] = tmxProp;
            }

            return(tmxProperties);
        }
        public void ParseTileXml(XElement elem, TmxMap tmxMap, uint firstId)
        {
            Logger.WriteVerbose("Parse tile data (gid = {0}, id {1}) ...", this.GlobalId, this.LocalId);

            this.Properties = TmxProperties.FromXml(elem);

            // Do we have an object group for this tile?
            XElement elemObjectGroup = elem.Element("objectgroup");
            if (elemObjectGroup != null)
            {
                this.ObjectGroup = TmxObjectGroup.FromXml(elemObjectGroup, null, tmxMap);
                FixTileColliderObjects(tmxMap);
            }

            // Is this an animated tile?
            XElement elemAnimation = elem.Element("animation");
            if (elemAnimation != null)
            {
                this.Animation = TmxAnimation.FromXml(elemAnimation, firstId);
            }
        }
Beispiel #26
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteLine("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Beispiel #27
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);
        }
Beispiel #28
0
        public static TmxLayer FromXml(XElement elem, TmxMap tmxMap)
        {
            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)
                {
                    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.UnityLayerOverrideName = tmxLayer.Properties.GetPropertyValueAsString("unity:layer", "");
            tmxLayer.BuildCollisionLayers();

            return(tmxLayer);
        }
        // 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);
                }
            }
        }
Beispiel #30
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);
        }
Beispiel #31
0
        public static TmxLayer FromXml(XElement elem, TmxMap tmxMap, int layerIndex)
        {
            Program.WriteVerbose(elem.ToString());
            TmxLayer tmxLayer = new TmxLayer();

            // 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.DefaultName = TmxHelper.GetAttributeAsString(elem, "name");
            tmxLayer.UniqueName  = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_");

            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;

            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.DefaultName);
                    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.Path == 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;
            }

            return(tmxLayer);
        }
Beispiel #32
0
 public TmxMap()
 {
     this.IsLoaded   = false;
     this.Properties = new TmxProperties();
 }