DialoguePanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. StartShowMassge();
  146. }
  147. public void ShowPanel(int[] LanID, string[] icon,
  148. ShowDialogueEventData.MessageShowType messageShowType,
  149. Action<int?> finish)
  150. {
  151. showIconName = icon;
  152. this.dialogueMessaga = LanID;
  153. this.messageShowType = messageShowType;
  154. this.finish = finish;
  155. index = 0;
  156. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  157. StartShowMassge();
  158. }
  159. private async void StartShowMassge()
  160. {
  161. if (isOver)
  162. return;
  163. if (index >= dialogueMessaga.Length)
  164. {
  165. isOver = true;
  166. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  167. {
  168. ShowOptions();
  169. }
  170. // 所有句子显示完成,检查是否有选项
  171. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  172. {
  173. // ShowOptions();
  174. }
  175. else
  176. {
  177. if (eventConditionConfig.ID != 0 && _eventLinkData != null && _eventLinkData.eventConditions.Count > 0)
  178. {
  179. foreach (var eventConditionData in _eventLinkData.eventConditions)
  180. {
  181. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  182. {
  183. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionData));
  184. return;
  185. }
  186. }
  187. }
  188. if (eventConditionConfig.optionType == 3)
  189. {
  190. List<ItemInfo> itemInfos = new List<ItemInfo>();
  191. for (var i = 0; i < eventConditionConfig.PrizeIDs.Length; i++)
  192. {
  193. DropConfig dropConfig = ConfigComponent.Instance.Get<DropConfig>(eventConditionConfig.PrizeIDs[i]);
  194. if (dropConfig.dropType == 3)
  195. {
  196. ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0], eventConditionConfig.PrizeNums[i]);
  197. itemInfos.Add(itemInfo);
  198. }
  199. else
  200. {
  201. List<ItemInfo> items = DropManager.Instance.DropItem(eventConditionConfig.PrizeIDs[i]);
  202. itemInfos.AddRange(items);
  203. }
  204. }
  205. ShowItemMoveToTargetPanel showItemMoveToTargetUiPane = UIManager.Instance.GetComponent<ShowItemMoveToTargetPanel>();
  206. Vector3 target = showItemMoveToTargetUiPane.transform.worldToLocalMatrix * Btn_Bag.transform.position;
  207. Vector3 startPos = showItemMoveToTargetUiPane.transform.worldToLocalMatrix * StartPos.transform.position;
  208. int maxCount = 1;
  209. foreach (var itemInfo in itemInfos)
  210. {
  211. int addValue = (int)itemInfo.count.Value / maxCount;
  212. int finishVale = (int)itemInfo.count.Value % maxCount;
  213. // ItemInfo i = itemInfo;
  214. showItemMoveToTargetUiPane.ShowPanel(startPos, target, maxCount, itemInfo.config.icon, CombatItemShowEventData.ShowType.HeroExp, (imageMove) =>
  215. {
  216. int currAddValue = addValue;
  217. if (imageMove.GroupIndex == maxCount - 1)
  218. {
  219. currAddValue += finishVale;
  220. }
  221. });
  222. }
  223. await TimerComponent.Instance.WaitAsync(2000);
  224. }
  225. // 无选项,关闭面板,返回 null
  226. UIManager.Instance.HideUIUIPanel(this);
  227. finish?.Invoke(null);
  228. }
  229. return;
  230. }
  231. _isShowNextButton = false;
  232. shownextIcon.SetActive(false);
  233. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  234. if (showIconName != null && index < showIconName.Length)
  235. {
  236. icon.icon_name = showIconName[index];
  237. }
  238. index++;
  239. switch (messageShowType)
  240. {
  241. case ShowDialogueEventData.MessageShowType.Default:
  242. showText.text = m;
  243. ShowNextIcon();
  244. break;
  245. case ShowDialogueEventData.MessageShowType.Verbatim:
  246. _sb.Clear();
  247. _currShowMessage = m.ToCharArray();
  248. _sb.Append(_currShowMessage[0]);
  249. showText.text = _sb.ToString();
  250. _isUpdate = true;
  251. _currShowIndex = 1;
  252. _skipTyping = false;
  253. break;
  254. }
  255. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  256. }
  257. private async void ShowOptions()
  258. {
  259. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  260. _isShowingOptions = true;
  261. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  262. {
  263. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  264. {
  265. DialogueOptionWidget dialogueOptionWidget =
  266. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  267. dialogueOptionWidget.CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i], eventConditionConfig.ID, SelectOption);
  268. }
  269. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  270. OptionMarsk.SetActive(true);
  271. }
  272. }
  273. private void SelectOption(DialogueOptionWidget obj)
  274. {
  275. if (!EventSystemManager.Instance.CeekEventGroupComplete(obj.eventLinkData.eventConditions))
  276. {
  277. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  278. return;
  279. }
  280. OptionMarsk.SetActive(false);
  281. int selectedOptionID = obj.eventConditionConfig.ID;
  282. UIManager.Instance.HideUIUIPanel(this);
  283. finish?.Invoke(selectedOptionID);
  284. }
  285. private void ShowNextIcon()
  286. {
  287. shownextIcon.SetActive(true);
  288. _isShowNextButton = true;
  289. }
  290. public override void Hide()
  291. {
  292. base.Hide();
  293. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  294. }
  295. public void Update()
  296. {
  297. if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  298. return;
  299. if (!_isUpdate) return;
  300. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  301. {
  302. if (_currShowIndex >= _currShowMessage.Length)
  303. {
  304. _isUpdate = false;
  305. ShowNextIcon();
  306. if (index >= dialogueMessaga.Length)
  307. {
  308. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  309. {
  310. ShowOptions();
  311. }
  312. }
  313. return;
  314. }
  315. _currShowTime += Time.deltaTime;
  316. if (_currShowTime > _showTime)
  317. {
  318. _currShowTime -= _showTime;
  319. _sb.Append(_currShowMessage[_currShowIndex]);
  320. _currShowIndex++;
  321. showText.text = _sb.ToString();
  322. }
  323. }
  324. }
  325. public override void Close()
  326. {
  327. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  328. _isShowingOptions = false;
  329. _skipTyping = false;
  330. index = 0;
  331. eventConditionConfig = default;
  332. _eventLinkData = null;
  333. CurrentEventList = null;
  334. _currShowMessage = null;
  335. dialogueMessaga = null;
  336. DialgueType1.gameObject.SetActive(false);
  337. DialgueType2.gameObject.SetActive(false);
  338. OptionMarsk.SetActive(false);
  339. isOver = false;
  340. base.Close();
  341. }
  342. }
  343. }