Пример #1
0
        //DEBUG
        public static void Debug(CCTMXMap map)
        {
//			string s = "[TMX:" + map.fileName + "]\n";
//			if (map.properties != null) {
//				s += "  " + "Properties: ";
//				var enumerator = map.properties.GetEnumerator();
//				while (enumerator.MoveNext()) {
//					KeyValuePair<object, object> propPair = enumerator.Current;
//					s += propPair.Key + "=" + propPair.Value + " ";
//				}
//				s += "\n";
//			}
//			s += "  " + "GID-FILES:\n";
//			foreach (KeyValuePair<int, string> pair in map.gidToFiles) {
//				s += "  " + "  " + pair.Key + "=" + pair.Value;
//				if(map.gidToTileProperties.ContainsKey(pair.Key)){
//					s += " properties: ";
//					var enumerator = map.gidToTileProperties[pair.Key].GetEnumerator();
//					while (enumerator.MoveNext()) {
//						KeyValuePair<object, object> propPair = enumerator.Current;
//						s += propPair.Key + "=" + propPair.Value + " ";
//					}
//				}
//				s += "\n";
//			}
//			s +=	"  " + "Layers("+ map.layers.Length +")\n";
//			foreach (CCTMXLayer layer in map.layers) {
//				s += "  " + "  " + "Layer:" + layer.name + "\n";
//				if (layer.properties != null) {
//					s += "  " + "  " + "  " + "Properties: ";
//
//					var enumerator = layer.properties.GetEnumerator();
//					while (enumerator.MoveNext()) {
//						KeyValuePair<object, object> propPair = enumerator.Current;
//						s += propPair.Key + "=" + propPair.Value + " ";
//					}
//					s += "\n";
//				}
//				s += "  " + "  " + "  " + "Tiles:\n";
//				for(int row=0; row<layer.tiles.Length; row++){
//					s += "  " + "  " + "  " + " ";
//					for(int col=0; col<layer.tiles[row].Length; col++)
//					{
//						CCTMXTile tile = layer.tiles[row][col];
//						if(tile!=null)
//							s += layer.tiles[row][col].gid + " ";
//						else
//							s += "0" + " ";
//					}
//					s += "\n";
//				}
//			}
//			CCDebug.Log (s);
        }
Пример #2
0
        public static CCTMXMap Parse(string file, NSDictionary tilesetCaches = null)
        {
            string      text   = LoadText(file);
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(text);
            XmlNodeList nodeList = xmlDoc.DocumentElement.ChildNodes;

            // get mapWidth, mapHeight, tileWidth, tileHeight
            XmlNode mapNode = xmlDoc.DocumentElement;

            float version = float.Parse(mapNode.Attributes["version"].InnerText);

            if (!FloatUtils.EQ(version, 1.0f))
            {
                CCDebug.Warning("cocos2d:CCTMXMap: Found {0} tmx file, but only 1.0 version was tested.", version);
            }

            string dir = file.Replace(Path.GetFileName(file), "");

            CCTMXMap map = new CCTMXMap();

            map.fileName   = file;
            map.cols       = int.Parse(mapNode.Attributes["width"].InnerText);
            map.rows       = int.Parse(mapNode.Attributes["height"].InnerText);
            map.tileWidth  = int.Parse(mapNode.Attributes["tilewidth"].InnerText);
            map.tileHeight = int.Parse(mapNode.Attributes["tileheight"].InnerText);

            Dictionary <int, string> gidToFiles = new Dictionary <int, string> (256);
            Dictionary <int, Dictionary <string, string> > gidToTileProperties = new Dictionary <int, Dictionary <string, string> > (256);
            List <CCTMXLayer> layers = new List <CCTMXLayer> (8);


            var enumerator = nodeList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                XmlNode nodeData = (XmlNode)enumerator.Current;
                if (nodeData.Name == "tileset")
                {
                    ParseTileset(nodeData, gidToFiles, gidToTileProperties, dir, tilesetCaches);
                }
                else if (nodeData.Name == "layer")
                {
                    CCTMXLayer layer = ParseLayer(nodeData, map.cols, map.rows, gidToFiles, gidToTileProperties);
                    layers.Add(layer);
                }
                else if (nodeData.Name == "objectgroup")
                {
                    CCTMXLayer layer = ParseObjectGroup(nodeData, map.cols, map.rows, map.tileWidth, map.tileHeight, gidToFiles, gidToTileProperties);
                    layers.Add(layer);
                }
                else if (nodeData.Name == "properties")
                {
                    if (map.properties == null)
                    {
                        map.properties = new Dictionary <string, string>();
                    }
                    ParseProperties(nodeData, map.properties);
                }
            }
            map.gidToFiles          = gidToFiles;
            map.gidToTileProperties = gidToTileProperties;
            map.layers = layers.ToArray();
            return(map);
        }