Example #1
0
 /// <summary>
 /// Loads a Map
 /// </summary>
 public static Tileset LoadTileset(string filename)
 {
     using (var s = File.OpenRead(filename))
     {
         return(Tileset.FromStream(s));
     }
 }
Example #2
0
        public void TestTileIndexing(string file)
        {
            using (var mapStream = File.OpenRead(file))
            {
                var map = Map.FromStream(mapStream);

                Tileset LoadTileset(ExternalTileset t)
                {
                    using (var stream = File.OpenRead(Path.Combine(Path.GetDirectoryName(file), t.source)))
                    {
                        return(Tileset.FromStream(stream));
                    }
                }

                foreach (var ts in map.Tilesets.OfType <ExternalTileset>())
                {
                    ts.LoadTileset(LoadTileset);
                }
                var q = from t in map.Tilesets
                        from i in Enumerable.Range(t.FirstGid, t.Rows * t.Columns)
                        select new
                {
                    index = i,
                    tile  = t[i]
                };
                CollectionAssert.AllItemsAreUnique(q.ToArray().Select(i => i.index).ToArray());
            }
        }
        static void LoadTilemap(string filename, out TmxLayer floor_tiles, out TmxLayer background_tiles, out TmxLayer behind_tiles, out TmxLayer play_tiles, out TmxLayer roof_tiles, out TmxMap map, out Tileset tileset)
        {
            map = new TmxMap("res/maps/game map.tmx");

            floor_tiles      = map.Layers[0];
            background_tiles = map.Layers[1];
            behind_tiles     = map.Layers[2];
            play_tiles       = map.Layers[3];
            roof_tiles       = map.Layers[4];

            using (var stream = File.OpenRead("res/tilesets/tileseta.tsx"))
                tileset = Tileset.FromStream(stream);
            //Console.WriteLine($"{tileset.Name}");
        }
Example #4
0
        public static Map Process(string imagePath, string tilesetPath, string output)
        {
            Tileset ts;
            var     dict = new Dictionary <byte[], int>(new ByteArrayComparer());

            using (var stream = File.OpenRead(tilesetPath))
            {
                ts = Tileset.FromStream(stream);
                if (ts.FirstGid == 0)
                {
                    ts.FirstGid = 1;
                }

                var imgPath = Path.Combine(Path.GetDirectoryName(tilesetPath), ts.ImagePath);
                using (var tsImg = new Bitmap(imgPath))
                {
                    for (int gid = ts.TileCount + ts.FirstGid - 1; gid >= ts.FirstGid; gid--)
                    {
                        dict[tsImg.GetTileHash(ts[gid].GetRectangle())] = gid;
                    }
                }
            }
            TileLayer layer;

            using (var img = new Bitmap(imagePath))
            {
                var tw = ts.TileWidth;
                var th = ts.TileHeight;

                var columns = img.Width / tw;
                var rows    = img.Height / th;
                layer = new TileLayer()
                {
                    Name      = "Layer0",
                    Encoding  = "csv",
                    LayerType = LayerType.tilelayer,
                    X         = 0,
                    Y         = 0,
                    Width     = columns,
                    Height    = rows,
                    Opacity   = 1,
                    Visible   = true,
                    Data      = new int[columns * rows]
                };


                for (int r = 0, i = 0; r < rows; r++)
                {
                    for (int c = 0; c < columns; c++, i++)
                    {
                        var rect = new Rectangle(c * tw, r * th, tw, th);
                        var hash = img.GetTileHash(rect);
                        layer.Data[i] = dict.TryGetValue(hash, out var gid) ? gid : 0;
                    }
                }
            }
            var source = tilesetPath.MakeRelativePath(output).Replace('\\', '/');
            var map    = new Map()
            {
                CellWidth   = ts.TileWidth,
                CellHeight  = ts.TileHeight,
                Width       = layer.Width,
                Height      = layer.Height,
                Orientation = Orientation.orthogonal,
                RenderOrder = RenderOrder.rightdown,
                Tilesets    = new ITileset[] { new ExternalTileset {
                                                   FirstGid = ts.FirstGid, source = source
                                               } },
                Layers = new BaseLayer[] { layer }
            };

            return(map);
        }