Пример #1
0
        void ParseTilesetElement()
        {
            string externalTilesetFilename = null;

            // Tileset source
            if (currentAttributeDict.TryGetValue(TilesetElementFileSource, out externalTilesetFilename))
            {
                externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename,
                                                                               TileMapFileName);

                currentFirstGID = short.Parse(currentAttributeDict[TilesetElementFirstGid]);

                ParseXmlFile(externalTilesetFilename);
            }
            else
            {
                var tileset = new CCTileSetInfo();

                tileset.Name = currentAttributeDict[TilesetElementName];

                // First GID
                if (currentFirstGID == 0)
                {
                    tileset.FirstGid = short.Parse(currentAttributeDict[TilesetElementFirstGid]);
                }
                else
                {
                    tileset.FirstGid = currentFirstGID;
                    currentFirstGID  = 0;
                }

                string tileSpacingStr = null;
                string borderSizeStr  = null;

                // Tilesheet tile spacing
                if (currentAttributeDict.TryGetValue(TilesetElementTileSpacing, out tileSpacingStr))
                {
                    tileset.TileSpacing = int.Parse(tileSpacingStr);
                }

                // Tilesheet border width
                if (currentAttributeDict.TryGetValue(TilesetElementBorderSize, out borderSizeStr))
                {
                    tileset.BorderWidth = int.Parse(borderSizeStr);
                }

                // Tile texel size
                CCSize tileTexelSize;
                tileTexelSize.Width   = CCUtils.CCParseFloat(currentAttributeDict[TilesetElementTexelWidth]);
                tileTexelSize.Height  = CCUtils.CCParseFloat(currentAttributeDict[TilesetElementTexelHeight]);
                tileset.TileTexelSize = tileTexelSize;

                Tilesets.Add(tileset);
            }
        }
Пример #2
0
        internal static float DeSerializeFloat(StreamReader sr)
        {
            string s = sr.ReadLine();

            if (s == null)
            {
                CCLog.Log("DeSerializeFloat: null");
                return(0f);
            }
            return(CCUtils.CCParseFloat(s));
        }
Пример #3
0
        internal static CCPoint DeSerializePoint(StreamReader sr)
        {
            string x = sr.ReadLine();

            if (x == null)
            {
                CCLog.Log("DeSerializePoint: null");
                return(CCPoint.Zero);
            }
            CCPoint pt = new CCPoint();

            string[] s = x.Split(' ');
            pt.X = CCUtils.CCParseFloat(s[0]);
            pt.Y = CCUtils.CCParseFloat(s[1]);
            return(pt);
        }
Пример #4
0
        internal static CCSize DeSerializeSize(StreamReader sr)
        {
            string x = sr.ReadLine();

            if (x == null)
            {
                CCLog.Log("DeSerializeSize: null");
                return(CCSize.Zero);
            }
            CCSize pt = new CCSize();

            string[] s = x.Split(' ');
            pt.Width  = CCUtils.CCParseFloat(s[0]);
            pt.Height = CCUtils.CCParseFloat(s[1]);
            return(pt);
        }
Пример #5
0
        void ParseLayerElement()
        {
            var layerInfo = new CCTileLayerInfo();

            layerInfo.Name = currentAttributeDict[LayerElementName];

            CCTileMapCoordinates layerSize;

            layerSize.Column          = (int)CCUtils.CCParseFloat(currentAttributeDict[LayerElementNumOfColumns]);
            layerSize.Row             = (int)CCUtils.CCParseFloat(currentAttributeDict[LayerElementNumOfRows]);
            layerInfo.LayerDimensions = layerSize;

            layerInfo.TileGIDAndFlags = new CCTileGidAndFlags[layerSize.Column * layerSize.Row];

            if (currentAttributeDict.ContainsKey(LayerElementVisible))
            {
                string visible = currentAttributeDict[LayerElementVisible];
                layerInfo.Visible = !(visible == "0");
            }
            else
            {
                layerInfo.Visible = true;
            }

            if (currentAttributeDict.ContainsKey(LayerElementOpacity))
            {
                string opacity = currentAttributeDict[LayerElementOpacity];
                layerInfo.Opacity = (byte)(byte.MaxValue * CCUtils.CCParseFloat(opacity));
            }
            else
            {
                layerInfo.Opacity = byte.MaxValue;
            }

            float x = currentAttributeDict.ContainsKey(LayerElementXOffset) ?
                      CCUtils.CCParseFloat(currentAttributeDict[LayerElementXOffset]) : 0;
            float y = currentAttributeDict.ContainsKey(LayerElementYOffset) ?
                      CCUtils.CCParseFloat(currentAttributeDict[LayerElementYOffset]) : 0;

            layerInfo.TileCoordOffset = new CCPoint(x, y);

            Layers.Add(layerInfo);

            currentParentElement = CCTileMapProperty.Layer;
        }
