Example #1
0
        public MapObject(XmlNode node)
        {
            if (node.Attributes["name"] != null)
            {
                Name = node.Attributes["name"].Value;
            }
            else
            {
                Name = "Object";
            }

            if (node.Attributes["type"] != null)
            {
                Type = node.Attributes["type"].Value;
            }

            // values default to 0 if the attribute is missing from the node
            int x = node.Attributes["x"] != null?int.Parse(node.Attributes["x"].Value, CultureInfo.InvariantCulture) : 0;

            int y = node.Attributes["y"] != null?int.Parse(node.Attributes["y"].Value, CultureInfo.InvariantCulture) : 0;

            int width = node.Attributes["width"] != null?int.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture) : 0;

            int height = node.Attributes["height"] != null?int.Parse(node.Attributes["height"].Value, CultureInfo.InvariantCulture) : 0;

            Bounds = new Rect(x, y, width, height);

            MapObjectType = MapObjectType.Box;

            XmlNode propertiesNode = node["properties"];

            if (propertiesNode != null)
            {
                Properties = new PropertyCollection(propertiesNode);
            }

            // stores a string of points to parse out if this object is a polygon or polyline
            string pointsAsString = null;

            // if there's a GID, it's a tile object
            if (node.Attributes["gid"] != null)
            {
                //ObjectType = MapObjectType.Tile;
                GID = int.Parse(node.Attributes["gid"].Value, CultureInfo.InvariantCulture);
            }
            // if there's an ellipse node, it's an ellipse object
            else if (node["ellipse"] != null)
            {
                MapObjectType = MapObjectType.Ellipse;
            }
            // if there's a polygon node, it's a polygon object
            else if (node["polygon"] != null)
            {
                //ObjectType = MapObjectType.Polygon;
                pointsAsString = node["polygon"].Attributes["points"].Value;
                MapObjectType  = MapObjectType.Polygon;
            }
            // if there's a polyline node, it's a polyline object
            else if (node["polyline"] != null)
            {
                //ObjectType = MapObjectType.Polyline;
                pointsAsString = node["polyline"].Attributes["points"].Value;
                MapObjectType  = MapObjectType.Polyline;
            }

            // if we have some points to parse, we do that now
            if (pointsAsString != null)
            {
                // points are separated first by spaces
                Points = new List <Vector2>();
                string[] pointPairs = pointsAsString.Split(' ');
                foreach (string p in pointPairs)
                {
                    // then we split on commas
                    string[] coords = p.Split(',');

                    // then we parse the X/Y coordinates
                    Points.Add(new Vector2(
                                   float.Parse(coords[0], CultureInfo.InvariantCulture),
                                   float.Parse(coords[1], CultureInfo.InvariantCulture)));
                }
            }
        }
Example #2
0
 internal MapObjectLayer(string name, int width, int height, int layerDepth, bool visible, float opacity, PropertyCollection properties, List <MapObject> initialObjects)
     : base(name, width, height, layerDepth, visible, opacity, properties)
 {
     Objects = new List <MapObject>();
     initialObjects.ForEach(AddObject);
 }
Example #3
0
 internal Layer(string name, int width, int height, int layerDepth, bool visible, float opacity, PropertyCollection properties)
 {
     this.Name       = name;
     this.Width      = width;
     this.Height     = height;
     this.LayerDepth = layerDepth;
     this.Visible    = visible;
     this.Opacity    = opacity;
     this.Properties = properties;
 }
Example #4
0
        /// <summary>
        /// Load this TileSet's information from node
        /// </summary>
        /// <param name="node">NanoXMLNode to parse</param>
        /// <param name="map">Reference to the Map this TileSet is in</param>
        /// <param name="firstGID">First ID is a per-Map property, so External TileSets won't have this info in the node</param>
        protected TileSet(NanoXMLNode node, Map map, int firstGID = 1)
        {
            if (node.GetAttribute("firstgid") == null || !int.TryParse(node.GetAttribute("firstgid").Value, out FirstId))
            {
                FirstId = firstGID;
            }

            //this.FirstId = int.Parse(node.GetAttribute("firstgid").Value, CultureInfo.InvariantCulture);
            this.Name       = node.GetAttribute("name").Value;
            this.TileWidth  = int.Parse(node.GetAttribute("tilewidth").Value, CultureInfo.InvariantCulture);
            this.TileHeight = int.Parse(node.GetAttribute("tileheight").Value, CultureInfo.InvariantCulture);

            if (node.GetAttribute("spacing") != null)
            {
                this.Spacing = int.Parse(node.GetAttribute("spacing").Value, CultureInfo.InvariantCulture);
            }

            if (node.GetAttribute("margin") != null)
            {
                this.Margin = int.Parse(node.GetAttribute("margin").Value, CultureInfo.InvariantCulture);
            }

            NanoXMLNode tileOffset = node["tileoffset"];

            if (tileOffset != null)
            {
                this.TileOffsetX = int.Parse(tileOffset.GetAttribute("x").Value, CultureInfo.InvariantCulture);
                this.TileOffsetY = -int.Parse(tileOffset.GetAttribute("y").Value, CultureInfo.InvariantCulture);
            }

            NanoXMLNode imageNode = node["image"];

            this.Image = imageNode.GetAttribute("source").Value;


            // if the image is in any director up from us, just take the filename
            //if (this.Image.StartsWith(".."))
            //	this.Image = Path.GetFileName(this.Image);

            if (imageNode.GetAttribute("trans") != null)
            {
                string color = imageNode.GetAttribute("trans").Value;
                string r     = color.Substring(0, 2);
                string g     = color.Substring(2, 2);
                string b     = color.Substring(4, 2);
                this.ColorKey = new Color((byte)Convert.ToInt32(r, 16), (byte)Convert.ToInt32(g, 16), (byte)Convert.ToInt32(b, 16));
            }
            foreach (NanoXMLNode subNode in node.SubNodes)
            {
                if (subNode.Name.Equals("tile"))
                {
                    int id = this.FirstId + int.Parse(subNode.GetAttribute("id").Value, CultureInfo.InvariantCulture);

                    // Load Tile Properties, if any
                    NanoXMLNode propertiesNode = subNode["properties"];
                    if (propertiesNode != null)
                    {
                        PropertyCollection properties = new PropertyCollection(propertiesNode);                        //Property.ReadProperties(propertiesNode);
                        this.TileProperties.Add(id, properties);
                    }

                    // Load Tile Animation, if any
                    NanoXMLNode animationNode = subNode["animation"];
                    if (animationNode != null)
                    {
                        TileAnimation _tileAnimation = new TileAnimation();
                        foreach (NanoXMLNode frame in animationNode.SubNodes)
                        {
                            if (!frame.Name.Equals("frame"))
                            {
                                continue;
                            }
                            int tileid   = int.Parse(frame.GetAttribute("tileid").Value, CultureInfo.InvariantCulture) + FirstId;
                            int duration = int.Parse(frame.GetAttribute("duration").Value, CultureInfo.InvariantCulture);
                            _tileAnimation.AddTileFrame(tileid, duration);
                        }
                        this.AnimatedTiles.Add(id, _tileAnimation);
                    }

                    // Load Tile Objects, if any
                    NanoXMLNode objectsNode = subNode["objectgroup"];
                    if (objectsNode != null)
                    {
                        List <TileObject> tileObjects = new List <TileObject>();
                        foreach (NanoXMLNode tileObjNode in objectsNode.SubNodes)
                        {
                            TileObject tObj = new TileObject(tileObjNode);
                            tObj.ScaleObject(map.TileWidth, map.TileHeight, map.Orientation);
                            tileObjects.Add(tObj);
                        }
                        // There's a bug in Tiled 0.10.1- where the objectgroup node won't go away even if you delete all objects from a tile's collision group.
                        if (tileObjects.Count > 0)
                        {
                            TilesObjects.Add(id, tileObjects);
                        }
                    }
                }
            }
        }