Ejemplo n.º 1
0
        static CCTMXLayer ParseLayer(XmlNode nodeData, int cols, int rows, Dictionary <int, string> gidToFiles, Dictionary <int, Dictionary <string, string> > gidToTileProperties)
        {
            XmlNode data     = nodeData.SelectSingleNode("data");
            string  name     = nodeData.Attributes["name"].InnerText;
            string  encoding = data.Attributes["encoding"].InnerText;

            NSUtils.Assert(encoding == "csv", "cocos2d:CCTMXMapParser: Unsupported encoding {0} found. Only csv is supported now.", encoding);

            string          csvData = data.InnerText;
            CCTMXTiledLayer layer   = new CCTMXTiledLayer();

            layer.name     = name;
            layer.visiable = nodeData.Attributes["visible"] != null && nodeData.Attributes["visible"].InnerText == "0";
            layer.tiles    = new CCTMXTile[rows][];
            for (int row = 0; row < rows; row++)
            {
                layer.tiles[row] = new CCTMXTile[cols];
            }
            string[] layerData  = csvData.Split(',');
            int      totalTiles = cols * rows;

            for (int i = 0; i < totalTiles; i++)
            {
                int col    = i % cols;
                int row    = Mathf.FloorToInt(i / cols);
                int tileId = int.Parse(layerData[i].ToString().Trim());
                if (tileId > 0)
                {
                    CCTMXTile tile = new CCTMXTile();
                    tile.gid = tileId;
                    tile.col = col;
                    tile.row = row;
                    try{
                        tile.file = gidToFiles[tile.gid];
                    }catch {
                        throw new UnityEngine.UnityException(string.Format("cocos2d: CCTMXMapParser: gid [{0}] not found", tile.gid));
                    }
                    Dictionary <string, string> tileProperties = null;
                    if (!gidToTileProperties.TryGetValue(tile.gid, out tileProperties))
                    {
                        tileProperties = null;
                    }
                    tile.sharedProperties = tileProperties;
                    layer.tiles[row][col] = tile;
                }
            }

            XmlNode propertiesNode = nodeData.SelectSingleNode("properties");

            if (propertiesNode != null)
            {
                layer.properties = new Dictionary <string, string>();
                ParseProperties(propertiesNode, layer.properties);
            }
            return(layer);
        }
Ejemplo n.º 2
0
        static CCTMXLayer ParseObjectGroup(XmlNode nodeData, int cols, int rows, int tileWidth, int tileHeight, Dictionary <int, string> gidToFiles, Dictionary <int, Dictionary <string, string> > gidToTileProperties)
        {
            string name = nodeData.Attributes["name"].InnerText;

            CCTMXObjectGroup layer = new CCTMXObjectGroup();

            layer.name    = name;
            layer.objects = new List <CCTMXTile> ();

            layer.visiable = nodeData.Attributes["visible"] != null &&
                             nodeData.Attributes["visible"].InnerText == "0";

            XmlNodeList childNodes = nodeData.ChildNodes;

            var enumerator = childNodes.GetEnumerator();

            while (enumerator.MoveNext())
            {
                XmlNode childNode = (XmlNode)enumerator.Current;
                if (childNode.Name == "object")
                {
                    float mapX = float.Parse(childNode.Attributes["x"].InnerText);
                    float mapY = float.Parse(childNode.Attributes["y"].InnerText);
                    int   col  = Mathf.RoundToInt(mapX / tileWidth);

                    XmlAttribute gidAttr = childNode.Attributes["gid"];
                    CCTMXTile    tile    = new CCTMXTile();
                    tile.col = col;
                    if (gidAttr != null)
                    {
                        int row = Mathf.RoundToInt(mapY / tileHeight) - 1;
                        tile.row = row;
                        tile.gid = int.Parse(gidAttr.InnerText);
                        if (!gidToFiles.TryGetValue(tile.gid, out tile.file))
                        {
                            throw new KeyNotFoundException(string.Format("CCTMXParser: file key not found with tile gid={0}, grid=[{1}, {2}], pos=[{3:0},{4,0}]", tile.gid, col, row, mapX, mapY));
                        }

                        Dictionary <string, string> tileProperties = null;
                        if (!gidToTileProperties.TryGetValue(tile.gid, out tileProperties))
                        {
                            tileProperties = null;
                        }
                        tile.sharedProperties = tileProperties;
                    }
                    else
                    {
                        int row = Mathf.RoundToInt(mapY / tileHeight);
                        tile.row = row;
                        tile.gid = -1;
                        XmlAttribute wAttr = childNode.Attributes["width"];
                        XmlAttribute hAttr = childNode.Attributes["height"];
                        tile.polygonPoints = new List <Vector2>();
                        if (wAttr != null && hAttr != null)
                        {
                            float   w  = float.Parse(wAttr.InnerText);
                            float   h  = float.Parse(hAttr.InnerText);
                            Vector2 p0 = new Vector2(0, 0);
                            Vector2 p1 = new Vector2(w / tileWidth, 0);
                            Vector2 p2 = new Vector2(w / tileWidth, h / tileHeight);
                            Vector2 p3 = new Vector2(0, h / tileHeight);

                            tile.polygonPoints.Add(p0);
                            tile.polygonPoints.Add(p1);
                            tile.polygonPoints.Add(p2);
                            tile.polygonPoints.Add(p3);
                        }
                        else
                        {
                            XmlNode polylineNode = childNode.SelectSingleNode("polyline");
                            if (polylineNode != null)
                            {
                                string   text      = polylineNode.Attributes["points"].InnerText;
                                string[] pointStrs = text.Split(' ');
                                for (int i = 0; i < pointStrs.Length; i++)
                                {
                                    string pointStr = pointStrs[i];
                                    if (pointStr.Trim().Length == 0)
                                    {
                                        continue;
                                    }
                                    string[] pointParts = pointStr.Split(',');
                                    float    x          = float.Parse(pointParts[0]);
                                    float    y          = float.Parse(pointParts[1]);
                                    int      pcol       = Mathf.RoundToInt(x / tileWidth);
                                    int      prow       = Mathf.RoundToInt(y / tileHeight);
                                    tile.polygonPoints.Add(new Vector2(pcol, prow));
                                }
                            }
                            else
                            {
                                NSUtils.Assert(false, "cocos2d:CCTMXParse: Unsupported shape found: {0}, {1}, {2}", name, Mathf.RoundToInt(mapY / tileHeight), col);
                            }
                        }
                    }
                    layer.objects.Add(tile);

                    XmlNode propertiesNode = childNode.SelectSingleNode("properties");
                    if (propertiesNode != null)
                    {
                        tile.properties = new Dictionary <string, string>();
                        ParseProperties(propertiesNode, tile.properties);
                    }
                }
                else if (childNode.Name == "properties")
                {
                    if (layer.properties == null)
                    {
                        layer.properties = new Dictionary <string, string>();
                    }
                    ParseProperties(childNode, layer.properties);
                }
            }
            return(layer);
        }