/// <summary> /// initializes a TMX Tiled Map with a TMX file /// </summary> public bool initWithTMXFile(string tmxFile) { Debug.Assert(tmxFile != null && tmxFile.Length > 0, "TMXTiledMap: tmx file should not bi nil"); contentSize = new CCSize(0, 0); CCTMXMapInfo mapInfo = CCTMXMapInfo.formatWithTMXFile(tmxFile); if (mapInfo == null) { return(false); } Debug.Assert(mapInfo.Tilesets.Count != 0, "TMXTiledMap: Map not found. Please check the filename."); 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) { if (null == m_pTMXLayers) { m_pTMXLayers = new Dictionary <string, CCTMXLayer>(); Debug.Assert(m_pTMXLayers != null, "Allocate memory failed!"); } for (int i = 0; i < layers.Count; i++) { CCTMXLayerInfo layerInfo = layers[i]; if (layerInfo != null && layerInfo.m_bVisible) { CCTMXLayer child = parseLayer(layerInfo, mapInfo); addChild(child, idx, idx); // record the CCTMXLayer object by it's name string layerName = child.LayerName; m_pTMXLayers.Add(layerName, child); // update content size with the max size CCSize childSize = child.contentSize; CCSize currentSize = this.contentSize; currentSize.width = Math.Max(currentSize.width, childSize.width); currentSize.height = Math.Max(currentSize.height, childSize.height); this.contentSize = currentSize; idx++; } } } return(true); }
private CCTMXTilesetInfo tilesetForLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCSize mTLayerSize = layerInfo.m_tLayerSize; List <CCTMXTilesetInfo> tilesets = mapInfo.Tilesets; if (tilesets != null && tilesets.Count > 0) { for (int i = 0; i < tilesets.Count; i++) { CCTMXTilesetInfo item = tilesets[i]; if (item != null) { for (int j = 0; (float)j < mTLayerSize.height; j++) { for (int k = 0; (float)k < mTLayerSize.width; k++) { int num = (int)((float)k + mTLayerSize.width * (float)j); int mPTiles = layerInfo.m_pTiles[num]; if (mPTiles != 0 && mPTiles >= item.m_uFirstGid) { mapInfo.Tilesets.Reverse(); return(item); } } } } } } CCLog.Log("cocos2d: Warning: TMX Layer '{0}' has no tiles", new object[] { layerInfo.m_sName }); return(null); }
private CCTMXLayer parseLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXTilesetInfo cCTMXTilesetInfo = this.tilesetForLayer(layerInfo, mapInfo); CCTMXLayer cCTMXLayer = CCTMXLayer.layerWithTilesetInfo(cCTMXTilesetInfo, layerInfo, mapInfo); layerInfo.m_bOwnTiles = false; cCTMXLayer.setupTiles(); return(cCTMXLayer); }
public void textHandler(object ctx, byte[] ch, int len) { CCTMXMapInfo cCTMXMapInfo = this; if (cCTMXMapInfo.StoringCharacters) { cCTMXMapInfo.CurrentString = ch; } }
public static CCTMXMapInfo formatWithTMXFile(string tmxFile) { CCTMXMapInfo cCTMXMapInfo = new CCTMXMapInfo(); if (cCTMXMapInfo.initWithTMXFile(tmxFile)) { return(cCTMXMapInfo); } return(null); }
/// <summary> /// creates a TMX Format with a tmx file /// </summary> public static CCTMXMapInfo formatWithTMXFile(string tmxFile) { CCTMXMapInfo pRet = new CCTMXMapInfo(); if (pRet.initWithTMXFile(tmxFile)) { return(pRet); } return(null); }
private CCTMXLayer parseLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXTilesetInfo tileset = tilesetForLayer(layerInfo, mapInfo); CCTMXLayer layer = CCTMXLayer.layerWithTilesetInfo(tileset, layerInfo, mapInfo); // tell the layerinfo to release the ownership of the tiles map. layerInfo.m_bOwnTiles = false; layer.setupTiles(); return(layer); }
public void textHandler(object ctx, byte[] ch, int len) { //CC_UNUSED_PARAM(ctx); CCTMXMapInfo pTMXMapInfo = this; //string pText = System.Text.UTF8Encoding.UTF8.GetString(ch, 0, len); if (pTMXMapInfo.StoringCharacters) { //string currentString = pTMXMapInfo.CurrentString; //currentString += pText; pTMXMapInfo.CurrentString = ch; } }
private CCTMXTilesetInfo tilesetForLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCSize size = layerInfo.m_tLayerSize; List <CCTMXTilesetInfo> tilesets = mapInfo.Tilesets; if (tilesets != null && tilesets.Count > 0) { for (int i = 0; i < tilesets.Count; i++) { CCTMXTilesetInfo tileset = tilesets[i]; if (tileset != null) { for (int y = 0; y < size.height; y++) { for (int x = 0; x < size.width; x++) { int pos = (int)(x + size.width * y); int gid = layerInfo.m_pTiles[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 assert will be thrown later if (gid >= tileset.m_uFirstGid) { mapInfo.Tilesets.Reverse(); return(tileset); } } } } } } } // If all the tiles are 0, return empty tileset CCLog.Log("cocos2d: Warning: TMX Layer '{0}' has no tiles", layerInfo.m_sName); return(null); }
public bool initWithTMXFile(string tmxFile) { this.contentSize = new CCSize(0f, 0f); CCTMXMapInfo cCTMXMapInfo = CCTMXMapInfo.formatWithTMXFile(tmxFile); if (cCTMXMapInfo == null) { return(false); } this.m_tMapSize = cCTMXMapInfo.MapSize; this.m_tTileSize = cCTMXMapInfo.TileSize; this.m_nMapOrientation = cCTMXMapInfo.Orientation; this.ObjectGroups = cCTMXMapInfo.ObjectGroups; this.Properties = cCTMXMapInfo.Properties; this.m_pTileProperties = cCTMXMapInfo.TileProperties; int num = 0; List <CCTMXLayerInfo> layers = cCTMXMapInfo.Layers; if (layers != null && layers.Count > 0) { if (this.m_pTMXLayers == null) { this.m_pTMXLayers = new Dictionary <string, CCTMXLayer>(); } for (int i = 0; i < layers.Count; i++) { CCTMXLayerInfo item = layers[i]; if (item != null && item.m_bVisible) { CCTMXLayer cCTMXLayer = this.parseLayer(item, cCTMXMapInfo); this.addChild(cCTMXLayer, num, num); string layerName = cCTMXLayer.LayerName; this.m_pTMXLayers.Add(layerName, cCTMXLayer); CCSize cCSize = cCTMXLayer.contentSize; CCSize cCSize1 = this.contentSize; cCSize1.width = Math.Max(cCSize1.width, cCSize.width); cCSize1.height = Math.Max(cCSize1.height, cCSize.height); this.contentSize = cCSize1; num++; } } } return(true); }
/// <summary> /// creates a CCTMXLayer with an tileset info, a layer info and a map info /// </summary> public static CCTMXLayer layerWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXLayer pRet = new CCTMXLayer(); if (pRet.initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo)) { return pRet; } return null; }
public bool initWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCSize mTLayerSize = layerInfo.m_tLayerSize; float single = mTLayerSize.width * mTLayerSize.height; float single1 = single * 0.35f + 1f; CCTexture2D cCTexture2D = null; if (tilesetInfo != null) { cCTexture2D = CCTextureCache.sharedTextureCache().addImage(tilesetInfo.m_sSourceImage); } if (!base.initWithTexture(cCTexture2D, (int)single1)) { return(false); } this.m_sLayerName = layerInfo.m_sName; this.m_tLayerSize = layerInfo.m_tLayerSize; this.m_pTiles = layerInfo.m_pTiles; this.m_uMinGID = layerInfo.m_uMinGID; this.m_uMaxGID = layerInfo.m_uMaxGID; this.m_cOpacity = layerInfo.m_cOpacity; this.m_pProperties = layerInfo.Properties; this.m_fContentScaleFactor = CCDirector.sharedDirector().ContentScaleFactor; this.m_pTileSet = tilesetInfo; this.m_tMapTileSize = mapInfo.TileSize; this.m_uLayerOrientation = (CCTMXOrientatio)mapInfo.Orientation; this.position = this.calculateLayerOffset(layerInfo.m_tOffset); this.m_pAtlasIndexArray = ccCArray.ccCArrayNew((int)single); base.contentSizeInPixels = new CCSize(this.m_tLayerSize.width * this.m_tMapTileSize.width, this.m_tLayerSize.height * this.m_tMapTileSize.height); CCSize mTMapTileSize = this.m_tMapTileSize; mTMapTileSize.width = mTMapTileSize.width / this.m_fContentScaleFactor; CCSize mFContentScaleFactor = this.m_tMapTileSize; mFContentScaleFactor.height = mFContentScaleFactor.height / this.m_fContentScaleFactor; this.m_bUseAutomaticVertexZ = false; this.m_nVertexZvalue = 0; this.m_fAlphaFuncValue = 0f; return(true); }
public void startElement(object ctx, string name, string[] atts) { CCTMXMapInfo mUFirstGid = this; string str = name; Dictionary <string, string> strs = new Dictionary <string, string>(); if (atts != null && atts[0] != null) { for (int i = 0; i + 1 < (int)atts.Length; i = i + 2) { string str1 = atts[i]; strs.Add(str1, atts[i + 1]); } } if (str == "map") { string item = strs["version"]; if (item != "1.0") { CCLog.Log("cocos2d: TMXFormat: Unsupported TMX version: {0}", new object[] { item }); } string item1 = strs["orientation"]; if (item1 == "orthogonal") { mUFirstGid.Orientation = 0; } else if (item1 == "isometric") { mUFirstGid.Orientation = 2; } else if (item1 != "hexagonal") { object[] orientation = new object[] { mUFirstGid.Orientation }; CCLog.Log("cocos2d: TMXFomat: Unsupported orientation: {0}", orientation); } else { mUFirstGid.Orientation = 1; } CCSize cCSize = new CCSize() { width = ccUtils.ccParseFloat(strs["width"]), height = ccUtils.ccParseFloat(strs["height"]) }; mUFirstGid.MapSize = cCSize; CCSize cCSize1 = new CCSize() { width = ccUtils.ccParseFloat(strs["tilewidth"]), height = ccUtils.ccParseFloat(strs["tileheight"]) }; mUFirstGid.TileSize = cCSize1; mUFirstGid.ParentElement = 1; } else if (str == "tileset") { if (!strs.Keys.Contains <string>("source")) { CCTMXTilesetInfo cCTMXTilesetInfo = new CCTMXTilesetInfo() { m_sName = strs["name"], m_uFirstGid = ccUtils.ccParseInt(strs["firstgid"]) }; if (strs.Keys.Contains <string>("spacing")) { cCTMXTilesetInfo.m_uSpacing = ccUtils.ccParseInt(strs["spacing"]); } if (strs.Keys.Contains <string>("margin")) { cCTMXTilesetInfo.m_uMargin = ccUtils.ccParseInt(strs["margin"]); } CCSize cCSize2 = new CCSize() { width = ccUtils.ccParseFloat(strs["tilewidth"]), height = ccUtils.ccParseFloat(strs["tileheight"]) }; cCTMXTilesetInfo.m_tTileSize = cCSize2; mUFirstGid.Tilesets.Add(cCTMXTilesetInfo); } else { string item2 = strs["source"]; item2 = CCFileUtils.fullPathFromRelativeFile(item2, mUFirstGid.TMXFileName); mUFirstGid.parseXMLFile(item2); } } else if (str == "tile") { CCTMXTilesetInfo cCTMXTilesetInfo1 = mUFirstGid.Tilesets.LastOrDefault <CCTMXTilesetInfo>(); Dictionary <string, string> strs1 = new Dictionary <string, string>(); mUFirstGid.ParentGID = cCTMXTilesetInfo1.m_uFirstGid + ccUtils.ccParseInt(strs["id"]); mUFirstGid.TileProperties.Add(mUFirstGid.ParentGID, strs1); mUFirstGid.ParentElement = 5; } else if (str == "layer") { CCTMXLayerInfo cCTMXLayerInfo = new CCTMXLayerInfo() { m_sName = strs["name"] }; CCSize cCSize3 = new CCSize() { width = ccUtils.ccParseFloat(strs["width"]), height = ccUtils.ccParseFloat(strs["height"]) }; cCTMXLayerInfo.m_tLayerSize = cCSize3; cCTMXLayerInfo.m_pTiles = new int[(int)cCSize3.width * (int)cCSize3.height]; if (!strs.Keys.Contains <string>("visible")) { cCTMXLayerInfo.m_bVisible = true; } else { cCTMXLayerInfo.m_bVisible = !(strs["visible"] == "0"); } if (!strs.Keys.Contains <string>("opacity")) { cCTMXLayerInfo.m_cOpacity = 255; } else { string str2 = strs["opacity"]; cCTMXLayerInfo.m_cOpacity = (byte)(255f * ccUtils.ccParseFloat(str2)); } float single = (strs.Keys.Contains <string>("x") ? ccUtils.ccParseFloat(strs["x"]) : 0f); cCTMXLayerInfo.m_tOffset = new CCPoint(single, (strs.Keys.Contains <string>("y") ? ccUtils.ccParseFloat(strs["y"]) : 0f)); mUFirstGid.Layers.Add(cCTMXLayerInfo); mUFirstGid.ParentElement = 2; } else if (str == "objectgroup") { CCTMXObjectGroup cCTMXObjectGroup = new CCTMXObjectGroup() { GroupName = strs["name"] }; CCPoint cCPoint = new CCPoint(); if (strs.ContainsKey("x")) { cCPoint.x = ccUtils.ccParseFloat(strs["x"]) * mUFirstGid.TileSize.width; } if (strs.ContainsKey("y")) { cCPoint.y = ccUtils.ccParseFloat(strs["y"]) * mUFirstGid.TileSize.height; } cCTMXObjectGroup.PositionOffset = cCPoint; mUFirstGid.ObjectGroups.Add(cCTMXObjectGroup); mUFirstGid.ParentElement = 3; } else if (str == "image") { CCTMXTilesetInfo cCTMXTilesetInfo2 = mUFirstGid.Tilesets.LastOrDefault <CCTMXTilesetInfo>(); string item3 = strs["source"]; cCTMXTilesetInfo2.m_sSourceImage = CCFileUtils.fullPathFromRelativeFile(item3, mUFirstGid.TMXFileName); } else if (str == "data") { string str3 = (strs.ContainsKey("encoding") ? strs["encoding"] : ""); string str4 = (strs.ContainsKey("compression") ? strs["compression"] : ""); if (str3 == "base64") { mUFirstGid.LayerAttribs = mUFirstGid.LayerAttribs | 2; mUFirstGid.StoringCharacters = true; if (str4 == "gzip") { mUFirstGid.LayerAttribs = mUFirstGid.LayerAttribs | 4; } else if (str4 == "zlib") { mUFirstGid.LayerAttribs = mUFirstGid.LayerAttribs | 8; } } } else if (str == "object") { CCTMXObjectGroup cCTMXObjectGroup1 = mUFirstGid.ObjectGroups.LastOrDefault <CCTMXObjectGroup>(); Dictionary <string, string> strs2 = new Dictionary <string, string>(); string str5 = "name"; strs2.Add(str5, (strs.ContainsKey("name") ? strs["name"] : "")); str5 = "type"; strs2.Add(str5, (strs.ContainsKey("type") ? strs["type"] : "")); int num = ccUtils.ccParseInt(strs["x"]) + (int)cCTMXObjectGroup1.PositionOffset.x; str5 = "x"; strs2.Add(str5, num.ToString()); int mapSize = ccUtils.ccParseInt(strs["y"]) + (int)cCTMXObjectGroup1.PositionOffset.y; mapSize = (int)(mUFirstGid.MapSize.height * mUFirstGid.TileSize.height) - mapSize - (strs.ContainsKey("height") ? ccUtils.ccParseInt(strs["height"]) : 0); str5 = "y"; strs2.Add(str5, mapSize.ToString()); str5 = "width"; strs2.Add(str5, (strs.ContainsKey("width") ? strs["width"] : "")); str5 = "height"; strs2.Add(str5, (strs.ContainsKey("height") ? strs["height"] : "")); cCTMXObjectGroup1.Objects.Add(strs2); mUFirstGid.ParentElement = 4; } else if (str == "property") { if (mUFirstGid.ParentElement == 0) { object[] objArray = new object[] { strs["name"], strs["value"] }; CCLog.Log("TMX tile map: Parent element is unsupported. Cannot add property named '{0}' with value '{1}'", objArray); } else if (mUFirstGid.ParentElement == 1) { string item4 = strs["value"]; string item5 = strs["name"]; mUFirstGid.Properties.Add(item5, item4); } else if (mUFirstGid.ParentElement == 2) { CCTMXLayerInfo cCTMXLayerInfo1 = mUFirstGid.Layers.LastOrDefault <CCTMXLayerInfo>(); string item6 = strs["value"]; string str6 = strs["name"]; cCTMXLayerInfo1.Properties.Add(str6, item6); } else if (mUFirstGid.ParentElement == 3) { CCTMXObjectGroup cCTMXObjectGroup2 = mUFirstGid.ObjectGroups.LastOrDefault <CCTMXObjectGroup>(); string item7 = strs["value"]; string str7 = strs["name"]; cCTMXObjectGroup2.Properties.Add(str7, item7); } else if (mUFirstGid.ParentElement == 4) { CCTMXObjectGroup cCTMXObjectGroup3 = mUFirstGid.ObjectGroups.LastOrDefault <CCTMXObjectGroup>(); Dictionary <string, string> strs3 = cCTMXObjectGroup3.Objects.LastOrDefault <Dictionary <string, string> >(); strs3.Add(strs["name"], strs["value"]); } else if (mUFirstGid.ParentElement == 5) { Dictionary <string, string> strs4 = mUFirstGid.TileProperties[mUFirstGid.ParentGID]; strs4.Add(strs["name"], strs["value"]); } } if (strs != null) { strs = null; } }
/// <summary> /// initializes a TMX Tiled Map with a TMX file /// </summary> public bool InitWithTmxFile(string tmxFile) { Debug.Assert(!String.IsNullOrEmpty(tmxFile), "TMXTiledMap: tmx file should not bi nil"); ContentSize = CCSize.Zero; CCTMXMapInfo mapInfo = new CCTMXMapInfo(tmxFile); if (mapInfo == null) { return false; } Debug.Assert(mapInfo.Tilesets.Count != 0, "TMXTiledMap: Map not found. Please check the filename."); BuildWithMapInfo(mapInfo); return true; }
public void endElement(object ctx, string elementName) { CCTMXMapInfo cCTMXMapInfo = this; byte[] currentString = cCTMXMapInfo.CurrentString; if (!(elementName == "data") || (cCTMXMapInfo.LayerAttribs & 2) == 0) { if (elementName == "map") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "layer") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "objectgroup") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "object") { cCTMXMapInfo.ParentElement = 0; } return; } cCTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo cCTMXLayerInfo = cCTMXMapInfo.Layers.LastOrDefault <CCTMXLayerInfo>(); if ((cCTMXMapInfo.LayerAttribs & 12) == 0) { for (int i = 0; i < (int)cCTMXLayerInfo.m_pTiles.Length; i++) { cCTMXLayerInfo.m_pTiles[i] = currentString[i * 4]; } } else { using (MemoryStream memoryStream = new MemoryStream(currentString, false)) { if ((cCTMXMapInfo.LayerAttribs & 4) != 0) { using (GZipInputStream gZipInputStream = new GZipInputStream(memoryStream)) { using (BinaryReader binaryReader = new BinaryReader(gZipInputStream)) { for (int j = 0; j < (int)cCTMXLayerInfo.m_pTiles.Length; j++) { cCTMXLayerInfo.m_pTiles[j] = binaryReader.ReadInt32(); } } } } if ((cCTMXMapInfo.LayerAttribs & 8) != 0) { using (ZInputStream zInputStream = new ZInputStream(memoryStream)) { for (int k = 0; k < (int)cCTMXLayerInfo.m_pTiles.Length; k++) { cCTMXLayerInfo.m_pTiles[k] = zInputStream.Read(); zInputStream.Read(); zInputStream.Read(); zInputStream.Read(); } } } } } cCTMXMapInfo.CurrentString = null; }
/// <summary> /// creates a TMX Format with a tmx file /// </summary> public static CCTMXMapInfo formatWithTMXFile(string tmxFile) { CCTMXMapInfo pRet = new CCTMXMapInfo(); if (pRet.initWithTMXFile(tmxFile)) { return pRet; } return null; }
// the XML parser calls here with all the elements public void startElement(object ctx, string name, string[] atts) { CCTMXMapInfo pTMXMapInfo = this; string elementName = name; Dictionary <string, string> 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)(CCTMXOrientatio.CCTMXOrientationOrtho); } else if (orientationStr == "isometric") { pTMXMapInfo.Orientation = (int)(CCTMXOrientatio.CCTMXOrientationIso); } else if (orientationStr == "hexagonal") { pTMXMapInfo.Orientation = (int)(CCTMXOrientatio.CCTMXOrientationHex); } else { CCLog.Log("cocos2d: TMXFomat: Unsupported orientation: {0}", pTMXMapInfo.Orientation); } CCSize sMapSize = new CCSize(); sMapSize.width = ccUtils.ccParseFloat(attributeDict["width"]); sMapSize.height = ccUtils.ccParseFloat(attributeDict["height"]); pTMXMapInfo.MapSize = sMapSize; CCSize sTileSize = new CCSize(); sTileSize.width = ccUtils.ccParseFloat(attributeDict["tilewidth"]); sTileSize.height = ccUtils.ccParseFloat(attributeDict["tileheight"]); pTMXMapInfo.TileSize = sTileSize; // The parent element is now "map" pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyMap; } 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); pTMXMapInfo.parseXMLFile(externalTilesetFilename); } else { CCTMXTilesetInfo tileset = new CCTMXTilesetInfo(); tileset.m_sName = attributeDict["name"]; tileset.m_uFirstGid = ccUtils.ccParseInt(attributeDict["firstgid"]); if (attributeDict.Keys.Contains("spacing")) { tileset.m_uSpacing = ccUtils.ccParseInt(attributeDict["spacing"]); } if (attributeDict.Keys.Contains("margin")) { tileset.m_uMargin = ccUtils.ccParseInt(attributeDict["margin"]); } CCSize s = new CCSize(); 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(); Dictionary <string, string> dict = new Dictionary <string, string>(); pTMXMapInfo.ParentGID = (info.m_uFirstGid + ccUtils.ccParseInt(attributeDict["id"])); pTMXMapInfo.TileProperties.Add(pTMXMapInfo.ParentGID, dict); pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyTile; } else if (elementName == "layer") { CCTMXLayerInfo layer = new CCTMXLayerInfo(); layer.m_sName = attributeDict["name"]; CCSize s = new CCSize(); s.width = ccUtils.ccParseFloat(attributeDict["width"]); s.height = ccUtils.ccParseFloat(attributeDict["height"]); layer.m_tLayerSize = s; layer.m_pTiles = new int[(int)s.width * (int)s.height]; if (attributeDict.Keys.Contains("visible")) { string visible = attributeDict["visible"]; layer.m_bVisible = !(visible == "0"); } else { layer.m_bVisible = true; } if (attributeDict.Keys.Contains("opacity")) { string opacity = attributeDict["opacity"]; layer.m_cOpacity = (byte)(255 * ccUtils.ccParseFloat(opacity)); } else { layer.m_cOpacity = 255; } float x = attributeDict.Keys.Contains("x") ? ccUtils.ccParseFloat(attributeDict["x"]) : 0; float y = attributeDict.Keys.Contains("y") ? ccUtils.ccParseFloat(attributeDict["y"]) : 0; layer.m_tOffset = new CCPoint(x, y); pTMXMapInfo.Layers.Add(layer); // The parent element is now "layer" pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyLayer; } else if (elementName == "objectgroup") { CCTMXObjectGroup objectGroup = new CCTMXObjectGroup(); objectGroup.GroupName = attributeDict["name"]; CCPoint positionOffset = new CCPoint(); 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)TMXProperty.TMXPropertyObjectGroup; } 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)TMXLayerAttrib.TMXLayerAttribBase64; pTMXMapInfo.StoringCharacters = true; if (compression == "gzip") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)TMXLayerAttrib.TMXLayerAttribGzip; } else if (compression == "zlib") { layerAttribs = pTMXMapInfo.LayerAttribs; pTMXMapInfo.LayerAttribs = layerAttribs | (int)TMXLayerAttrib.TMXLayerAttribZlib; } Debug.Assert(compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method"); } Debug.Assert(pTMXMapInfo.LayerAttribs != (int)TMXLayerAttrib.TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported"); } else if (elementName == "object") { char[] buffer = new char[32]; 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 Dictionary <string, string> dict = new Dictionary <string, string>(); // Set the name of the object to the value for "name" string key = "name"; string value = attributeDict.ContainsKey("name") ? attributeDict["name"] : ""; dict.Add(key, value); // Assign all the attributes as key/name pairs in the properties dictionary key = "type"; value = attributeDict.ContainsKey("type") ? attributeDict["type"] : ""; dict.Add(key, value); int x = ccUtils.ccParseInt(attributeDict["x"]) + (int)objectGroup.PositionOffset.x; key = "x"; value = x.ToString(); dict.Add(key, value); int y = ccUtils.ccParseInt(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") ? ccUtils.ccParseInt(attributeDict["height"]) : 0); key = "y"; value = y.ToString(); dict.Add(key, value); key = "width"; value = attributeDict.ContainsKey("width") ? attributeDict["width"] : ""; dict.Add(key, value); key = "height"; value = attributeDict.ContainsKey("height") ? attributeDict["height"] : ""; dict.Add(key, value); // Add the object to the objectGroup objectGroup.Objects.Add(dict); // The parent element is now "object" pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyObject; } else if (elementName == "property") { if (pTMXMapInfo.ParentElement == (int)TMXProperty.TMXPropertyNone) { 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)TMXProperty.TMXPropertyMap) { // The parent element is the map string value = attributeDict["value"]; string key = attributeDict["name"]; pTMXMapInfo.Properties.Add(key, value); } else if (pTMXMapInfo.ParentElement == (int)TMXProperty.TMXPropertyLayer) { // 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)TMXProperty.TMXPropertyObjectGroup) { // 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)TMXProperty.TMXPropertyObject) { // 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)TMXProperty.TMXPropertyTile) { Dictionary <string, string> dict = pTMXMapInfo.TileProperties[pTMXMapInfo.ParentGID]; string propertyName = attributeDict["name"]; string propertyValue = attributeDict["value"]; dict.Add(propertyName, propertyValue); } } if (attributeDict != null) { attributeDict = null; } }
private CCTMXLayer parseLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXTilesetInfo tileset = tilesetForLayer(layerInfo, mapInfo); CCTMXLayer layer = CCTMXLayer.layerWithTilesetInfo(tileset, layerInfo, mapInfo); // tell the layerinfo to release the ownership of the tiles map. layerInfo.m_bOwnTiles = false; layer.setupTiles(); return layer; }
/// <summary> /// initializes a CCTMXLayer with a tileset info, a layer info and a map info /// </summary> public bool initWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { // XXX: is 35% a good estimate ? CCSize size = layerInfo.m_tLayerSize; 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.m_sName; m_tLayerSize = layerInfo.m_tLayerSize; m_pTiles = layerInfo.m_pTiles; m_uMinGID = layerInfo.m_uMinGID; m_uMaxGID = layerInfo.m_uMaxGID; m_cOpacity = layerInfo.m_cOpacity; m_pProperties = layerInfo.Properties; // m_pProperties = CCStringToStringDictionary::dictionaryWithDictionary(layerInfo->getProperties()); m_fContentScaleFactor = CCDirector.sharedDirector().ContentScaleFactor; // tilesetInfo m_pTileSet = tilesetInfo; //CC_SAFE_RETAIN(m_pTileSet); // mapInfo m_tMapTileSize = mapInfo.TileSize; m_uLayerOrientation = (CCTMXOrientatio)mapInfo.Orientation; // offset (after layer orientation is set); CCPoint offset = this.calculateLayerOffset(layerInfo.m_tOffset); this.position = offset; m_pAtlasIndexArray = ccCArray.ccCArrayNew((int)totalNumberOfTiles); this.contentSizeInPixels = new CCSize(m_tLayerSize.width * m_tMapTileSize.width, m_tLayerSize.height * m_tMapTileSize.height); m_tMapTileSize.width /= m_fContentScaleFactor; m_tMapTileSize.height /= m_fContentScaleFactor; m_bUseAutomaticVertexZ = false; m_nVertexZvalue = 0; m_fAlphaFuncValue = 0; return(true); } return(false); }
/// <summary> /// creates a TMX Format with a tmx file /// </summary> public static CCTMXMapInfo Create(string tmxFile) { var pRet = new CCTMXMapInfo(); pRet.InitWithTmxFile(tmxFile); return pRet; }
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++; } } } }
/** 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; }
/// <summary> /// initializes a CCTMXLayer with a tileset info, a layer info and a map info /// </summary> public bool initWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { // XXX: is 35% a good estimate ? CCSize size = layerInfo.m_tLayerSize; 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.m_sName; m_tLayerSize = layerInfo.m_tLayerSize; m_pTiles = layerInfo.m_pTiles; m_uMinGID = layerInfo.m_uMinGID; m_uMaxGID = layerInfo.m_uMaxGID; m_cOpacity = layerInfo.m_cOpacity; m_pProperties = layerInfo.Properties; // m_pProperties = CCStringToStringDictionary::dictionaryWithDictionary(layerInfo->getProperties()); m_fContentScaleFactor = CCDirector.sharedDirector().ContentScaleFactor; // tilesetInfo m_pTileSet = tilesetInfo; //CC_SAFE_RETAIN(m_pTileSet); // mapInfo m_tMapTileSize = mapInfo.TileSize; m_uLayerOrientation = (CCTMXOrientatio)mapInfo.Orientation; // offset (after layer orientation is set); CCPoint offset = this.calculateLayerOffset(layerInfo.m_tOffset); this.position = offset; m_pAtlasIndexArray = ccCArray.ccCArrayNew((int)totalNumberOfTiles); this.contentSizeInPixels = new CCSize(m_tLayerSize.width * m_tMapTileSize.width, m_tLayerSize.height * m_tMapTileSize.height); m_tMapTileSize.width /= m_fContentScaleFactor; m_tMapTileSize.height /= m_fContentScaleFactor; m_bUseAutomaticVertexZ = false; m_nVertexZvalue = 0; m_fAlphaFuncValue = 0; return true; } return false; }
private CCTMXTilesetInfo tilesetForLayer(CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCSize size = layerInfo.m_tLayerSize; List<CCTMXTilesetInfo> tilesets = mapInfo.Tilesets; if (tilesets != null && tilesets.Count > 0) { for (int i = 0; i < tilesets.Count; i++) { CCTMXTilesetInfo tileset = tilesets[i]; if (tileset != null) { for (int y = 0; y < size.height; y++) { for (int x = 0; x < size.width; x++) { int pos = (int)(x + size.width * y); int gid = layerInfo.m_pTiles[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 assert will be thrown later if (gid >= tileset.m_uFirstGid) { mapInfo.Tilesets.Reverse(); return tileset; } } } } } } } // If all the tiles are 0, return empty tileset CCLog.Log("cocos2d: Warning: TMX Layer '{0}' has no tiles", layerInfo.m_sName); return null; }
/** creates a CCTMXLayer with an tileset info, a layer info and a map info */ public static CCTMXLayer Create(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { var pRet = new CCTMXLayer(); pRet.InitWithTilesetInfo(tilesetInfo, layerInfo, mapInfo); return pRet; }
public void endElement(object ctx, string elementName) { CCTMXMapInfo pTMXMapInfo = this; byte[] buffer = pTMXMapInfo.CurrentString; if (elementName == "data" && (pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribBase64) != 0) { pTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); if ((pTMXMapInfo.LayerAttribs & ((int)(TMXLayerAttrib.TMXLayerAttribGzip) | (int)TMXLayerAttrib.TMXLayerAttribZlib)) != 0) { using (MemoryStream ms = new MemoryStream(buffer, false)) { //gzip compress if ((pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribGzip) != 0) { using (GZipInputStream inStream = new GZipInputStream(ms)) { using (var br = new BinaryReader(inStream)) { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = br.ReadInt32(); } } } } //zlib if ((pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribZlib) != 0) { using (ZInputStream inZStream = new ZInputStream(ms)) { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = inZStream.Read(); inZStream.Read(); inZStream.Read(); inZStream.Read(); } } } } } else { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = buffer[i * 4]; } } pTMXMapInfo.CurrentString = null; } else if (elementName == "map") { // The map element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "layer") { // The layer element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "objectgroup") { // The objectgroup element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "object") { // The object element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } }
/// <summary> /// creates a CCTMXLayer with an tileset info, a layer info and a map info /// </summary> public static CCTMXLayer layerWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo) { CCTMXLayer pRet = new CCTMXLayer(); if (pRet.initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo)) { return(pRet); } return(null); }
/** 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); }