Пример #6
0
        public static CCSize CCSizeFromString(string content)
        {
            CCSize ret = new CCSize();

            do
            {
                List <string> strs = new List <string>();
                if (!CCUtils.SplitWithForm(content, strs))
                {
                    break;
                }

                float width  = CCUtils.CCParseFloat(strs[0]);
                float height = CCUtils.CCParseFloat(strs[1]);

                ret = new CCSize(width, height);
            } while (false);

            return(ret);
        }
Пример #7
0
        public static CCPoint CCPointFromString(string content)
        {
            CCPoint ret = CCPoint.Zero;

            do
            {
                List <string> strs = new List <string>();
                if (!CCUtils.SplitWithForm(content, strs))
                {
                    break;
                }

                float x = CCUtils.CCParseFloat(strs[0]);
                float y = CCUtils.CCParseFloat(strs[1]);

                ret.X = x;
                ret.Y = y;
            } while (false);

            return(ret);
        }
Пример #8
0
        void ParseObjectGroupElement()
        {
            var objectGroup = new CCTileMapObjectGroup();

            objectGroup.GroupName = currentAttributeDict[ObjectGrpElementName];

            CCPoint positionOffset = CCPoint.Zero;

            if (currentAttributeDict.ContainsKey(ObjectGrpElementXOffset))
            {
                positionOffset.X = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementXOffset]) * TileTexelSize.Width;
            }
            if (currentAttributeDict.ContainsKey(ObjectGrpElementYOffset))
            {
                positionOffset.Y = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementYOffset]) * TileTexelSize.Height;
            }
            objectGroup.PositionOffset = positionOffset;

            ObjectGroups.Add(objectGroup);

            currentParentElement = CCTileMapProperty.ObjectGroup;
        }
Пример #9
0
        void ParseMapElement()
        {
            // Version
            float version = CCUtils.CCParseFloat(currentAttributeDict[MapElementVersion]);

            if (version != 1.0f)
            {
                CCLog.Log("CocosSharp: CCTileMapInfo: Unsupported TMX version: {0}", version);
            }

            // Map type. Ortho, Iso, etc.
            string        mapTypeStr = currentAttributeDict[MapElementMapType];
            CCTileMapType mapType    = CCTileMapType.None;

            if (mapTypeKeys.TryGetValue(mapTypeStr, out mapType))
            {
                this.MapType = mapType;
            }
            else
            {
                CCLog.Log("CocosSharp: CCTileMapInfo: Unsupported TMX map type: {0}", mapTypeStr);
            }

            // Num of tile rows/columns in map
            CCTileMapCoordinates mapSize;

            mapSize.Column     = (int)CCUtils.CCParseFloat(currentAttributeDict[MapElementNumOfColumns]);
            mapSize.Row        = (int)CCUtils.CCParseFloat(currentAttributeDict[MapElementNumOfRows]);
            this.MapDimensions = mapSize;

            // Default tile texel dimensions
            CCSize tileTexelSize;

            tileTexelSize.Width  = CCUtils.CCParseFloat(currentAttributeDict[MapElementTileTexelWidth]);
            tileTexelSize.Height = CCUtils.CCParseFloat(currentAttributeDict[MapElementTileTexelHeight]);
            this.TileTexelSize   = tileTexelSize;

            this.currentParentElement = CCTileMapProperty.Map;
        }
