/// <summary> /// Loads a level /// </summary> /// <param name="path">Path to level to load</param> /// <param name="width">Width of level</param> /// <param name="height">Height of level</param> /// <returns>The level loaded</returns> public static Level LoadLevel(string path, int width, int height) { Level level = new Level(width, height); string[] fileData = File.ReadAllText(path).Split(new string[]{" ", "\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); int i = 0, j = 0; foreach (string s in fileData) { if (s.Equals("0")) { level.SetData(i, j, null); } else { bool visible = true; string name = s; if (name.StartsWith("-")) { visible = false; name = name.Substring(1, s.Length - 1); } level.SetData(i, j, new LevelData(name, visible)); } i++; if (i >= width) { j++; i = 0; if (j >= height) { break; } } } return level; }
public MainForm() { InitializeComponent(); SetLevelSelected(false); LoadLevelList(); currentLevel = new Level(levelWidth, levelHeight); blockDatabase = new BlockDB(blockListPath); blockDatabase.LoadBlocks(); InitializeBlockBrushes(); }
private void LevelListSelectedIndexChanged(object sender, EventArgs e) { bool loadLevel = !levelUnsaved; if (previousListBoxIndex != -1 && levelListBox.SelectedIndex != previousListBoxIndex && levelUnsaved) { DialogResult result = MessageBox.Show("You are about to switch level. Do you want to save your current level?", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == System.Windows.Forms.DialogResult.Yes) { loadLevel = true; Save((string)levelListBox.Items[previousListBoxIndex]); } else if (result == System.Windows.Forms.DialogResult.Cancel) { levelListBox.SelectedIndex = previousListBoxIndex; } else { loadLevel = true; SetLevelUnsaved(false); } } if (loadLevel && levelListBox.SelectedIndex != -1) { currentLevel = FileHelper.LoadLevel((string)levelListBox.SelectedItem, levelWidth, levelHeight); levelRenderBox.Refresh(); } previousListBoxIndex = levelListBox.SelectedIndex; SetLevelSelected(levelListBox.SelectedItem != null); }
/// <summary> /// Saves the provided level to the specified path /// </summary> /// <param name="level">Level to save</param> /// <param name="path">Path to file</param> /// <returns>True if file was saved, otherwise false</returns> public static bool SaveLevel(Level level, string path) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < level.Height; i++) { for (int j = 0; j < level.Width; j++) { LevelData data = level.GetData(j, i); if (data != null) { if (data.Visible == false) { stringBuilder.Append("-"); } stringBuilder.Append(data.Name); } else { stringBuilder.Append(emptyCharacter); } stringBuilder.Append(" "); } stringBuilder.Append("\r\n"); } try { File.WriteAllText(path, stringBuilder.ToString()); } catch (IOException e) { MessageBox.Show("Could not save level: " + e.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } return true; }