public static bool?ToNullableBoolean(this string s, bool?def = default(bool?)) { bool result; return(Boolean.TryParse(s, out result) ? result : def); }
/* * Function executes a single line in the current script after it has already been processed into an array of arguments * Parameter afterSave controls whether the line is the first line after loading a saved game, in which case it does not execute text lines (as the text is contained in the save) */ private void ExecuteCommand(List <String> command, bool afterSave) { int intValue; bool boolValue; // Runs function corresponding to the command with the variables given switch (command[0]) { // Setup case "label": if (command.Count == 3) { _assets.CreateLabel(command[2], currentScriptIndex.ToString(), command[1]); } break; case "image": switch (command.Count) { case 3: _assets.CreateBackground(command[1], command[2], true); break; case 4: _assets.AddImageToCharacter(command[1], command[2], command[3]); break; } break; case "character": if (command.Count == 4) { if (command.Contains("abbreviation")) { _assets.AddAbbreviationToCharacter(command[1], command[3]); break; } if (command.Contains("height")) { _assets.SetCharacterHeight(command[1], command[3]); break; } if (command.Contains("horizontalOffset")) { _assets.SetCharacterOffset(command[1], command[3], false); break; } if (command.Contains("verticalOffset")) { _assets.SetCharacterOffset(command[1], command[3], true); break; } } switch (command.Count) { case 4: _assets.CreateCharacter(command[1], command[2], command[3]); break; case 3: _assets.CreateCharacter(command[1], command[2]); break; case 2: _assets.CreateCharacter(command[1]); break; } break; case "color": if (command.Count == 5) { _assets.SetCharacterColor(command[1], command[2], command[3], command[4]); } break; case "sound": if (command.Count == 3) { _assets.CreateSound(command[1], command[2], false); } break; case "music": if (command.Count == 3) { _assets.CreateSound(command[1], command[2], true); } break; case "video": if (command.Count == 3) { _assets.CreateVideo(command[1], command[2]); } break; case "choice": if (command.Count == 3 && command[1] == "create") { _assets.CreateChoice(command[2]); } else if (command.Count == 5) { string choiceName = command[1]; if (command[2] == "set" && command[3] == "text") { _assets.EditChoiceText(choiceName, command[4]); } else if (command[2] == "add") { _assets.AddOptionToChoice(choiceName, command[3], command[4]); } } break; case "font": if (command.Count == 3) { SetFont(command[1], command[2]); } break; // Script navigation case "jump": if (command.Count == 2) { JumpToLabel(command[1]); } break; case "include": if (command.Count == 2) { _scripts.Add(new Script(command[1], _scripts.Count)); ProcessScript(_scripts.Count - 1); } break; case "comment": break; // Graphics case "show": // Additional options if (command.Contains("pause")) { Settings.executeNext = false; } int fadeDuration = 0; if (command.Contains("fade")) { int commandIndex = command.FindIndex(s => s == "fade"); int.TryParse(command[commandIndex + 1], out fadeDuration); } if (command.Count >= 2) { // Try to show background with given name if (ShowBackground(command[1], fadeDuration)) { break; } // Try to show choice with given name if (ShowChoice(command[1])) { Settings.executeNext = false; break; } } // Try to show character with given name if (command.Count >= 4) { if (command.Contains("left")) { if (ShowCharacter(command[1], command[2], fadeDuration, "left")) { break; } } if (command.Contains("right")) { if (ShowCharacter(command[1], command[2], fadeDuration, "right")) { break; } } else { if (ShowCharacter(command[1], command[2], fadeDuration)) { break; } } } break; case "clear": int unprocessedParts = command.Count - 1; if (command.Contains("pause")) { unprocessedParts--; Settings.executeNext = false; } fadeDuration = 0; if (command.Contains("fade")) { unprocessedParts--; int commandIndex = command.FindIndex(s => s == "fade"); if (int.TryParse(command[commandIndex + 1], out fadeDuration)) { unprocessedParts--; } } if (command.Contains("background")) { ClearBackground(fadeDuration); break; } if (unprocessedParts > 0) { ClearCharacters(fadeDuration, command[1]); break; } ClearCharacters(fadeDuration); break; case "ui": if (command.Contains("pause")) { Settings.executeNext = false; } if (command.Contains("show")) { ManipulateUI(true); } else if (command.Contains("hide")) { ManipulateUI(false); } break; // Sound and video case "play": if (command.Contains("pause")) { Settings.executeNext = false; } bool allowProgress = command.Contains("progress"); bool hideUI = command.Contains("hide"); bool repeat = command.Contains("repeat") || command.Contains("r"); double volume = 1.0; if (command.Count >= 3) { double.TryParse(command[2], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out volume); } if (PlaySound(command[1], volume, repeat)) { break; } PlayVideo(command[1], volume, allowProgress, hideUI); break; case "stop": if (command.Contains("pause")) { Settings.executeNext = false; } switch (command[1]) { case "sound": StopSound(true); break; case "music": StopMusic(true); break; case "video": StopVideo(); break; default: StopSound(true); StopMusic(true); StopVideo(); break; } break; case "save": SaveGame(0); break; case "end": Settings.executeNext = false; EndGame(); break; // Variable manipulation case "int": if (Int32.TryParse(command[2], out intValue)) { _assets.CreateVariable(command[1], intValue); } break; case "bool": if (Boolean.TryParse(command[2], out boolValue)) { _assets.CreateVariable(command[1], boolValue); } break; case "if": Variable var = _assets.variables.Find(i => i.name == command[1]); if (var == null) { break; } switch (var) { case Assets.Boolean boolVal: if (Boolean.TryParse(command[3], out boolValue)) { if (boolValue == boolVal.value) { JumpToLabel(command[4]); } } break; case Integer intVal: if (Int32.TryParse(command[3], out intValue)) { switch (command[2]) { case "<" when intVal.value <intValue: case ">" when intVal.value> intValue: case "=" when intVal.value == intValue: JumpToLabel(command[4]); break; } } break; } break; case "add": if (Int32.TryParse(command[2], out intValue)) { _assets.IntegerAdd(command[1], intValue); } break; case "subtract": if (Int32.TryParse(command[2], out intValue)) { _assets.IntegerSubtract(command[1], intValue); } break; case "set": if (Int32.TryParse(command[2], out intValue)) { _assets.IntegerSet(command[1], intValue); } else if (Boolean.TryParse(command[2], out boolValue)) { _assets.BooleanSet(command[1], boolValue); } break; // Settings case "name": if (command.Count == 3) { if (command[1] == "game") { Settings.gameName = command[2]; this.Title = command[2]; } else if (command[1] == "protagonist") { Settings.protagonistName = command[2]; } } break; // Text default: if (!afterSave) { if (!ExecuteTextCommand(command)) { MessageBox.Show("Error in displaying text on line " + _scripts[currentScriptIndex].currentLine + "!"); } Settings.executeNext = false; } break; } }
/// <summary> /// String to Boolean(字符串 转换成 布尔型) /// </summary> /// <remarks> /// 2014-06-23 16:31 Created By iceStone /// </remarks> /// <param name="s">String</param> /// <param name="def">Default</param> /// <returns>Byte</returns> public static bool ToBoolean(this string s, bool def = default(bool)) { bool result; return(Boolean.TryParse(s, out result) ? result : def); }