Пример #10
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("CocosSharp: 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("CocosSharp: 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.ContainsKey("source"))
                {
                    string externalTilesetFilename = attributeDict["source"];

                    externalTilesetFilename = CCFileUtils.FullPathFromRelativeFile(externalTilesetFilename,
                                                                                   pTMXMapInfo.TMXFileName);

                    currentFirstGID = uint.Parse(attributeDict["firstgid"]);

                    pTMXMapInfo.ParseXmlFile(externalTilesetFilename);
                }
                else
                {
                    var tileset = new CCTMXTilesetInfo();

                    tileset.Name = attributeDict["name"];

                    if (currentFirstGID == 0)
                    {
                        tileset.FirstGid = uint.Parse(attributeDict["firstgid"]);
                    }
                    else
                    {
                        tileset.FirstGid = currentFirstGID;
                        currentFirstGID  = 0;
                    }

                    if (attributeDict.ContainsKey("spacing"))
                    {
                        tileset.Spacing = int.Parse(attributeDict["spacing"]);
                    }

                    if (attributeDict.ContainsKey("margin"))
                    {
                        tileset.Margin = int.Parse(attributeDict["margin"]);
                    }

                    CCSize s;
                    s.Width          = CCUtils.CCParseFloat(attributeDict["tilewidth"]);
                    s.Height         = CCUtils.CCParseFloat(attributeDict["tileheight"]);
                    tileset.TileSize = s;

                    pTMXMapInfo.Tilesets.Add(tileset);
                }
            }
            else if (elementName == "tile")
            {
                List <CCTMXTilesetInfo> tilesets = pTMXMapInfo.Tilesets;
                int tilesetCount      = tilesets != null ? tilesets.Count : 0;
                CCTMXTilesetInfo info = tilesetCount > 0 ? tilesets[tilesetCount - 1] : null;
                var dict = new Dictionary <string, string>();
                pTMXMapInfo.ParentGID = (info.FirstGid + 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.ContainsKey("visible"))
                {
                    string visible = attributeDict["visible"];
                    layer.Visible = !(visible == "0");
                }
                else
                {
                    layer.Visible = true;
                }

                if (attributeDict.ContainsKey("opacity"))
                {
                    string opacity = attributeDict["opacity"];
                    layer.Opacity = (byte)(255 * CCUtils.CCParseFloat(opacity));
                }
                else
                {
                    layer.Opacity = 255;
                }

                float x = attributeDict.ContainsKey("x") ? CCUtils.CCParseFloat(attributeDict["x"]) : 0;
                float y = attributeDict.ContainsKey("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")
            {
                List <CCTMXTilesetInfo> tilesets = pTMXMapInfo.Tilesets;
                int tilesetCount         = tilesets != null ? tilesets.Count : 0;
                CCTMXTilesetInfo tileset = tilesetCount > 0 ? tilesets[tilesetCount - 1] : null;

                // build full path
                string imagename = attributeDict["source"];
                tileset.SourceImage = 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")
            {
                List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups;
                int objectGroupCount         = objectGroups != null ? objectGroups.Count : 0;
                CCTMXObjectGroup objectGroup = objectGroupCount > 0 ? objectGroups[objectGroupCount - 1] : null;

                // 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
                    List <CCTMXLayerInfo> layers = pTMXMapInfo.Layers;
                    int            layersCount   = layers != null ? layers.Count : 0;
                    CCTMXLayerInfo layer         = layersCount > 0 ? layers[layersCount - 1] : null;

                    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
                    List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups;
                    int objGroupsCount           = objectGroups != null ? objectGroups.Count : 0;
                    CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? objectGroups[objGroupsCount - 1] : null;
                    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
                    List <CCTMXObjectGroup> objectGroups = pTMXMapInfo.ObjectGroups;
                    int objGroupsCount           = objectGroups != null ? objectGroups.Count : 0;
                    CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? objectGroups[objGroupsCount - 1] : null;

                    List <Dictionary <string, string> > objects = objectGroup.Objects;
                    int objCount = objects != null ? objects.Count : 0;
                    Dictionary <string, string> dict = objCount > 0 ? objects[objCount - 1] : null;

                    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
                int objGroupsCount           = ObjectGroups != null ? ObjectGroups.Count : 0;
                CCTMXObjectGroup objectGroup = objGroupsCount > 0 ? ObjectGroups[objGroupsCount - 1] : null;

                List <Dictionary <string, string> > objects = objectGroup.Objects;
                int objCount = objects != null ? objects.Count : 0;
                Dictionary <string, string> dict = objCount > 0 ? objects[objCount - 1] : null;

                // 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*)ObjectGroups->lastObject();
                // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject();
                // TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"];
            }
        }
Пример #11
0
        public static CCRect CCRectFromString(string rectSpec)
        {
            CCRect result = CCRect.Zero;

            do
            {
                if (rectSpec == null)
                {
                    break;
                }

                string content = rectSpec;

                // find the first '{' and the third '}'
                int nPosLeft  = content.IndexOf('{');
                int nPosRight = content.IndexOf('}');
                for (int i = 1; i < 3; ++i)
                {
                    if (nPosRight == -1)
                    {
                        break;
                    }
                    nPosRight = content.IndexOf('}', nPosRight + 1);
                }
                if (nPosLeft == -1 || nPosRight == -1)
                {
                    break;
                }
                content = content.Substring(nPosLeft + 1, nPosRight - nPosLeft - 1);
                int nPointEnd = content.IndexOf('}');
                if (nPointEnd == -1)
                {
                    break;
                }
                nPointEnd = content.IndexOf(',', nPointEnd);
                if (nPointEnd == -1)
                {
                    break;
                }

                // get the point string and size string
                string pointStr = content.Substring(0, nPointEnd);
                string sizeStr  = content.Substring(nPointEnd + 1);
                //, content.Length - nPointEnd
                // split the string with ','
                List <string> pointInfo = new List <string>();

                if (!CCUtils.SplitWithForm(pointStr, pointInfo))
                {
                    break;
                }
                List <string> sizeInfo = new List <string>();
                if (!CCUtils.SplitWithForm(sizeStr, sizeInfo))
                {
                    break;
                }

                float x      = CCUtils.CCParseFloat(pointInfo[0]);
                float y      = CCUtils.CCParseFloat(pointInfo[1]);
                float width  = CCUtils.CCParseFloat(sizeInfo[0]);
                float height = CCUtils.CCParseFloat(sizeInfo[1]);

                result = new CCRect(x, y, width, height);
            } while (false);

            return(result);
        }