Ejemplo n.º 1
0
        private static TileLayer GenerateTileLayer(XElement element, bool isInfinate, int tileWidth, int tileHeight)
        {
            string name        = XMLHelperFuncs.GetStringFromAttribute(element, "name");
            int    layerWidth  = XMLHelperFuncs.GetIntFromAttribute(element, "width");
            int    layerHeight = XMLHelperFuncs.GetIntFromAttribute(element, "height");

            // These attributes don't necisarially exist in the file.
            // So if we don't we set what it's default value 'should' be.
            bool  visible          = XMLHelperFuncs.GetBoolFromAttribute(element, "visible", true);
            bool  locked           = XMLHelperFuncs.GetBoolFromAttribute(element, "locked");
            float opacity          = XMLHelperFuncs.GetFloatFromAttribute(element, "opacity", 1.0f);
            float horizontalOffset = XMLHelperFuncs.GetFloatFromAttribute(element, "offsetx", 0.0f);
            float verticalOffset   = XMLHelperFuncs.GetFloatFromAttribute(element, "offsety", 0.0f);

            TileLayer layer;
            string    encodedData = "";


            DataEncoding dataEncoding = DataEncoding.CSV;

            // The encoding attribute only exists if data isn't in XML format
            if (XMLHelperFuncs.DoesAttributeExist(element.Element("data"), "encoding"))
            {
                string strDataEncoding = XMLHelperFuncs.GetStringFromAttribute(element.Element("data"), "encoding");

                switch (strDataEncoding)
                {
                case ("csv"):
                    dataEncoding = DataEncoding.CSV;
                    break;

                case ("base64"):
                    if (XMLHelperFuncs.DoesAttributeExist(element.Element("data"), "compression"))
                    {
                        if (XMLHelperFuncs.GetStringFromAttribute(element.Element("data"), "compression") == "gzip")
                        {
                            dataEncoding = DataEncoding.BASE64GZIP;
                        }
                        else if (XMLHelperFuncs.GetStringFromAttribute(element.Element("data"), "compression") == "zlib")
                        {
                            dataEncoding = DataEncoding.BASE64ZLIB;
                        }
                        else
                        {
                            //Todo: report error that compression is unknown
                        }
                    }
                    else
                    {
                        dataEncoding = DataEncoding.BASE64;
                    }
                    break;

                default:
                    //Todo: report error that encoding is unkown
                    break;
                }
            }
            else
            {
                dataEncoding = DataEncoding.XML;
            }

            layer = new TileLayer(name,
                                  layerWidth,
                                  layerHeight,
                                  tileWidth,
                                  tileHeight,
                                  encodedData,
                                  dataEncoding,
                                  visible,
                                  locked,
                                  opacity,
                                  horizontalOffset,
                                  verticalOffset);

            // Decode and add data

            if (!isInfinate)
            {
                if (dataEncoding != DataEncoding.XML)
                {
                    encodedData = XMLHelperFuncs.GetStringFromElement(element, "data");
                    layer.AddMapChunk <string>(encodedData, 0, 0, layerWidth, layerHeight, tileWidth, tileHeight, dataEncoding);
                }
                else
                {
                    encodedData = "";
                    XElement dataElement = XMLHelperFuncs.GetElement(element, "data");
                    layer.AddMapChunk(dataElement, 0, 0, layerWidth, layerHeight, tileWidth, tileHeight, dataEncoding);
                }
            }
            else

            {
                if (dataEncoding != DataEncoding.XML)
                {
                    foreach (var chunkElement in XMLHelperFuncs.GetElement(element, "data").Elements())
                    {
                        int    x               = XMLHelperFuncs.GetIntFromAttribute(chunkElement, "x");
                        int    y               = XMLHelperFuncs.GetIntFromAttribute(chunkElement, "y");
                        int    width           = XMLHelperFuncs.GetIntFromAttribute(chunkElement, "width");
                        int    height          = XMLHelperFuncs.GetIntFromAttribute(chunkElement, "height");
                        string tempEncodedData = chunkElement.Value.ToString();

                        layer.AddMapChunk(tempEncodedData, x, y, width, height, tileWidth, tileHeight, dataEncoding);
                    }
                }
                else
                {
                    encodedData = "";

                    //Todo
                }
            }



            return(layer);
        }