public void ReadText(string text) { if (dialogText != null) { //放回跳脫字元 text = RPGCore.Put_Back_EscChar(text); //自定義變數 text = RPGCore.ReadCustomVariables(text); dialogText.text = text; } }
//參數設定 public void ReadArgs(ref string args) { if (args == "") { return; } //跳脫字元 args = RPGCore.Put_Back_EscChar(args); //自定義變數 args = RPGCore.ReadCustomVariables(args); //title foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_TITLE)) { title.text = match.Groups["title"].Value; } //by foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_BY)) { //找到說話的物件,計算位置,將UI放置在該物件旁 (UI座標) GameObject talk_by = GameObject.Find(match.Groups["by"].Value); if (talk_by != null) { Vector3 target_top_pos = talk_by.transform.position; //在物件上方 Renderer rend = talk_by.GetComponent <Renderer>(); if (rend != null) { target_top_pos.y += rend.bounds.size.y * 1.5f; } else { target_top_pos.y += 2.5f; } Vector2 ui_pos = Camera.main.WorldToScreenPoint(target_top_pos); //ui_pos.y+=100; Wrapper.transform.position = ui_pos; } else { Debug.LogError("talk by not found."); } } #region //img foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_IMG)) { SetPortraitImg(match.Groups["side"].Value, match.Groups["imgPath"].Value); } //animation foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_ANIMATION)) { GameObject target = GameObject.Find((match.Groups["objectName"].Value)); if (target != null) { Animator animator = target.GetComponent <Animator>(); if (animator != null) { animator.Play((match.Groups["clipName"].Value)); } else { Debug.LogError("<animation arg error> animator not found"); } } } //audio foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_AUDIO)) { string operation = (match.Groups["operation"].Value); GameObject target = GameObject.Find((match.Groups["objectName"].Value)); if (target != null) { AudioSource audioSource = target.GetComponent <AudioSource>(); if (audioSource != null) { if (operation == "play") { audioSource.Play(); } else if (operation == "pause") { audioSource.Pause(); } else if (operation == "stop") { audioSource.Stop(); } } else { Debug.LogError("<audio arg error> audioSource not found"); } } } //set foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_SET)) { Debug.Log("SET"); string _key = Regex.Replace(match.Groups["key"].Value, @"['""]", "");; string _value = match.Groups["value"].Value; string _operator = match.Groups["operator"].Value; //optional string _value2 = match.Groups["value2"].Value; //optional //判斷value是字典key或值 bool v1_is_key = Regex.IsMatch(_value, @"['""].*?['""]"); bool v2_is_key = Regex.IsMatch(_value2, @"['""].*?['""]"); string v1 = _value, v2 = _value2; if (v1_is_key) { v1 = RPGCore.GetDictValue(Regex.Replace(_value, @"['""]", "")); } if (v2_is_key) { v2 = RPGCore.GetDictValue(Regex.Replace(_value2, @"['""]", "")); } //先做運算 if (_operator != "") { int v1_num = int.Parse(v1); int v2_num = int.Parse(v2); if (_operator == "+") { RPGCore.SetDictionaryValue(_key, (v1_num + v2_num).ToString()); } if (_operator == "-") { RPGCore.SetDictionaryValue(_key, (v1_num - v2_num).ToString()); } if (_operator == "*") { RPGCore.SetDictionaryValue(_key, (v1_num * v2_num).ToString()); } if (_operator == "/") { RPGCore.SetDictionaryValue(_key, (v1_num / v2_num).ToString()); } } else { //不做運算直接給值 RPGCore.SetDictionaryValue(_key, v1); } } //Init foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_INIT)) { Debug.Log("Init"); string _key = Regex.Replace(match.Groups["key"].Value, @"['""]", "");; string _value = match.Groups["value"].Value; string _operator = match.Groups["operator"].Value; //optional string _value2 = match.Groups["value2"].Value; //optional //判斷value是字典key或值 bool v1_is_key = Regex.IsMatch(_value, @"['""].*?['""]"); bool v2_is_key = Regex.IsMatch(_value2, @"['""].*?['""]"); string v1 = _value, v2 = _value2; bool key_is_inDict = false; RPGCore.GetDictValue(_key, out key_is_inDict); //已經在字典裡就不用設值 if (key_is_inDict) { return; } if (v1_is_key) { v1 = RPGCore.GetDictValue(Regex.Replace(_value, @"['""]", "")); } if (v2_is_key) { v2 = RPGCore.GetDictValue(Regex.Replace(_value2, @"['""]", "")); } //先做運算 if (_operator != "") { int v1_num = int.Parse(v1); int v2_num = int.Parse(v2); if (_operator == "+") { RPGCore.SetDictionaryValue(_key, (v1_num + v2_num).ToString()); } if (_operator == "-") { RPGCore.SetDictionaryValue(_key, (v1_num - v2_num).ToString()); } if (_operator == "*") { RPGCore.SetDictionaryValue(_key, (v1_num * v2_num).ToString()); } if (_operator == "/") { RPGCore.SetDictionaryValue(_key, (v1_num / v2_num).ToString()); } } else { //不做運算直接給值 RPGCore.SetDictionaryValue(_key, v1); } } //其他 (可刪除,或去補齊方法cs檔): 用括號的參數改用統一regex拆解 foreach (Match match in GetMatch(args, RPGCore.REGEX_FUNC_PARA)) { //方法名稱 string functionName = match.Groups["func"].Value; Debug.Log(functionName); string[] paras = match.Groups["para"].Value.Split(','); //enable /disable if (functionName == "enable") { foreach (string objName in paras) { GameObject target = temp_preparedList.Find(x => x.name.Equals(objName)); if (target != null) { target.SetActive(true); } } } if (functionName == "disable") { foreach (string objName in paras) { GameObject target = temp_preparedList.Find(x => x.name.Equals(objName)); if (target != null) { target.SetActive(false); } } } //**************** YOUR OWN SCRIPT FUNCTION ***************************** //Effect 特效 if (functionName == "effect") { foreach (string effect_name in paras) { if (effect_name == "shake") { //Effecter.cs //StartCoroutine(Effecter.Shake(Wrapper, 5, 10, 0.25f)); Debug.Log("Do SHAKE"); } } } //changeScene(scenetoLoad ) 換場景 if (functionName == "changeScene") { //load場景 string scene_to_load = paras[0]; SceneManager.LoadScene(scene_to_load); } if (functionName == "price") { string _item = paras[0].Trim(); Merchant merchant = FindObjectOfType <Merchant>(); if (_item.Equals("wep")) { RPGCore.SetDictionaryValue("temp_wep_cost", merchant.selling_weapon.cost.ToString()); } else if (_item.Equals("ammo")) { RPGCore.SetDictionaryValue("temp_ammo_cost", merchant.selling_ammo.cost.ToString()); } } //buy(item) if (functionName == "buy") { string bought_item = paras[0]; Debug.Log("BUY " + bought_item); if (bought_item.Equals("hp")) { int _cost = int.Parse(RPGCore.temp_StoryRecord["temp_hp_cost"]); if (ScoreManager.instance.UsePoint(_cost)) { FindObjectOfType <PlayerControl>().Heal(100); } else { RPGCore.temp_StoryRecord["temp_buy_res"] = "0"; } } else if (bought_item.Equals("weapon")) { Debug.Log("BUY weapon"); if (!FindObjectOfType <Merchant>().BuyWeapon()) { RPGCore.temp_StoryRecord["temp_buy_res"] = "0"; } } else if (bought_item.Equals("ammo")) { if (!FindObjectOfType <Merchant>().BuyAmmo()) { RPGCore.temp_StoryRecord["temp_buy_res"] = "0"; } } else if (bought_item.Equals("nextlv")) { FindObjectOfType <Merchant>().NextLv(); } else if (bought_item.Equals("money")) { //int _cost = int.Parse(RPGCore.temp_StoryRecord["temp_money_cost"]); BlackScreenEffect bke = FindObjectOfType <BlackScreenEffect>(); bke.SetMagnitude(bke.mask_magnitude * 2); ScoreManager.instance.AddScore(500); } } } #endregion }
//參數設定 public void ReadArgs(ref string args) { if (args == "") { return; } //跳脫字元 args = RPGCore.Put_Back_EscChar(args); //自定義變數 args = RPGCore.ReadCustomVariables(args); //title foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_TITLE)) { title.text = match.Groups["title"].Value; } //by foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_BY)) { //找到說話的物件,計算位置,將UI放置在該物件旁 (UI座標) GameObject talk_by = GameObject.Find(match.Groups["by"].Value); if (talk_by != null) { Vector3 target_top_pos = talk_by.transform.position; //在物件上方 Renderer rend = talk_by.GetComponent <Renderer>(); if (rend != null) { target_top_pos.y += rend.bounds.size.y * 1.5f; } else { target_top_pos.y += 2.5f; } Vector2 ui_pos = Camera.main.WorldToScreenPoint(target_top_pos); //ui_pos.y+=100; Wrapper.transform.position = ui_pos; } else { Debug.LogError("talk by not found."); } } #region //img foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_IMG)) { SetPortraitImg(match.Groups["side"].Value, match.Groups["imgPath"].Value); } //animation foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_ANIMATION)) { GameObject target = GameObject.Find((match.Groups["objectName"].Value)); if (target != null) { Animator animator = target.GetComponent <Animator>(); if (animator != null) { animator.Play((match.Groups["clipName"].Value)); } else { Debug.LogError("<animation arg error> animator not found"); } } } //audio foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_AUDIO)) { string operation = (match.Groups["operation"].Value); GameObject target = GameObject.Find((match.Groups["objectName"].Value)); if (target != null) { AudioSource audioSource = target.GetComponent <AudioSource>(); if (audioSource != null) { if (operation == "play") { audioSource.Play(); } else if (operation == "pause") { audioSource.Pause(); } else if (operation == "stop") { audioSource.Stop(); } } else { Debug.LogError("<audio arg error> audioSource not found"); } } } //set foreach (Match match in GetMatch(args, RPGCore.REGEX_ARGS_SET)) { Debug.Log("SET"); string _key = Regex.Replace(match.Groups["key"].Value, @"['""]", "");; string _value = match.Groups["value"].Value; string _operator = match.Groups["operator"].Value; //optional string _value2 = match.Groups["value2"].Value; //optional //判斷value是字典key或值 bool v1_is_key = Regex.IsMatch(_value, @"['""].*?['""]"); bool v2_is_key = Regex.IsMatch(_value2, @"['""].*?['""]"); string v1 = _value, v2 = _value2; if (v1_is_key) { v1 = RPGCore.GetDictValue(Regex.Replace(_value, @"['""]", "")); } if (v2_is_key) { v2 = RPGCore.GetDictValue(Regex.Replace(_value2, @"['""]", "")); } //先做運算 if (_operator != "") { int v1_num = int.Parse(v1); int v2_num = int.Parse(v2); if (_operator == "+") { RPGCore.SetDictionaryValue(_key, (v1_num + v2_num).ToString()); } if (_operator == "-") { RPGCore.SetDictionaryValue(_key, (v1_num - v2_num).ToString()); } if (_operator == "*") { RPGCore.SetDictionaryValue(_key, (v1_num * v2_num).ToString()); } if (_operator == "/") { RPGCore.SetDictionaryValue(_key, (v1_num / v2_num).ToString()); } } else { //不做運算直接給值 RPGCore.SetDictionaryValue(_key, v1); } } //其他 (可刪除,或去補齊方法cs檔): 用括號的參數改用統一regex拆解 foreach (Match match in GetMatch(args, RPGCore.REGEX_FUNC_PARA)) { //方法名稱 string functionName = match.Groups["func"].Value; Debug.Log(functionName); string[] paras = match.Groups["para"].Value.Split(','); //enable /disable if (functionName == "enable") { foreach (string objName in paras) { GameObject target = temp_preparedList.Find(x => x.name.Equals(objName)); if (target != null) { target.SetActive(true); } } } if (functionName == "disable") { foreach (string objName in paras) { GameObject target = temp_preparedList.Find(x => x.name.Equals(objName)); if (target != null) { target.SetActive(false); } } } //**************** YOUR OWN SCRIPT FUNCTION ***************************** //Effect 特效 if (functionName == "effect") { foreach (string effect_name in paras) { if (effect_name == "shake") { //Effecter.cs StartCoroutine(Effecter.Shake(Wrapper, 5, 10, 0.25f)); Debug.Log("Do SHAKE"); } } } //changeScene(scenetoLoad ) 換場景 if (functionName == "changeScene") { //load場景 string scene_to_load = paras[0]; SceneManager.LoadScene(scene_to_load); } } #endregion }
//tag設定 public IEnumerator ReadTags(string tag) { Debug.Log(line_index + " " + "TAG " + tag + Lines[line_index].args + " " + Lines[line_index].text); //l=line 基本單行對話 if (tag == "l") { //開啟對話框 if (dialogText != null) { dialogText.gameObject.SetActive(true); } ReadArgs(ref Lines[line_index].args); ReadText(Lines[line_index].text); //line_index = Lines[line_index].endLine + 1; //下一句 GoNextLine(Lines[line_index].endLine + 1); } //select 選單 內含opt數個 else if (tag == "select") { //開啟選單 if (selectPanel != null) { selectPanel.gameObject.SetActive(true); //清除舊選項 foreach (Transform child in selectPanel.transform) { GameObject.Destroy(child.gameObject); } } ReadArgs(ref Lines[line_index].args); ReadText(Lines[line_index].text); //select開始的index int select_start_index = line_index; line_index++; int if_start_index = 0; //在選單創建選項 (tag: opt) Debug.Log("select end line " + Lines[select_start_index].endLine); List <RPGCore.Line> _opts = new List <RPGCore.Line>(); for (int i = line_index; i < Lines[select_start_index].endLine;) { if (option_btn_prefab != null) { //Read Text: string _opt_text = RPGCore.Put_Back_EscChar(Lines[i].text); _opt_text = RPGCore.ReadCustomVariables(_opt_text); if (Lines[i].tag == "if") { //紀錄if開始位置 if_start_index = i; if (!RPGCore.if_compare(Lines[i].args)) { //skip this opt i = Lines[i].endLine + 1; continue; } else { i = i + 1; //Read Text: _opt_text = RPGCore.Put_Back_EscChar(Lines[i].text); _opt_text = RPGCore.ReadCustomVariables(_opt_text); } } else if (Lines[i].tag == "/if") { i = Lines[if_start_index].endLine + 1; continue; } GameObject opt = Instantiate(option_btn_prefab, selectPanel.transform.position, Quaternion.identity, selectPanel.transform); opt.name = _opts.Count.ToString(); //設定text //opt.GetComponentInChildren<TextMeshProUGUI>().text = Lines[i].text; opt.GetComponentInChildren <TextMeshProUGUI>().text = _opt_text; Debug.Log(line_index + " " + "TAG " + tag + Lines[i].args + " " + Lines[i].text); } _opts.Add(Lines[i]); i = Lines[i].endLine + 1; } //等待選擇: while (input == "") { yield return(new WaitForFixedUpdate()); } Debug.Log("<color=green> 選擇: " + input + " " + _opts[int.Parse(input)].args + "</color>"); //使用選擇的結果 ReadArgs(ref _opts[int.Parse(input)].args); //從select結尾+1開始 GoNextLine(Lines[select_start_index].endLine + 1); UpdateDialog(); } //if 條件式 // true=> 跳至下一行 // false=> 跳至endline+1 else if (tag == "if") { ReadArgs(ref Lines[line_index].args); if (RPGCore.if_compare(Lines[line_index].args)) { GoNextLine(line_index + 1); } else { GoNextLine(Lines[line_index].endLine + 1); } UpdateDialog(); } else if (tag.StartsWith("/")) //FOR:[bug] if、select結尾後沒有其他段落會直接跳到story tag後解讀失敗 { //line_index++; forceEnd = true; } else { Debug.LogError("未知tag " + tag); } yield return(new WaitForFixedUpdate()); isReadingTags = null; }