DialoguePanel.cs 15 KB

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