Exemplo n.º 1
0
 public CCTMXObjectGroup objectGroupNamed(string groupName)
 {
     if (this.m_pObjectGroups != null && this.m_pObjectGroups.Count > 0)
     {
         for (int i = 0; i < this.m_pObjectGroups.Count; i++)
         {
             CCTMXObjectGroup item = this.m_pObjectGroups[i];
             if (item != null && item.GroupName == groupName)
             {
                 return(item);
             }
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// return the TMXObjectGroup for the secific group
        /// </summary>
        public CCTMXObjectGroup objectGroupNamed(string groupName)
        {
            if (m_pObjectGroups != null && m_pObjectGroups.Count > 0)
            {
                for (int i = 0; i < m_pObjectGroups.Count; i++)
                {
                    CCTMXObjectGroup objectGroup = m_pObjectGroups[i];
                    if (objectGroup != null && objectGroup.GroupName == groupName)
                    {
                        return(objectGroup);
                    }
                }
            }

            // objectGroup not found
            return(null);
        }
Exemplo n.º 3
0
        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;
            }
        }
Exemplo n.º 4
0
        // 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 = float.Parse(attributeDict["width"]);
                sMapSize.height = float.Parse(attributeDict["height"]);
                pTMXMapInfo.MapSize = sMapSize;

                CCSize sTileSize = new CCSize();
                sTileSize.width = float.Parse(attributeDict["tilewidth"]);
                sTileSize.height = float.Parse(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 = int.Parse(attributeDict["firstgid"]);

                    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 = new CCSize();
                    s.width = float.Parse(attributeDict["tilewidth"]);
                    s.height = float.Parse(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 + int.Parse(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 = float.Parse(attributeDict["width"]);
                s.height = float.Parse(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 * float.Parse(opacity));
                }
                else
                {
                    layer.m_cOpacity = 255;
                }

                float x = attributeDict.Keys.Contains("x") ? float.Parse(attributeDict["x"]) : 0;
                float y = attributeDict.Keys.Contains("y") ? float.Parse(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 = float.Parse(attributeDict["x"]) * pTMXMapInfo.TileSize.width;
                if (attributeDict.ContainsKey("y"))
                    positionOffset.y = float.Parse(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 = int.Parse(attributeDict["x"]) + (int)objectGroup.PositionOffset.x;
                key = "x";
                value = x.ToString();
                dict.Add(key, value);

                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);
                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;
            }
        }
Exemplo n.º 5
0
        // 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;
            }
        }
Exemplo n.º 6
0
        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.CCTMXOrientationOrtho);
                else if (orientationStr == "isometric")
                    pTMXMapInfo.Orientation = (int) (CCTMXOrientation.CCTMXOrientationIso);
                else if (orientationStr == "hexagonal")
                    pTMXMapInfo.Orientation = (int) (CCTMXOrientation.CCTMXOrientationHex);
                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) 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
                {
                    var tileset = new CCTMXTilesetInfo();

                    tileset.m_sName = attributeDict["name"];
                    tileset.m_uFirstGid = uint.Parse(attributeDict["firstgid"]);

                    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) TMXProperty.TMXPropertyTile;
            }
            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) TMXProperty.TMXPropertyLayer;
            }
            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) 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")
            {
                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) 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);
                }
            }
            else if (elementName == "polygon")
            {
                // find parent object's dict and add polygon-points to it
                // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject();
                // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();
                // TODO: dict->setObject(attributeDict objectForKey:@"points"] forKey:@"polygonPoints"];
            }
            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"];
            }

            if (attributeDict != null)
            {
                attributeDict = null;
            }
        }