Ejemplo n.º 1
0
        public void LoadMap(string level)
        {
            IFileResourceService resourceService = serviceProvider.GetService <IFileResourceService>();
            IGraphicsService     graphicsService = serviceProvider.GetService <IGraphicsService>();

            map = new TmxMap(resourceService.GetFileName("Tilemap/" + level + ".tmx"));

            foreach (var tileset in map.Tilesets)
            {
                var imageName = tileset.Name;

                if (imageName == "Objects")
                {
                    continue;
                }

                // open stream and add the image stream to graphics
                using (var stream = resourceService.Open(tileset.Image.Source))
                {
                    graphicsService.AddImage(imageName, stream);
                }

                var width  = tileset.Columns.Value;
                var height = tileset.TileCount.Value / tileset.Columns.Value;

                for (int i = 0; i < tileset.TileCount.Value; i++)
                {
                    var gid      = tileset.FirstGid + i;
                    var idOffset = i % tileset.Columns.Value;
                    var x        = idOffset * (1f / width);
                    var row      = (i - idOffset) / width;
                    var y        = (height - 1 - row) * (1f / height);

                    graphicsService.AddTile(gid, imageName, x, y, 1f / width, 1f / height);

                    if (tileset.Tiles.ContainsKey(i))
                    {
                        var tile = tileset.Tiles[i];
                        if (tile.AnimationFrames != null && tile.AnimationFrames.Count > 0)
                        {
                            if (gidTiles.ContainsKey(gid))
                            {
                                continue;
                            }
                            gidTiles.Add(gid, tile);
                        }
                    }
                }
            }
        }