public void ReadMap() { if (reader == null) throw new NullReferenceException("BinaryReader null!"); string sizeRead = reader.ReadString(); string[] split = sizeRead.Split(new char[] { '=', 'x' }); int worldSize = ((Convert.ToInt32(split[1])) * (Convert.ToInt32(split[2]))); tilemap = new Tile[int.Parse(split[1]), int.Parse(split[2])]; int count = 0; while (count < worldSize) { string tileRead = reader.ReadString(); string[] split2 = tileRead.Split(new char[] { ';' }, 4); int x, y; bool isBg = bool.Parse(split2[3]); x = (int)Math.Floor((double)Int32.Parse(split2[1]) / 32); y = (int)Math.Floor((double)Int32.Parse(split2[2]) / 32); string tileDataName = split2[0]; Tile t = new Tile { Name = split2[0].Trim(), BackgroundTile = isBg, X = x * 32, Y = y * 32 }; if (y > tilemap.GetLength(0)) continue; if (x > tilemap.GetLength(1)) continue; tilemap[y, x] = t; count++; } reader.Close(); reader.Dispose(); }
public BinarySaveWriter(string path, Tile[,] tiles) { if (File.Exists(path)) { try { File.Delete(path); } catch(Exception ex) { Console.WriteLine("Error writing binary: " + ex.Message); } } writer = new BinaryWriter(File.Open(path, FileMode.CreateNew)); tilemap = tiles; }
public void ReadSave() { if (sr == null) throw new NullReferenceException("StreamReader null!"); string input = ""; while(!sr.EndOfStream) { input = sr.ReadLine(); if (input.Trim() != string.Empty) { string[] split = input.Split(new char[] { ';' }, 4); int x, y; bool isBg = bool.Parse(split[3]); x = (int)Math.Floor((double)Int32.Parse(split[1]) / 32); y = (int)Math.Floor((double)Int32.Parse(split[2]) / 32); string tileDataName = split[0]; Tile t = new Tile { Name = split[0].Trim(), BackgroundTile = isBg, X = x * 32, Y = y * 32}; tiles[y, x] = t; } } sr.Close(); }
/// <summary> /// Absolute x and y values are used here. /// Also sets the tile position. /// </summary> public void SetTile(int x, int y, Tile toReplace) { int tX = (int)Math.Floor((double)(x + 16) / 32); //add half of the cross hair so it's centered int tY = (int)Math.Floor((double)(y + 16) / 32); if (tX > WorldObj.ForegroundLayerTiles.GetLength(1) || tX < 0 || tY > WorldObj.ForegroundLayerTiles.GetLength(0) || tY < 0) return; if (WorldObj.ForegroundLayerTiles[tY, tX].Hardness == -1) return; else { if (toReplace.Type == TileType.Air) { if (WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName != null) { if (placeSoundSEI == null) { int soundIndex = ran.Next(1, 5); placeSoundSEI = MainGame.CustomContentManager.GetSoundEffect(string.Format(WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName, soundIndex)).CreateInstance(); placeSoundSEI.Play(); } else { //if (placeSoundSEI.State == SoundState.Paused) //{ int soundIndex = ran.Next(1, 5); placeSoundSEI = MainGame.CustomContentManager.GetSoundEffect(string.Format(WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName, soundIndex)).CreateInstance(); placeSoundSEI.Play(); //} } } WorldObj.ForegroundLayerTiles[tY, tX] = toReplace; } else { WorldObj.ForegroundLayerTiles[tY, tX] = toReplace; if (WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName != null) { if (placeSoundSEI == null) { int soundIndex = ran.Next(1, 5); placeSoundSEI = MainGame.CustomContentManager.GetSoundEffect(string.Format(WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName, soundIndex)).CreateInstance(); placeSoundSEI.Play(); } else { //if (placeSoundSEI.State == SoundState.Stopped) //{ int soundIndex = ran.Next(1, 5); placeSoundSEI = MainGame.CustomContentManager.GetSoundEffect(string.Format(WorldObj.ForegroundLayerTiles[tY, tX].PlaceSoundName, soundIndex)).CreateInstance(); placeSoundSEI.Play(); //} } } } WorldObj.ForegroundLayerTiles[tY, tX].Position = new Vector2(tX * 32, tY * 32); //if (tiles[tY, tX].Type == TileType.Air || tiles[tY, tX].IsBackground) // Lightmap[tY, tX] = 5; //else if (tiles[tY, tX].Light == TileType.Torch) // Lightmap[tY, tX] = 12; //else // Lightmap[tY, tX] = 0; WorldObj.Lightmap[tY, tX] = WorldObj.ForegroundLayerTiles[tY, tX].Light; if (WorldObj.ForegroundLayerTiles[tY, tX].IsBackground) { WorldObj.Lightmap[tY, tX] = 5; WorldObj.ForegroundLayerTiles[tY, tX].TransparencyOfTile = TileTransparency.FullyTransparent; } } }
static void WriteBinarySave(Tile[,] tileMap) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save binary save"; sfd.Filter = "Minecraft 2D Binary Saves (*.mc2dbin)|*.mc2dbin"; if(sfd.ShowDialog() == DialogResult.OK) { BinarySaveWriter bsw = new BinarySaveWriter(sfd.FileName, tileMap); bsw.WriteSave(); } mainLine(); }