Esempio n. 1
0
    private Map ParseMap(XmlNodeList nodes)
    {
        Map map = new Map();

        foreach (XmlNode node in nodes)
        {
            //Map Info
            map.version     = (node.Attributes.GetNamedItem("version").InnerText);
            map.orientation = (node.Attributes.GetNamedItem("version").InnerText);
            map.width       = (Convert.ToInt16(node.Attributes.GetNamedItem("width").InnerText));
            map.height      = (Convert.ToInt16(node.Attributes.GetNamedItem("height").InnerText));

            //Tile info
            map.tilewidth  = (Convert.ToInt16(node.Attributes.GetNamedItem("tilewidth").InnerText));
            map.tileheight = (Convert.ToInt16(node.Attributes.GetNamedItem("tileheight").InnerText));
            var imageNode = node.SelectSingleNode("//image");

            map.imagesrc    = imageNode.Attributes ["source"].Value;
            map.imagewidth  = Convert.ToInt16(imageNode.Attributes ["width"].Value);
            map.imageheight = Convert.ToInt16(imageNode.Attributes ["height"].Value);

            //Loop the layers
            foreach (XmlNode aLayerNode in node.SelectNodes("layer"))
            {
                string layerName = aLayerNode.Attributes ["name"].Value;
                Layer  aLayer    = new Layer(layerName);
                //loop the tiles
                Debug.Log("Found " + layerName);
                foreach (XmlNode aTile in aLayerNode.SelectNodes("//layer[@name='" + layerName + "']/data/tile"))
                {
                    Tile t = new Tile();
                    t.gid = Convert.ToInt16(aTile.Attributes ["gid"].Value);
                    aLayer.AddTile(t);
                }
                map.AddLayer(aLayer);
            }

            //Loop the Object Groups
            foreach (XmlNode anObjectGroup in node.SelectNodes("objectgroup"))
            {
                string      groupName   = anObjectGroup.Attributes ["name"].Value;
                ObjectGroup objectGroup = new ObjectGroup(groupName);
                foreach (XmlNode anObject in anObjectGroup.SelectNodes("//objectgroup[@name='" + groupName + "']/object"))
                {
                    if (anObject.Attributes ["name"] != null)
                    {
                        string objName = anObject.Attributes ["name"].Value;

                        MapObject mo = new MapObject(objName);

                        int x = Convert.ToInt16(anObject.Attributes ["x"].Value);
                        int y = Convert.ToInt16(anObject.Attributes ["y"].Value);

                        if (anObject.Attributes ["width"] != null)
                        {
                            int width = Convert.ToInt16(anObject.Attributes ["width"].Value);
                            mo.width = width;
                        }

                        if (anObject.Attributes ["height"] != null)
                        {
                            int height = Convert.ToInt16(anObject.Attributes ["height"].Value);
                            mo.height = height;
                        }

                        string type = anObject.Attributes ["type"].Value;

                        if (type.Equals("PolygonCollider2d"))
                        {
                            XmlNode polygonInfo = anObjectGroup.SelectSingleNode("//objectgroup[@name='" + groupName + "']/object[@name='" + objName + "']/polygon");

                            string   pointsAsString = polygonInfo.Attributes ["points"].Value;
                            string[] pointsSplit    = pointsAsString.Split(' ');

                            foreach (string aPoint in pointsSplit)
                            {
                                PolygonPoint polygonPoint = new PolygonPoint(aPoint);
                                mo.AddPoint(polygonPoint);
                            }
                        }

                        mo.type = type;
                        mo.x    = x;
                        mo.y    = y;

                        objectGroup.AddObject(mo);
                    }
                }
                map.AddObjectGroup(objectGroup);
            }
        }

        return(map);
    }