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; }
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); }