public static TileLayer TileLayerFromXml(GameResourceManager gameResourceManager, XElement layerElement)
        {
            var name = layerElement.Attribute("name").Value;
            var mapSize = MathUtil.ParseSize(layerElement.Element("MapSize").Value);
            var tileSize = MathUtil.ParseSize(layerElement.Element("TileSize").Value);
            var tileReferences = GetTileReferences(gameResourceManager, layerElement.Element("TileDefinitionReferences")).ToList();
            var tiles = GetTileRowsFromXml(layerElement.Element("Tiles"));

            var layer = new TileLayer(name, mapSize, tileSize);
            XmlRepository.BaseFromXml(layer, layerElement);

            int x = 0;
            foreach (var row in tiles)
            {
                int y = 0;
                foreach (var element in row)
                {
                    layer[x, y++] = tileReferences.Single(r => r.Id == element).Definition;
                }

                x++;
            }

            return layer;
        }
        private static IEnumerable<TileReference> CreateTileReferences(TileLayer tileLayer)
        {
            var tileDefinitions = new List<TileDefinition>();
            for (var i = 0; i < tileLayer.MapSize.Width; i++)
                for (var j = 0; j < tileLayer.MapSize.Height; j++)
                {
                    tileDefinitions.Add(tileLayer[i, j]);
                }

            return tileDefinitions
                .Distinct()
                .Select((x, i) => new TileReference { Id = i, Definition = x });
        }
        public static XElement ToXml(TileLayer tileLayer)
        {
            var tileReferences = CreateTileReferences(tileLayer).ToList();

            return new XElement("TileLayer",
                XmlRepository.LayerBaseToXml(tileLayer),
                new XElement("MapSize", tileLayer.MapSize),
                new XElement("TileSize", tileLayer.TileSize),
                new XElement("TileDefinitionReferences", tileReferences.Select(x =>
                    new XElement("Reference",
                        new XAttribute("id", x.Id),
                        new XAttribute("sheetName", x.Definition.SheetName),
                        new XAttribute("name", x.Definition.Name)))),
                new XElement("Tiles", GetRowsXml(tileLayer, tileReferences)));
        }
        private static IEnumerable<XElement> GetRowsXml(TileLayer tileLayer, List<TileReference> tileReferences)
        {
            for (var i = 0; i < tileLayer.MapSize.Width; i++)
            {
                var row = new int[tileLayer.MapSize.Height];
                for (var j = 0; j < tileLayer.MapSize.Height; j++)
                {
                    row[j] = tileReferences.Single(x => x.Definition == tileLayer[i, j]).Id;
                }

                yield return new XElement("Row", string.Join(", ", row));
            }
        }
        private TileLayer CreateTileLayer()
        {
            var texture = this.ResourceManager.GetTexture(@"Sandbox\TileSheet");
            var sheet = new TileSheet(texture, "Background", new Size(16, 16));
            var red = sheet.CreateTileDefinition("red", new Point(0, 0));
            var green = sheet.CreateTileDefinition("green", new Point(16, 0));
            var yellow = sheet.CreateTileDefinition("yellow", new Point(32, 0));
            var purple = sheet.CreateTileDefinition("purple", new Point(0, 16));
            sheet.CreateTileDefinition("orange", new Point(16, 16));
            sheet.CreateTileDefinition("blue", new Point(32, 16));

            //sheet.Save(@"C:\Users\Pascal\Dev\DotNet\GitHub\XNAGameEngine2D\Color TileSheet.xml");

            this.ResourceManager.AddTileSheet(sheet);

            var tileLayer = new TileLayer("Tiles", new Size(32, 32), new Size(16, 16));
            tileLayer[0, 0] = purple;
            tileLayer[0, 1] = green;
            tileLayer[1, 0] = yellow;
            tileLayer[1, 1] = red;
            tileLayer[10, 10] = purple;
            tileLayer[4, 20] = green;

            tileLayer.ParallaxScrollingVector = new Vector(2.0f, 2.0f);
            tileLayer.Offset = new Vector(0, -20);

            return tileLayer;
        }