private bool SaveWorld() { try { string SavePath = Directory.GetCurrentDirectory() + "\\Worlds"; bool OverWrite = true; lblEditor.Text = "Saving " + txtWorldName.Text + ".LoD..."; //Detect if doesnt exist if (!Directory.Exists(SavePath)) Directory.CreateDirectory(SavePath); //Are you sure you want to overwrite if (File.Exists(SavePath + "\\" + txtWorldName.Text + ".LoD")) { DialogResult dialogResult = MessageBox.Show("This world already exists\n\nDo you want to overwrite it?", "World Exists", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { OverWrite = true; SavePath = SavePath + "\\" + txtWorldName.Text + ".LoD"; } else if (dialogResult == DialogResult.No) { OverWrite = false; } } else SavePath = SavePath + "\\" + txtWorldName.Text + ".LoD"; if (OverWrite == true) { Thread.Sleep(500); DataTypes.WorldFile ThisWorld = new DataTypes.WorldFile(); ThisFloor.FloorName = txtFloorName.Text; world[FloorNum] = ThisFloor; ThisWorld.WorldName = txtWorldName.Text; ThisWorld.WorldAuthor = txtAuthor.Text; ThisWorld.WorldState = world; ThisWorld.DefaultPlayer = ThisPlayer; if (ThisWorld.Credits == null) ThisWorld.Credits = new List<string>(); ThisWorld.Credits.Clear(); foreach (string credit in Credits) { ThisWorld.Credits.Add(credit); } using (Stream stream = File.Open(SavePath, FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, ThisWorld); } return true; } else return false; } catch { return false; } }
public frmWorldDesigner(bool LoadWorld, string WorldName) { InitializeComponent(); //Check If New World if (LoadWorld == true) { string LoadPath = string.Concat(Directory.GetCurrentDirectory(), "\\Worlds\\", WorldName); //MessageBox.Show(LoadPath); DataTypes.WorldFile WorldState = new DataTypes.WorldFile(); using (Stream stream = File.Open(LoadPath, FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter(); WorldState = (DataTypes.WorldFile)bin.Deserialize(stream); } txtWorldName.Text = WorldState.WorldName; txtAuthor.Text = WorldState.WorldAuthor; world = WorldState.WorldState; if (WorldState.Credits != null) foreach (string credit in WorldState.Credits) { Credits.Add(credit); } ThisFloor = world[0]; txtFloorName.Text = ThisFloor.FloorName; txtMusicPath.Text = ThisFloor.FloorSong; ThisPlayer = WorldState.DefaultPlayer; cmbLevelSelect.Items.Clear(); for (int i = 1; i <= world.Count; i++) { cmbLevelSelect.Items.Add("Level " + i); } cmbLevelSelect.Items.Add("Add New Level..."); this.tblWorldLevel.CellPaint += new TableLayoutCellPaintEventHandler(tblWorldLevel_CellPaint); lblEditor.Text = "Default Starting Position is B:2"; } else { world = new List<DataTypes.Floor>(); ThisFloor = new DataTypes.Floor(); ThisFloor.CurrentFloor = new DataTypes.roomInfo[10, 10]; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { ThisFloor.CurrentFloor[x, y].CanMove = true; } } for (int x = 0; x < 10; x++) { ThisFloor.CurrentFloor[x, 0].CanMove = false; ThisFloor.CurrentFloor[x, 9].CanMove = false; } for (int y = 0; y < 10; y++) { ThisFloor.CurrentFloor[0, y].CanMove = false; ThisFloor.CurrentFloor[9, y].CanMove = false; } ThisFloor.CurrentFloor[1, 1].Description = "This is the starting position. When the player spawns this is the first thing they will read"; world.Add(ThisFloor); this.tblWorldLevel.CellPaint += new TableLayoutCellPaintEventHandler(tblWorldLevel_CellPaint); txtFloorName.Text = "Level 1"; lblEditor.Text = "Default Starting Position is B:2"; ThisPlayer = DefaultPlayer(); } SetWorldSize(); AddTableLabels(); }