Ejemplo n.º 1
0
        private static IEnumerable <TileMap> ReadLayers(XElement xMap, Dictionary <int, Tile> tileset)
        {
            List <TileMap> tileMaps = new List <TileMap>();

            foreach (var xLayer in xMap.Elements("layer"))
            {
                TileMap tileMap = new TileMap(XParser.ParseInt(xLayer, "width"), XParser.ParseInt(xLayer, "height"));

                var xData = xLayer.Element("data");
                if (xData != null)
                {
                    string csvData  = xData.Value;
                    int[]  tileData = csvData.Split(',').Select(s => int.Parse(s)).ToArray();

                    for (int y = 0; y < tileMap.GridHeight; y++)
                    {
                        for (int x = 0; x < tileMap.GridWidth; x++)
                        {
                            var tileID = tileData[(y * tileMap.GridWidth) + x];
                            var tile   = tileset[tileID];
                            tileMap[x, y] = tile;
                        }
                    }
                }

                tileMaps.Add(tileMap);
            }

            return(tileMaps);
        }
Ejemplo n.º 2
0
        private static Dictionary <int, Tile> ReadTileset(XElement xMap, ContentManager content)
        {
            Dictionary <int, Tile> tileset = new Dictionary <int, Tile>();

            //The '0' tile is empty.
            tileset.Add(0, Tile.Empty);

            //Parse each tileset.
            foreach (var xTileset in xMap.Elements("tileset"))
            {
                //Get tileset information.
                int tileCount = XParser.ParseInt(xTileset, "tilecount");
                int firstGID  = XParser.ParseInt(xTileset, "firstgid");
                int columns   = XParser.ParseInt(xTileset, "columns");

                //Get main image.
                var       xImage = xTileset.Element("image");
                Texture2D image  = (xImage != null) ? content.Load <Texture2D>(XParser.GetString(xImage, "source")) : null;

                if (image != null)
                {
                    foreach (var xTile in xTileset.Elements("tile"))
                    {
                        int id = XParser.ParseInt(xTile, "id");

                        Tile tile = new Tile();
                        tile.Texture = image;
                        tile.Type    = ParseTileType(xTile);
                        tile.UV      = new Point(id % columns, id / columns);

                        tileset.Add(id + firstGID, tile);
                    }
                }
                else
                {
                    foreach (var xTile in xTileset.Elements("tile"))
                    {
                        int id = XParser.ParseInt(xTile, "id");

                        var       xTileImage = xTile.Element("image");
                        Texture2D tileImage  = content.Load <Texture2D>(XParser.GetString(xTileImage, "source"));

                        Tile tile = new Tile();
                        tile.Texture = tileImage;
                        tile.Type    = ParseTileType(xTile);
                        tile.UV      = Point.Zero;

                        tileset.Add(id + firstGID, tile);
                    }
                }
            }

            return(tileset);
        }
Ejemplo n.º 3
0
        private static void ReadProperties(GameWorld map, XElement xMap, ContentManager content)
        {
            int width  = XParser.ParseInt(xMap, "width") * Tile.Width;
            int height = XParser.ParseInt(xMap, "height") * Tile.Height;

            var properties = xMap.Element("properties");

            if (properties != null)
            {
                var musicProperty = properties.Elements().FirstOrDefault(e => XParser.GetString(e, "name") == "music");
                if (musicProperty != null)
                {
                    map.Music = content.Load <Song>(XParser.GetString(musicProperty, "value"));
                }
            }
        }