public static void Serialize(BigMap map, TileSet tileset, bool IncludeTileset, Stream destination, bool useCompression = true) { if (useCompression) { using (DeflateStream ds = new DeflateStream(destination, CompressionMode.Compress)) { DoSerialize(map, tileset, IncludeTileset, ds); } } else { DoSerialize(map, tileset, IncludeTileset, destination); } }
public static BigMapAndTileSet Deserialize(Stream input, TileSet currentTileSet = null, bool isCompressed = true) { if (isCompressed) { using (DeflateStream ds = new DeflateStream(input, CompressionMode.Decompress)) { return DoDeserialize(ds, currentTileSet); } } else { return DoDeserialize(input, currentTileSet); } }
private void changeMap(BigMap map, TileSet tileset = null) { TileSet temp = tileset ?? DefaultTileSet(); this.map = map; this.tileset = temp; palleteUserControl1.TileSet = temp; mapDisplayUserControl1.TileSet = temp; mapDisplayUserControl1.Map = map; Refresh(); }
private TileSet DefaultTileSet() { String imgPath = Path.Combine(Application.StartupPath, "Textures"); TileSet ts = new TileSet(); ts.AddTile(LoadTile(Path.Combine(imgPath, "grass4.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "grass1.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "weeds1.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "cobble2.JPG"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "brick1.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "rock1.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "sand2.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "sand3.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "dirt1.jpg"))); ts.AddTile(LoadTile(Path.Combine(imgPath, "leaves1.jpg"))); return ts; }
private static BigMapAndTileSet DoDeserialize(Stream input, TileSet currentTileSet) { SerializedMap serialized = Serializer.Deserialize<SerializedMap>(input); BigMapAndTileSet result = new BigMapAndTileSet(); BigMap map = new BigMap(); map.ClearLayers(); result.Map = map; result.TileSet = new TileSet(); List<Tile> tileList = new List<Tile>(); if (serialized.Tiles != null && serialized.Tiles.Count > 0) { result.TileSet.TileWidth = serialized.TileWidth; result.TileSet.TileHeight = serialized.TileHeight; foreach (byte[] image in serialized.Tiles) { Tile tile = new Tile(); tile.FullImage = LoadImage(image); tileList.Add(tile); result.TileSet.AddTile(tile); } } else if (currentTileSet != null) { result.TileSet = currentTileSet; foreach (Tile tile in currentTileSet.Tiles) { tileList.Add(tile); } } foreach (MapLayer layer in serialized.Layers) { RapidTestMapMaker.MapLayer newLayer = new RapidTestMapMaker.MapLayer(); newLayer.IsWall = layer.IsWall; map.AddLayer(newLayer); foreach (MapChunk chunk in layer.Chunks) { int l = chunk.X; int t = chunk.Y; int r = l + chunk.Width; int b = t + chunk.Height; int h = chunk.Height; IList<int> data = chunk.Data; for (int x = l; x < r; x++) { for (int y = t; y < b; y++) { int index = data[((x - l) * h) + (y - t)]; if (index != -1) { while (index >= tileList.Count) { Tile missingTile = new Tile(); tileList.Add(missingTile); result.TileSet.AddTile(missingTile); } newLayer[x, y] = tileList[index]; } } } } } return result; }
private static void DoSerialize(BigMap map, TileSet tileset, bool IncludeTileset, Stream destination) { SerializedMap result = new SerializedMap(); Dictionary<Tile, int> tileLookup = new Dictionary<Tile, int>(); if (tileset != null) { result.TileHeight = tileset.TileHeight; result.TileWidth = tileset.TileWidth; foreach (Tile tile in tileset.Tiles) { if (IncludeTileset) { result.Tiles.Add(SaveImage(tile.FullImage)); } tileLookup.Add(tile, tileLookup.Count); } } else { result.Tiles = null; } foreach (var layer in map.Layers) { Rectangle r = layer.ImageBounds; MapChunk chunk = new MapChunk(); chunk.X = r.Left; chunk.Y = r.Top; ; chunk.Width = r.Width; chunk.Height = r.Height; for (int x = r.Left; x < r.Right; x++) { for (int y = r.Top; y < r.Bottom; y++) { Tile tile = layer[x, y]; int index = -1; if (tile != null) { if (!tileLookup.TryGetValue(tile, out index)) { index = tileLookup.Count; tileLookup.Add(tile, index); } } chunk.Data.Add(index); } } MapLayer serializedLayer = new MapLayer(); serializedLayer.Visible = layer.Visible; serializedLayer.IsWall = layer.IsWall; serializedLayer.Chunks.Add(chunk); result.Layers.Add(serializedLayer); } Serializer.Serialize(destination, result); }