/// <summary> /// Saves the open world as a file /// </summary> void SaveAsFile(string saveLocation) { using (var saveSettings = new FormSaveSettings(saveLocation, OpenWorld)) { if (saveSettings.ShowDialog() == DialogResult.OK) { try { // TODO: different file types var settings = OpenWorld.Settings; var save = new WorldFile() { Size = new Point(settings.WorldSize.Width, settings.WorldSize.Height), TileSize = new Point(settings.TileSize.Width, settings.TileSize.Height), Properties = OpenWorld.UserProperties, Tilesets = new Tileset[OpenWorld.Tilesets.Count], Layers = new RawLayer[OpenWorld.LayerCount - 1], CollisionData = new int[settings.WorldSize.Width * settings.WorldSize.Height] }; for (int a = 0; a < OpenWorld.Tilesets.Count; a++) { var tileset = OpenWorld.Tilesets[a]; var size = tileset.Bounds.Size.Nearest(settings.TileSize); save.Tilesets[a] = new Tileset() { File = tileset.ShouldBuild ? tileset.BuildLocation : null, Size = new Point(size.Width, size.Height) }; } // convert layers to raw layers for (int b = 0; b < OpenWorld.LayerCount - 1; b++) { var rawLayer = new RawLayer() { Name = OpenWorld.GetLayerName(b), Data = new int[settings.WorldSize.Width * settings.WorldSize.Height] }; for (int x = 0; x < settings.WorldSize.Width; x++) { for (int y = 0; y < settings.WorldSize.Height; y++) { WorldTile tile = OpenWorld.GetTile(x, y, b); int rawValue = (tile == null) ? 0 : tile.GetIndex(settings.TileSize, save.Tilesets[tile.Tileset].Size) + OpenWorld.TilesetIndexes[tile.Tileset] + 1; rawLayer.Data[y * settings.WorldSize.Width + x] = rawValue; } } save.Layers[b] = rawLayer; } for (int x = 0; x < settings.WorldSize.Width; x++) { for (int y = 0; y < settings.WorldSize.Height; y++) { WorldTile tile = OpenWorld.GetTile(x, y, OpenWorld.LayerCount - 1); int rawValue = (tile == null) ? 0 : tile.GetIndex(settings.TileSize, save.Tilesets[tile.Tileset].Size) + OpenWorld.TilesetIndexes[tile.Tileset] + 1; save.CollisionData[y * settings.WorldSize.Width + x] = rawValue; } } XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // serialize and save xml data to the save location using (XmlWriter writer = XmlWriter.Create(saveLocation, xmlSettings)) { IntermediateSerializer.Serialize(writer, save, null); } OpenWorld.SaveLocation = saveLocation; OpenWorld.ChangesMade = false; MessageBox.Show("The world has successfully saved.", "World Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { MessageBox.Show("Error saving world\n\n" + exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }