/*--------------------------------------------------------------------------------------------------*/ /*------------------------------------ Map Loader and Saver ----------------------------------------*/ /*--------------------------------------------------------------------------------------------------*/ void saveToJSONFile() // serializes into a json file and then stores it into a text file { saveToJSON mapSave = new saveToJSON(); mapSave.xSize = grid.GridCols; // sets x to be the amount of grid collumns mapSave.ySize = grid.GridRows; // sets y to be the amount of grid rows mapSave.imagePaths = grid.gridTileNames; string json = JsonConvert.SerializeObject(mapSave, Formatting.Indented); if (grid.fromLoaded == true) { System.IO.File.WriteAllText(grid.filePathLoadedFrom, json); } else { System.IO.File.WriteAllText("JSON/" + saveToJSON.fileName + ".txt", json); } }
void loadMap() // can load a saved json file back into the map { string filePathContents = null; OpenFileDialog opf = new OpenFileDialog(); if (opf.ShowDialog() == true) { filePathContents = File.ReadAllText(opf.FileName); } if (filePathContents == null) { return; } grid.fromLoaded = true; grid.filePathLoadedFrom = opf.FileName; saveToJSON json = JsonConvert.DeserializeObject <saveToJSON>(filePathContents); grid.GridCols = json.xSize; grid.GridRows = json.ySize; grid.gridTileNames = json.imagePaths; TileMap.Children.Clear(); TileMap.RowDefinitions.Clear(); TileMap.ColumnDefinitions.Clear(); gridCreator(true); for (int i = 0; i < grid.GridCols * grid.GridRows; i++) // assigns the tiles to be in the map { if (grid.gridTileNames[i] != null) { BitmapImage copy = new BitmapImage(); copy.BeginInit(); copy.UriSource = new Uri(grid.gridTileNames[i]); copy.EndInit(); ImageBrush b = new ImageBrush(copy); (TileMap.Children[i] as Label).Background = b; } } }