DialoguePanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.Text;
  2. using Common.Utility.CombatEvent;
  3. using Core.Language;
  4. using Excel2Json;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using Fort23.Core;
  11. using GameLogic.Bag;
  12. using GameLogic.Player;
  13. using UnityEngine.UI;
  14. namespace Fort23.Mono
  15. {
  16. [UIBinding(prefab = "DialoguePanel")]
  17. public partial class DialoguePanel : UIPanel
  18. {
  19. private int[] dialogueMessaga;
  20. private ShowDialogueEventData.MessageShowType messageShowType;
  21. private Action<int?> finish;
  22. private int index;
  23. private char[] _currShowMessage;
  24. private float _showTime = 0.05f;
  25. private float _currShowTime = 0;
  26. protected StringBuilder _sb = new StringBuilder();
  27. private int _currShowIndex;
  28. private bool _isUpdate;
  29. private bool _isShowNextButton;
  30. private string[] showIconName;
  31. private EventLinkConfig eventConditionConfig;
  32. private bool _skipTyping;
  33. private bool _isShowingOptions;
  34. private AccountFileInfo.EventList CurrentEventList;
  35. private AccountFileInfo.EventLinkData _eventLinkData;
  36. private int type;
  37. private Text showText;
  38. private GameObject shownextIcon;
  39. private bool isOver;
  40. public static async void OpenDialoguePanel(AccountFileInfo.EventList CurrentEventList, int id, string[] icon,
  41. ShowDialogueEventData.MessageShowType messageShowType,
  42. Action<int?> finish)
  43. {
  44. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  45. dialoguePanel.ShowPanel(CurrentEventList, id, icon, messageShowType, finish);
  46. }
  47. public static async void OpenDialoguePanel(int[] LanID, string[] icon,
  48. ShowDialogueEventData.MessageShowType messageShowType,
  49. Action<int?> finish)
  50. {
  51. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  52. dialoguePanel.ShowPanel(LanID, icon, messageShowType, finish);
  53. }
  54. private void Init()
  55. {
  56. isAddStack = false;
  57. _skipTyping = false;
  58. _isShowingOptions = false;
  59. }
  60. protected override void AddEvent()
  61. {
  62. }
  63. protected override void DelEvent()
  64. {
  65. }
  66. public override void AddButtonEvent()
  67. {
  68. nextButton.onClick.AddListener(NextShow);
  69. Btn_Cancel.onClick.AddListener(() =>
  70. {
  71. UIManager.Instance.HideUIUIPanel(this);
  72. finish?.Invoke(-1);
  73. });
  74. }
  75. private void SkipTyping()
  76. {
  77. _skipTyping = true;
  78. if (_isUpdate)
  79. {
  80. _sb.Clear();
  81. _sb.Append(_currShowMessage);
  82. showText.text = _sb.ToString();
  83. _currShowIndex = _currShowMessage.Length;
  84. _isUpdate = false;
  85. ShowNextIcon();
  86. if (index >= dialogueMessaga.Length)
  87. {
  88. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  89. {
  90. ShowOptions();
  91. }
  92. }
  93. }
  94. }
  95. private void NextShow()
  96. {
  97. if (eventConditionConfig.optionType == 1 && _isShowingOptions)
  98. return;
  99. if (_isUpdate)
  100. {
  101. SkipTyping();
  102. }
  103. else
  104. {
  105. StartShowMassge();
  106. }
  107. }
  108. public void ShowPanel(AccountFileInfo.EventList CurrentEventList, int id, string[] icon,
  109. ShowDialogueEventData.MessageShowType messageShowType,
  110. Action<int?> finish)
  111. {
  112. this.CurrentEventList = CurrentEventList;
  113. eventConditionConfig = ConfigComponent.Instance.Get<EventLinkConfig>(id);
  114. _eventLinkData = CurrentEventList.eventLinks.FirstOrDefault(el => el.eventLinkId == id);
  115. showIconName = icon;
  116. this.dialogueMessaga = eventConditionConfig.LanID;
  117. this.messageShowType = messageShowType;
  118. this.finish = finish;
  119. index = 0;
  120. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  121. if (eventConditionConfig.DialogueType == 0 || eventConditionConfig.DialogueType == 1)
  122. {
  123. DialgueType1.gameObject.SetActive(true);
  124. DialgueType2.gameObject.SetActive(false);
  125. DialgueType3.gameObject.SetActive(false);
  126. showText = message;
  127. shownextIcon = nextIcon;
  128. }
  129. else if (eventConditionConfig.DialogueType == 2)
  130. {
  131. DialgueType1.gameObject.SetActive(false);
  132. DialgueType3.gameObject.SetActive(false);
  133. DialgueType2.gameObject.SetActive(true);
  134. showText = message2;
  135. shownextIcon = nextIcon2;
  136. }
  137. else if (eventConditionConfig.DialogueType == 3)
  138. {
  139. DialgueType1.gameObject.SetActive(false);
  140. DialgueType2.gameObject.SetActive(false);
  141. DialgueType3.gameObject.SetActive(true);
  142. showText = message3;
  143. shownextIcon = nextIcon3;
  144. }
  145. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  146. Text_Name1.text = LanguageManager.Instance.Text(eventNpc.name);
  147. StartShowMassge();
  148. }
  149. public void ShowPanel(int[] LanID, string[] icon,
  150. ShowDialogueEventData.MessageShowType messageShowType,
  151. Action<int?> finish)
  152. {
  153. showIconName = icon;
  154. this.dialogueMessaga = LanID;
  155. this.messageShowType = messageShowType;
  156. this.finish = finish;
  157. index = 0;
  158. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  159. StartShowMassge();
  160. }
  161. private async void StartShowMassge()
  162. {
  163. if (isOver)
  164. return;
  165. if (index >= dialogueMessaga.Length)
  166. {
  167. isOver = true;
  168. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  169. {
  170. ShowOptions();
  171. }
  172. // 所有句子显示完成,检查是否有选项
  173. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  174. {
  175. // ShowOptions();
  176. }
  177. else
  178. {
  179. if (eventConditionConfig.ID != 0 && _eventLinkData != null && _eventLinkData.eventConditions.Count > 0)
  180. {
  181. foreach (var eventConditionData in _eventLinkData.eventConditions)
  182. {
  183. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  184. {
  185. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionData));
  186. return;
  187. }
  188. }
  189. }
  190. if (eventConditionConfig.optionType == 3)
  191. {
  192. List<ItemInfo> itemInfos = new List<ItemInfo>();
  193. for (var i = 0; i < eventConditionConfig.PrizeIDs.Length; i++)
  194. {
  195. DropConfig dropConfig = ConfigComponent.Instance.Get<DropConfig>(eventConditionConfig.PrizeIDs[i]);
  196. if (dropConfig.dropType == 3)
  197. {
  198. ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0], eventConditionConfig.PrizeNums[i]);
  199. itemInfos.Add(itemInfo);
  200. }
  201. else
  202. {
  203. List<ItemInfo> items = DropManager.Instance.DropItem(eventConditionConfig.PrizeIDs[i]);
  204. itemInfos.AddRange(items);
  205. }
  206. }
  207. ShowItemMoveToTargetPanel showItemMoveToTargetUiPane = UIManager.Instance.GetComponent<ShowItemMoveToTargetPanel>();
  208. Vector3 target = showItemMoveToTargetUiPane.transform.worldToLocalMatrix * Btn_Bag.transform.position;
  209. Vector3 startPos = showItemMoveToTargetUiPane.transform.worldToLocalMatrix * StartPos.transform.position;
  210. int maxCount = 1;
  211. foreach (var itemInfo in itemInfos)
  212. {
  213. int addValue = (int)itemInfo.count.Value / maxCount;
  214. int finishVale = (int)itemInfo.count.Value % maxCount;
  215. // ItemInfo i = itemInfo;
  216. showItemMoveToTargetUiPane.ShowPanel(startPos, target, maxCount, itemInfo.config.icon, CombatItemShowEventData.ShowType.HeroExp, (imageMove) =>
  217. {
  218. int currAddValue = addValue;
  219. if (imageMove.GroupIndex == maxCount - 1)
  220. {
  221. currAddValue += finishVale;
  222. }
  223. });
  224. }
  225. await TimerComponent.Instance.WaitAsync(2000);
  226. }
  227. // 无选项,关闭面板,返回 null
  228. UIManager.Instance.HideUIUIPanel(this);
  229. finish?.Invoke(null);
  230. }
  231. return;
  232. }
  233. _isShowNextButton = false;
  234. shownextIcon.SetActive(false);
  235. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  236. if (showIconName != null && index < showIconName.Length)
  237. {
  238. icon.icon_name = showIconName[index];
  239. }
  240. index++;
  241. switch (messageShowType)
  242. {
  243. case ShowDialogueEventData.MessageShowType.Default:
  244. showText.text = m;
  245. ShowNextIcon();
  246. break;
  247. case ShowDialogueEventData.MessageShowType.Verbatim:
  248. _sb.Clear();
  249. _currShowMessage = m.ToCharArray();
  250. _sb.Append(_currShowMessage[0]);
  251. showText.text = _sb.ToString();
  252. _isUpdate = true;
  253. _currShowIndex = 1;
  254. _skipTyping = false;
  255. break;
  256. }
  257. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  258. }
  259. private async void ShowOptions()
  260. {
  261. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  262. _isShowingOptions = true;
  263. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  264. {
  265. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  266. {
  267. DialogueOptionWidget dialogueOptionWidget =
  268. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  269. dialogueOptionWidget.CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i], eventConditionConfig.ID, SelectOption);
  270. }
  271. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  272. OptionMarsk.SetActive(true);
  273. }
  274. }
  275. private void SelectOption(DialogueOptionWidget obj)
  276. {
  277. if (!EventSystemManager.Instance.CeekEventGroupComplete(obj.eventLinkData.eventConditions))
  278. {
  279. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  280. return;
  281. }
  282. OptionMarsk.SetActive(false);
  283. int selectedOptionID = obj.eventConditionConfig.ID;
  284. UIManager.Instance.HideUIUIPanel(this);
  285. finish?.Invoke(selectedOptionID);
  286. }
  287. private void ShowNextIcon()
  288. {
  289. shownextIcon.SetActive(true);
  290. _isShowNextButton = true;
  291. }
  292. public override void Hide()
  293. {
  294. base.Hide();
  295. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  296. }
  297. public void Update()
  298. {
  299. // if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  300. // return;
  301. if (!_isUpdate) return;
  302. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  303. {
  304. if (_currShowIndex >= _currShowMessage.Length)
  305. {
  306. _isUpdate = false;
  307. ShowNextIcon();
  308. if (index >= dialogueMessaga.Length)
  309. {
  310. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  311. {
  312. ShowOptions();
  313. }
  314. }
  315. return;
  316. }
  317. _currShowTime += Time.deltaTime;
  318. if (_currShowTime > _showTime)
  319. {
  320. _currShowTime -= _showTime;
  321. _sb.Append(_currShowMessage[_currShowIndex]);
  322. _currShowIndex++;
  323. showText.text = _sb.ToString();
  324. }
  325. }
  326. }
  327. public override void Close()
  328. {
  329. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  330. _isShowingOptions = false;
  331. _skipTyping = false;
  332. index = 0;
  333. eventConditionConfig = default;
  334. _eventLinkData = null;
  335. CurrentEventList = null;
  336. _currShowMessage = null;
  337. dialogueMessaga = null;
  338. // DialgueType1.gameObject.SetActive(false);
  339. // DialgueType2.gameObject.SetActive(false);
  340. // OptionMarsk.SetActive(false);
  341. isOver = false;
  342. base.Close();
  343. }
  344. }
  345. }