private void BuildWithMapInfo(CCTMXMapInfo mapInfo) { m_tMapSize = mapInfo.MapSize; m_tTileSize = mapInfo.TileSize; m_nMapOrientation = mapInfo.Orientation; ObjectGroups = mapInfo.ObjectGroups; Properties = mapInfo.Properties; m_pTileProperties = mapInfo.TileProperties; int idx = 0; //Layers List<CCTMXLayerInfo> layers = mapInfo.Layers; if (layers != null && layers.Count > 0) { for (int i = 0; i < layers.Count; i++) { CCTMXLayerInfo layerInfo = layers[i]; if (layerInfo != null && layerInfo.Visible) { CCTMXLayer child = ParseLayer(layerInfo, mapInfo); AddChild(child, idx, idx); // update content size with the max size CCSize childSize = child.ContentSize; CCSize currentSize = ContentSize; currentSize.Width = Math.Max(currentSize.Width, childSize.Width); currentSize.Height = Math.Max(currentSize.Height, childSize.Height); ContentSize = currentSize; idx++; } } } }
private CCTMXLayer ParseLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXTilesetInfo tileset = tilesetForLayer(layerInfo, mapInfo); CCTMXLayer layer = new CCTMXLayer(tileset, layerInfo, mapInfo); // tell the layerinfo to release the ownership of the tiles map. layerInfo.OwnTiles = false; layer.SetupTiles(); return layer; }
/** initializes a CCTMXLayer with a tileset info, a layer info and a map info */ public bool InitWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { // XXX: is 35% a good estimate ? CCSize size = layerInfo.LayerSize; float totalNumberOfTiles = size.Width * size.Height; float capacity = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ? CCTexture2D texture = null; if (tilesetInfo != null) { texture = CCTextureCache.SharedTextureCache.AddImage(tilesetInfo.m_sSourceImage); } if (base.InitWithTexture(texture, (int)capacity)) { // layerInfo m_sLayerName = layerInfo.Name; m_tLayerSize = size; m_pTiles = layerInfo.Tiles; m_uMinGID = layerInfo.MinGID; m_uMaxGID = layerInfo.MaxGID; m_cOpacity = layerInfo.Opacity; Properties = new Dictionary <string, string>(layerInfo.Properties); m_fContentScaleFactor = CCDirector.SharedDirector.ContentScaleFactor; // tilesetInfo m_pTileSet = tilesetInfo; // mapInfo m_tMapTileSize = mapInfo.TileSize; m_uLayerOrientation = (CCTMXOrientation)mapInfo.Orientation; // offset (after layer orientation is set); CCPoint offset = CalculateLayerOffset(layerInfo.Offset); Position = offset.PixelsToPoints(); m_pAtlasIndexArray = new List <int>((int)totalNumberOfTiles); var contentSize = new CCSize(m_tLayerSize.Width * m_tMapTileSize.Width, m_tLayerSize.Height * m_tMapTileSize.Height); ContentSize = contentSize.PixelsToPoints(); m_bUseAutomaticVertexZ = false; m_nVertexZvalue = 0; return(true); } return(false); }
private CCTMXTilesetInfo tilesetForLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCSize size = layerInfo.LayerSize; List<CCTMXTilesetInfo> tilesets = mapInfo.Tilesets; if (tilesets != null && tilesets.Count > 0) { for (int i = tilesets.Count - 1; i >= 0; i--) { CCTMXTilesetInfo tileset = tilesets[i]; if (tileset != null) { for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { var pos = (int) (x + size.Width * y); uint gid = layerInfo.Tiles[pos]; // gid are stored in little endian. // if host is big endian, then swap //if( o == CFByteOrderBigEndian ) // gid = CFSwapInt32( gid ); /* We support little endian.*/ // XXX: gid == 0 --> empty tile if (gid != 0) { // Optimization: quick return // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later if ((gid & CCTMXTileFlags.FlippedMask) >= tileset.m_uFirstGid) { return tileset; } } } } } } } // If all the tiles are 0, return empty tileset CCLog.Log("cocos2d: Warning: TMX Layer '{0}' has no tiles", layerInfo.Name); return null; }
public void EndElement(object ctx, string elementName) { CCTMXMapInfo pTMXMapInfo = this; byte[] encoded = null; if (elementName == "data" && (pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Base64) != 0) { pTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); if ((pTMXMapInfo.LayerAttribs & ((int)(CCTMXLayerAttrib.Gzip) | (int)CCTMXLayerAttrib.Zlib)) != 0) { //gzip compress if ((pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Gzip) != 0) { try { GZipStream inGZipStream = new GZipStream(new MemoryStream(pTMXMapInfo.CurrentString)); var outMemoryStream = new MemoryStream(); var buffer = new byte[1024]; while (true) { int bytesRead = inGZipStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } encoded = outMemoryStream.ToArray(); } catch (Exception ex) { CCLog.Log("failed to decompress embedded data object in TMX file."); CCLog.Log(ex.ToString()); } } //zlib if ((pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Zlib) != 0) { var inZInputStream = new ZInputStream(new MemoryStream(pTMXMapInfo.CurrentString)); var outMemoryStream = new MemoryStream(); var buffer = new byte[1024]; while (true) { int bytesRead = inZInputStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } encoded = outMemoryStream.ToArray(); } } else { encoded = pTMXMapInfo.CurrentString; } for (int i = 0; i < layer.Tiles.Length; i++) { int i4 = i * 4; var gid = (uint)( encoded[i4] | encoded[i4 + 1] << 8 | encoded[i4 + 2] << 16 | encoded[i4 + 3] << 24); layer.Tiles[i] = gid; } pTMXMapInfo.CurrentString = null; } else if (elementName == "map") { // The map element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "layer") { // The layer element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "objectgroup") { // The objectgroup element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "object") { // The object element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } }
public void StartElement(object ctx, string name, string[] atts) { CCTMXMapInfo pTMXMapInfo = this; string elementName = name; var attributeDict = new Dictionary <string, string>(); if (atts != null && atts[0] != null) { for (int i = 0; i + 1 < atts.Length; i += 2) { string key = atts[i]; string value = atts[i + 1]; attributeDict.Add(key, value); } } if (elementName == "map") { string version = attributeDict["version"]; if (version != "1.0") { CCLog.Log("cocos2d: TMXFormat: Unsupported TMX version: {0}", version); } string orientationStr = attributeDict["orientation"]; if (orientationStr == "orthogonal") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Ortho); } else if (orientationStr == "isometric") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Iso); } else if (orientationStr == "hexagonal") { pTMXMapInfo.Orientation = (int)(CCTMXOrientation.Hex); } else { CCLog.Log("cocos2d: TMXFomat: Unsupported orientation: {0}", pTMXMapInfo.Orientation); } CCSize sMapSize; sMapSize.Width = CCUtils.CCParseFloat(attributeDict["width"]); sMapSize.Height = CCUtils.CCParseFloat(attributeDict["height"]); pTMXMapInfo.MapSize = sMapSize; CCSize sTileSize; sTileSize.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); sTileSize.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); pTMXMapInfo.TileSize = sTileSize; // The parent element is now "map" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Map; } else if (elementName == "tileset") { // If this is an external tileset then start parsing that if (attributeDict.Keys.Contains("source")) { string externalTilesetFilename = attributeDict["source"]; externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename, pTMXMapInfo.TMXFileName); m_uCurrentFirstGID = uint.Parse(attributeDict["firstgid"]); pTMXMapInfo.ParseXmlFile(externalTilesetFilename); } else { var tileset = new CCTMXTilesetInfo(); tileset.m_sName = attributeDict["name"]; if (m_uCurrentFirstGID == 0) { tileset.m_uFirstGid = uint.Parse(attributeDict["firstgid"]); } else { tileset.m_uFirstGid = m_uCurrentFirstGID; m_uCurrentFirstGID = 0; } if (attributeDict.Keys.Contains("spacing")) { tileset.m_uSpacing = int.Parse(attributeDict["spacing"]); } if (attributeDict.Keys.Contains("margin")) { tileset.m_uMargin = int.Parse(attributeDict["margin"]); } CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); s.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); tileset.m_tTileSize = s; pTMXMapInfo.Tilesets.Add(tileset); } } else if (elementName == "tile") { CCTMXTilesetInfo info = pTMXMapInfo.Tilesets.LastOrDefault(); var dict = new Dictionary <string, string>(); pTMXMapInfo.ParentGID = (info.m_uFirstGid + uint.Parse(attributeDict["id"])); pTMXMapInfo.TileProperties.Add(pTMXMapInfo.ParentGID, dict); pTMXMapInfo.ParentElement = (int)CCTMXProperty.Tile; } else if (elementName == "layer") { var layer = new CCTMXLayerInfo(); layer.Name = attributeDict["name"]; CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["width"]); s.Height = CCUtils.CCParseFloat(attributeDict["height"]); layer.LayerSize = s; layer.Tiles = new uint[(int)s.Width * (int)s.Height]; if (attributeDict.Keys.Contains("visible")) { string visible = attributeDict["visible"]; layer.Visible = !(visible == "0"); } else { layer.Visible = true; } if (attributeDict.Keys.Contains("opacity")) { string opacity = attributeDict["opacity"]; layer.Opacity = (byte)(255 * CCUtils.CCParseFloat(opacity)); } else { layer.Opacity = 255; } float x = attributeDict.Keys.Contains("x") ? CCUtils.CCParseFloat(attributeDict["x"]) : 0; float y = attributeDict.Keys.Contains("y") ? CCUtils.CCParseFloat(attributeDict["y"]) : 0; layer.Offset = new CCPoint(x, y); pTMXMapInfo.Layers.Add(layer); // The parent element is now "layer" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Layer; } else if (elementName == "objectgroup") { var objectGroup = new CCTMXObjectGroup(); objectGroup.GroupName = attributeDict["name"]; CCPoint positionOffset = CCPoint.Zero; if (attributeDict.ContainsKey("x")) { positionOffset.X = CCUtils.CCParseFloat(attributeDict["x"]) * pTMXMapInfo.TileSize.Width; } if (attributeDict.ContainsKey("y")) { positionOffset.Y = CCUtils.CCParseFloat(attributeDict["y"]) * pTMXMapInfo.TileSize.Height; } objectGroup.PositionOffset = positionOffset; pTMXMapInfo.ObjectGroups.Add(objectGroup); // The parent element is now "objectgroup" pTMXMapInfo.ParentElement = (int)CCTMXProperty.ObjectGroup; } else if (elementName == "image") { CCTMXTilesetInfo tileset = pTMXMapInfo.Tilesets.LastOrDefault(); // build full path string imagename = attributeDict["source"]; tileset.m_sSourceImage = CCFileUtils.FullPathFromRelativeFile(imagename, pTMXMapInfo.TMXFileName); } else if (elementName == "data") { string encoding = attributeDict.ContainsKey("encoding") ? attributeDict["encoding"] : ""; string compression = attributeDict.ContainsKey("compression") ? attributeDict["compression"] : ""; if (encoding == "base64") { int layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Base64; pTMXMapInfo.StoringCharacters = true; if (compression == "gzip") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Gzip; } else if (compression == "zlib") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)CCTMXLayerAttrib.Zlib; } Debug.Assert(compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method"); } Debug.Assert(pTMXMapInfo.LayerAttribs != (int)CCTMXLayerAttrib.None, "TMX tile map: Only base64 and/or gzip/zlib maps are supported"); } else if (elementName == "object") { CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary <string, string>(); var pArray = new[] { "name", "type", "width", "height", "gid" }; for (int i = 0; i < pArray.Length; i++) { string key = pArray[i]; if (attributeDict.ContainsKey(key)) { dict.Add(key, attributeDict[key]); } } // But X and Y since they need special treatment // X int x = int.Parse(attributeDict["x"]) + (int)objectGroup.PositionOffset.X; dict.Add("x", x.ToString()); int y = int.Parse(attributeDict["y"]) + (int)objectGroup.PositionOffset.Y; // Correct y position. (Tiled uses Flipped, cocos2d uses Standard) y = (int)(pTMXMapInfo.MapSize.Height * pTMXMapInfo.TileSize.Height) - y - (attributeDict.ContainsKey("height") ? int.Parse(attributeDict["height"]) : 0); dict.Add("y", y.ToString()); // Add the object to the objectGroup objectGroup.Objects.Add(dict); // The parent element is now "object" pTMXMapInfo.ParentElement = (int)CCTMXProperty.Object; } else if (elementName == "property") { if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.None) { CCLog.Log("TMX tile map: Parent element is unsupported. Cannot add property named '{0}' with value '{1}'", attributeDict["name"], attributeDict["value"]); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Map) { // The parent element is the map string value = attributeDict["value"]; string key = attributeDict["name"]; pTMXMapInfo.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Layer) { // The parent element is the last layer CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); string value = attributeDict["value"]; string key = attributeDict["name"]; // Add the property to the layer layer.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.ObjectGroup) { // The parent element is the last object group CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); string value = attributeDict["value"]; string key = attributeDict["name"]; objectGroup.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Object) { // The parent element is the last object CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); Dictionary <string, string> dict = objectGroup.Objects.LastOrDefault(); string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } else if (pTMXMapInfo.ParentElement == (int)CCTMXProperty.Tile) { Dictionary <string, string> dict = pTMXMapInfo.TileProperties[pTMXMapInfo.ParentGID]; string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } } else if (elementName == "polygon") { // find parent object's dict and add polygon-points to it CCTMXObjectGroup objectGroup = m_pObjectGroups.LastOrDefault(); var dict = objectGroup.Objects.LastOrDefault(); // get points value string var value = attributeDict["points"]; if (!String.IsNullOrEmpty(value)) { var pPointsArray = new List <CCPoint>(); var pointPairs = value.Split(' '); foreach (var pontPair in pointPairs) { //TODO: Parse points //CCPoint point; //point.X = x + objectGroup.PositionOffset.X; //point.Y = y + objectGroup.PositionOffset.Y; //pPointsArray.Add(point); } //dict.Add("points", pPointsArray); } } else if (elementName == "polyline") { // find parent object's dict and add polyline-points to it // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject(); // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject(); // TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"]; } }
/** creates a CCTMXLayer with an tileset info, a layer info and a map info */ public CCTMXLayer(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { InitWithTilesetInfo(tilesetInfo, layerInfo, mapInfo); }
public void StartElement(object ctx, string name, string[] atts) { CCTMXMapInfo pTMXMapInfo = this; string elementName = name; var attributeDict = new Dictionary<string, string>(); if (atts != null && atts[0] != null) { for (int i = 0; i + 1 < atts.Length; i += 2) { string key = atts[i]; string value = atts[i + 1]; attributeDict.Add(key, value); } } if (elementName == "map") { string version = attributeDict["version"]; if (version != "1.0") { CCLog.Log("cocos2d: TMXFormat: Unsupported TMX version: {0}", version); } string orientationStr = attributeDict["orientation"]; if (orientationStr == "orthogonal") pTMXMapInfo.Orientation = (int) (CCTMXOrientation.Ortho); else if (orientationStr == "isometric") pTMXMapInfo.Orientation = (int) (CCTMXOrientation.Iso); else if (orientationStr == "hexagonal") pTMXMapInfo.Orientation = (int) (CCTMXOrientation.Hex); else CCLog.Log("cocos2d: TMXFomat: Unsupported orientation: {0}", pTMXMapInfo.Orientation); CCSize sMapSize; sMapSize.Width = CCUtils.CCParseFloat(attributeDict["width"]); sMapSize.Height = CCUtils.CCParseFloat(attributeDict["height"]); pTMXMapInfo.MapSize = sMapSize; CCSize sTileSize; sTileSize.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); sTileSize.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); pTMXMapInfo.TileSize = sTileSize; // The parent element is now "map" pTMXMapInfo.ParentElement = (int) CCTMXProperty.Map; } else if (elementName == "tileset") { // If this is an external tileset then start parsing that if (attributeDict.Keys.Contains("source")) { string externalTilesetFilename = attributeDict["source"]; externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename, pTMXMapInfo.TMXFileName); m_uCurrentFirstGID = uint.Parse(attributeDict["firstgid"]); pTMXMapInfo.ParseXmlFile(externalTilesetFilename); } else { var tileset = new CCTMXTilesetInfo(); tileset.m_sName = attributeDict["name"]; if (m_uCurrentFirstGID == 0) { tileset.m_uFirstGid = uint.Parse(attributeDict["firstgid"]); } else { tileset.m_uFirstGid = m_uCurrentFirstGID; m_uCurrentFirstGID = 0; } if (attributeDict.Keys.Contains("spacing")) tileset.m_uSpacing = int.Parse(attributeDict["spacing"]); if (attributeDict.Keys.Contains("margin")) tileset.m_uMargin = int.Parse(attributeDict["margin"]); CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["tilewidth"]); s.Height = CCUtils.CCParseFloat(attributeDict["tileheight"]); tileset.m_tTileSize = s; pTMXMapInfo.Tilesets.Add(tileset); } } else if (elementName == "tile") { CCTMXTilesetInfo info = pTMXMapInfo.Tilesets.LastOrDefault(); var dict = new Dictionary<string, string>(); pTMXMapInfo.ParentGID = (info.m_uFirstGid + uint.Parse(attributeDict["id"])); pTMXMapInfo.TileProperties.Add(pTMXMapInfo.ParentGID, dict); pTMXMapInfo.ParentElement = (int) CCTMXProperty.Tile; } else if (elementName == "layer") { var layer = new CCTMXLayerInfo(); layer.Name = attributeDict["name"]; CCSize s; s.Width = CCUtils.CCParseFloat(attributeDict["width"]); s.Height = CCUtils.CCParseFloat(attributeDict["height"]); layer.LayerSize = s; layer.Tiles = new uint[(int) s.Width * (int) s.Height]; if (attributeDict.Keys.Contains("visible")) { string visible = attributeDict["visible"]; layer.Visible = !(visible == "0"); } else { layer.Visible = true; } if (attributeDict.Keys.Contains("opacity")) { string opacity = attributeDict["opacity"]; layer.Opacity = (byte) (255 * CCUtils.CCParseFloat(opacity)); } else { layer.Opacity = 255; } float x = attributeDict.Keys.Contains("x") ? CCUtils.CCParseFloat(attributeDict["x"]) : 0; float y = attributeDict.Keys.Contains("y") ? CCUtils.CCParseFloat(attributeDict["y"]) : 0; layer.Offset = new CCPoint(x, y); pTMXMapInfo.Layers.Add(layer); // The parent element is now "layer" pTMXMapInfo.ParentElement = (int) CCTMXProperty.Layer; } else if (elementName == "objectgroup") { var objectGroup = new CCTMXObjectGroup(); objectGroup.GroupName = attributeDict["name"]; CCPoint positionOffset = CCPoint.Zero; if (attributeDict.ContainsKey("x")) positionOffset.X = CCUtils.CCParseFloat(attributeDict["x"]) * pTMXMapInfo.TileSize.Width; if (attributeDict.ContainsKey("y")) positionOffset.Y = CCUtils.CCParseFloat(attributeDict["y"]) * pTMXMapInfo.TileSize.Height; objectGroup.PositionOffset = positionOffset; pTMXMapInfo.ObjectGroups.Add(objectGroup); // The parent element is now "objectgroup" pTMXMapInfo.ParentElement = (int) CCTMXProperty.ObjectGroup; } else if (elementName == "image") { CCTMXTilesetInfo tileset = pTMXMapInfo.Tilesets.LastOrDefault(); // build full path string imagename = attributeDict["source"]; tileset.m_sSourceImage = CCFileUtils.FullPathFromRelativeFile(imagename, pTMXMapInfo.TMXFileName); } else if (elementName == "data") { string encoding = attributeDict.ContainsKey("encoding") ? attributeDict["encoding"] : ""; string compression = attributeDict.ContainsKey("compression") ? attributeDict["compression"] : ""; if (encoding == "base64") { int layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int) CCTMXLayerAttrib.Base64; pTMXMapInfo.StoringCharacters = true; if (compression == "gzip") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int) CCTMXLayerAttrib.Gzip; } else if (compression == "zlib") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int) CCTMXLayerAttrib.Zlib; } Debug.Assert(compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method"); } Debug.Assert(pTMXMapInfo.LayerAttribs != (int) CCTMXLayerAttrib.None, "TMX tile map: Only base64 and/or gzip/zlib maps are supported"); } else if (elementName == "object") { CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary<string, string>(); var pArray = new[] {"name", "type", "width", "height", "gid"}; for (int i = 0; i < pArray.Length; i++) { string key = pArray[i]; if (attributeDict.ContainsKey(key)) { dict.Add(key, attributeDict[key]); } } // But X and Y since they need special treatment // X int x = int.Parse(attributeDict["x"]) + (int) objectGroup.PositionOffset.X; dict.Add("x", x.ToString()); int y = int.Parse(attributeDict["y"]) + (int) objectGroup.PositionOffset.Y; // Correct y position. (Tiled uses Flipped, cocos2d uses Standard) y = (int) (pTMXMapInfo.MapSize.Height * pTMXMapInfo.TileSize.Height) - y - (attributeDict.ContainsKey("height") ? int.Parse(attributeDict["height"]) : 0); dict.Add("y", y.ToString()); // Add the object to the objectGroup objectGroup.Objects.Add(dict); // The parent element is now "object" pTMXMapInfo.ParentElement = (int) CCTMXProperty.Object; } else if (elementName == "property") { if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.None) { CCLog.Log("TMX tile map: Parent element is unsupported. Cannot add property named '{0}' with value '{1}'", attributeDict["name"], attributeDict["value"]); } else if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.Map) { // The parent element is the map string value = attributeDict["value"]; string key = attributeDict["name"]; pTMXMapInfo.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.Layer) { // The parent element is the last layer CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); string value = attributeDict["value"]; string key = attributeDict["name"]; // Add the property to the layer layer.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.ObjectGroup) { // The parent element is the last object group CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); string value = attributeDict["value"]; string key = attributeDict["name"]; objectGroup.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.Object) { // The parent element is the last object CCTMXObjectGroup objectGroup = pTMXMapInfo.ObjectGroups.LastOrDefault(); Dictionary<string, string> dict = objectGroup.Objects.LastOrDefault(); string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } else if (pTMXMapInfo.ParentElement == (int) CCTMXProperty.Tile) { Dictionary<string, string> dict = pTMXMapInfo.TileProperties[pTMXMapInfo.ParentGID]; string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } } else if (elementName == "polygon") { // find parent object's dict and add polygon-points to it CCTMXObjectGroup objectGroup = m_pObjectGroups.LastOrDefault(); var dict = objectGroup.Objects.LastOrDefault(); // get points value string var value = attributeDict["points"]; if (!String.IsNullOrEmpty(value)) { var pPointsArray = new List<CCPoint>(); var pointPairs = value.Split(' '); foreach (var pontPair in pointPairs) { //TODO: Parse points //CCPoint point; //point.X = x + objectGroup.PositionOffset.X; //point.Y = y + objectGroup.PositionOffset.Y; //pPointsArray.Add(point); } //dict.Add("points", pPointsArray); } } else if (elementName == "polyline") { // find parent object's dict and add polyline-points to it // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject(); // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject(); // TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"]; } }
/** initializes a CCTMXLayer with a tileset info, a layer info and a map info */ public bool InitWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { // XXX: is 35% a good estimate ? CCSize size = layerInfo.LayerSize; float totalNumberOfTiles = size.Width * size.Height; float capacity = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ? CCTexture2D texture = null; if (tilesetInfo != null) { texture = CCTextureCache.SharedTextureCache.AddImage(tilesetInfo.m_sSourceImage); } if (base.InitWithTexture(texture, (int) capacity)) { // layerInfo m_sLayerName = layerInfo.Name; m_tLayerSize = size; m_pTiles = layerInfo.Tiles; m_uMinGID = layerInfo.MinGID; m_uMaxGID = layerInfo.MaxGID; m_cOpacity = layerInfo.Opacity; Properties = new Dictionary<string, string>(layerInfo.Properties); m_fContentScaleFactor = CCDirector.SharedDirector.ContentScaleFactor; // tilesetInfo m_pTileSet = tilesetInfo; // mapInfo m_tMapTileSize = mapInfo.TileSize; m_uLayerOrientation = (CCTMXOrientation) mapInfo.Orientation; // offset (after layer orientation is set); CCPoint offset = CalculateLayerOffset(layerInfo.Offset); Position = CCMacros.CCPointPixelsToPoints(offset); m_pAtlasIndexArray = new List<int>((int) totalNumberOfTiles); ContentSize = CCMacros.CCSizePixelsToPoints(new CCSize(m_tLayerSize.Width * m_tMapTileSize.Width, m_tLayerSize.Height * m_tMapTileSize.Height)); m_bUseAutomaticVertexZ = false; m_nVertexZvalue = 0; return true; } return false; }
/** creates a CCTMXLayer with an tileset info, a layer info and a map info */ public CCTMXLayer (CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { InitWithTilesetInfo(tilesetInfo, layerInfo, mapInfo); }