void saveLevelToolStripMenuItem_Click(object sender, EventArgs e) { if (map == null) return; List<MapLayerData> mapLayerData = new List<MapLayerData>(); for (int i = 0; i < clbLayers.Items.Count; i++) { if (layers[i] is MapLayer) { MapLayerData data = new MapLayerData( clbLayers.Items[i].ToString(), ((MapLayer)layers[i]).Width, ((MapLayer)layers[i]).Height); for (int y = 0; y < ((MapLayer)layers[i]).Height; y++) for (int x = 0; x < ((MapLayer)layers[i]).Width; x++) data.SetTile(x, y, ((MapLayer)layers[i]).GetTile(x, y).TileIndex, ((MapLayer)layers[i]).GetTile(x, y).Tileset); mapLayerData.Add(data); } } MapData mapData = new MapData(levelData.MapName, tileSetData, mapLayerData); FolderBrowserDialog fbDialog = new FolderBrowserDialog(); fbDialog.Description = "Select Game Folder"; fbDialog.SelectedPath = Application.StartupPath; DialogResult result = fbDialog.ShowDialog(); if (result == DialogResult.OK) { if (!File.Exists(fbDialog.SelectedPath + @"\Game.xml")) { MessageBox.Show("Game not found", "Error"); return; } string LevelPath = Path.Combine(fbDialog.SelectedPath, @"Levels\"); string MapPath = Path.Combine(LevelPath, @"Maps\"); if (!Directory.Exists(LevelPath)) Directory.CreateDirectory(LevelPath); if (!Directory.Exists(MapPath)) Directory.CreateDirectory(MapPath); XnaSerializer.Serialize<LevelData>(LevelPath + levelData.LevelName + ".xml", levelData); XnaSerializer.Serialize<MapData>(MapPath + mapData.MapName + ".xml", mapData); } }
void saveLayerToolStripMenuItem_Click(object sender, EventArgs e) { if (layers.Count == 0) return; if (layers[clbLayers.SelectedIndex] is MapLayer) { SaveFileDialog sfDialog = new SaveFileDialog(); sfDialog.Filter = "Map Layer Data (*.mldt)|*.mldt"; sfDialog.CheckPathExists = true; sfDialog.OverwritePrompt = true; sfDialog.ValidateNames = true; DialogResult result = sfDialog.ShowDialog(); if (result != DialogResult.OK) return; MapLayerData data = new MapLayerData( clbLayers.SelectedItem.ToString(), ((MapLayer)layers[clbLayers.SelectedIndex]).Width, ((MapLayer)layers[clbLayers.SelectedIndex]).Height); for (int y = 0; y < ((MapLayer)layers[clbLayers.SelectedIndex]).Height; y++) { for (int x = 0; x < ((MapLayer)layers[clbLayers.SelectedIndex]).Width; x++) { data.SetTile( x, y, ((MapLayer)layers[clbLayers.SelectedIndex]).GetTile(x, y).TileIndex, ((MapLayer)layers[clbLayers.SelectedIndex]).GetTile(x, y).Tileset); } } try { XnaSerializer.Serialize<MapLayerData>(sfDialog.FileName, data); } catch (Exception exc) { MessageBox.Show(exc.Message, "Error saving map layer data"); } } }