Ejemplo n.º 1
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);
            }

            AddTileSetImage(node["image"]);

            _mapTileWidth = map.MapRenderParameter.TileWidth;
            _mapTileHeight = map.MapRenderParameter.TileHeight;

            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"];
                    List<TileObject> tileObjects = null;
                    if (objectsNode != null)
                    {
                        tileObjects = new List<TileObject>();
                        foreach (NanoXMLNode tileObjNode in objectsNode.SubNodes)
                        {
                            TileObject tObj = new TileObject(tileObjNode, _mapTileWidth, _mapTileHeight);
                            //tObj.ScaleObject(map.TileWidth, map.TileHeight, map.Orientation);
                            tObj = tObj.ScaleObject(map.MapRenderParameter) as TileObject;
                            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);
                    }

                    // Load Tile Image, if this is an ImageCollection TileSet
                    AddTileSetImage(subNode["image"], tileObjects);
                }
            }
        }
Ejemplo n.º 2
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
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);
                    }
                }
            }
        }