DialoguePanel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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.03f;
  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 CTask 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. public override CTask Show()
  55. {
  56. EventManager.Instance.Dispatch(CustomEventType.DialoguePanelOpen, null);
  57. return base.Show();
  58. }
  59. private void Init()
  60. {
  61. isAddStack = false;
  62. _skipTyping = false;
  63. _isShowingOptions = false;
  64. }
  65. protected override void AddEvent()
  66. {
  67. }
  68. protected override void DelEvent()
  69. {
  70. }
  71. public override void AddButtonEvent()
  72. {
  73. nextButton.onClick.AddListener(NextShow);
  74. Btn_Cancel.onClick.AddListener(() =>
  75. {
  76. UIManager.Instance.HideUIUIPanel(this);
  77. finish?.Invoke(-1);
  78. });
  79. }
  80. private void SkipTyping()
  81. {
  82. _skipTyping = true;
  83. if (_isUpdate)
  84. {
  85. _sb.Clear();
  86. _sb.Append(_currShowMessage);
  87. showText.text = _sb.ToString();
  88. _currShowIndex = _currShowMessage.Length;
  89. _isUpdate = false;
  90. ShowNextIcon();
  91. if (index >= dialogueMessaga.Length)
  92. {
  93. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  94. {
  95. ShowOptions();
  96. }
  97. }
  98. }
  99. }
  100. private void NextShow()
  101. {
  102. if (eventConditionConfig.optionType == 1 && _isShowingOptions)
  103. return;
  104. if (_isUpdate)
  105. {
  106. SkipTyping();
  107. }
  108. else
  109. {
  110. StartShowMassge();
  111. }
  112. }
  113. public void ShowPanel(AccountFileInfo.EventList CurrentEventList, int id, string[] icon,
  114. ShowDialogueEventData.MessageShowType messageShowType,
  115. Action<int?> finish)
  116. {
  117. this.CurrentEventList = CurrentEventList;
  118. eventConditionConfig = ConfigComponent.Instance.Get<EventLinkConfig>(id);
  119. _eventLinkData = CurrentEventList.eventLinks.FirstOrDefault(el => el.eventLinkId == id);
  120. showIconName = icon;
  121. this.dialogueMessaga = eventConditionConfig.LanID;
  122. this.messageShowType = messageShowType;
  123. this.finish = finish;
  124. index = 0;
  125. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  126. if (eventConditionConfig.DialogueType == 0 || eventConditionConfig.DialogueType == 1)
  127. {
  128. DialgueType1.gameObject.SetActive(true);
  129. DialgueType2.gameObject.SetActive(false);
  130. DialgueType3.gameObject.SetActive(false);
  131. showText = message;
  132. shownextIcon = nextIcon;
  133. }
  134. else if (eventConditionConfig.DialogueType == 2)
  135. {
  136. DialgueType1.gameObject.SetActive(false);
  137. DialgueType3.gameObject.SetActive(false);
  138. DialgueType2.gameObject.SetActive(true);
  139. showText = message2;
  140. shownextIcon = nextIcon2;
  141. }
  142. else if (eventConditionConfig.DialogueType == 3)
  143. {
  144. DialgueType1.gameObject.SetActive(false);
  145. DialgueType2.gameObject.SetActive(false);
  146. DialgueType3.gameObject.SetActive(true);
  147. showText = message3;
  148. shownextIcon = nextIcon3;
  149. }
  150. if (eventConditionConfig.NPCID == 100)
  151. {
  152. DialgueType1.GetComponent<CustomStateController>().ChangeState(0);
  153. // if (eventConditionConfig.DialogueType == 0 || eventConditionConfig.DialogueType == 1)
  154. // {
  155. //
  156. // }
  157. Text_Name1.text = PlayerManager.Instance.PlayerName;
  158. PLayerNameRoot.SetActive(true);
  159. }
  160. else if (eventConditionConfig.NPCID == 0)
  161. {
  162. PLayerNameRoot.SetActive(false);
  163. }
  164. else
  165. {
  166. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  167. if (eventNpc.isNpc)
  168. {
  169. DialgueType1.GetComponent<CustomStateController>().ChangeState(1);
  170. PLayerNameRoot.SetActive(true);
  171. Text_Name2.text = LanguageManager.Instance.Text(eventNpc.name);
  172. }
  173. else
  174. {
  175. PLayerNameRoot.SetActive(false);
  176. }
  177. }
  178. StartShowMassge();
  179. }
  180. public static string ReverseUsingLoop(string input)
  181. {
  182. if (string.IsNullOrEmpty(input))
  183. return input;
  184. StringBuilder sb = new StringBuilder(input.Length);
  185. for (int i = input.Length - 1; i >= 0; i--)
  186. {
  187. sb.Append(input[i]);
  188. }
  189. return sb.ToString();
  190. }
  191. public void ShowPanel(int[] LanID, string[] icon,
  192. ShowDialogueEventData.MessageShowType messageShowType,
  193. Action<int?> finish)
  194. {
  195. showIconName = icon;
  196. this.dialogueMessaga = LanID;
  197. this.messageShowType = messageShowType;
  198. this.finish = finish;
  199. index = 0;
  200. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  201. StartShowMassge();
  202. }
  203. private async void StartShowMassge()
  204. {
  205. if (isOver)
  206. return;
  207. icon.onSpriteAlter = () =>
  208. {
  209. icon.SetNativeSize();
  210. icon.gameObject.SetActive(true);
  211. };
  212. if (eventConditionConfig.NPCID != 100 && eventConditionConfig.NPCID != 0)
  213. {
  214. icon.gameObject.SetActive(false);
  215. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  216. icon.icon_name = eventNpc.icon;
  217. }
  218. else
  219. {
  220. icon.gameObject.SetActive(true);
  221. // icon.icon_name = "bg_lh_di1";
  222. }
  223. if (eventConditionConfig.EventID == 10000)
  224. {
  225. Btn_Cancel.gameObject.SetActive(false);
  226. }
  227. else
  228. {
  229. Btn_Cancel.gameObject.SetActive(true);
  230. }
  231. Btn_Bag.gameObject.SetActive(false);
  232. if (index >= dialogueMessaga.Length)
  233. {
  234. isOver = true;
  235. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  236. {
  237. ShowOptions();
  238. }
  239. // 所有句子显示完成,检查是否有选项
  240. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  241. {
  242. // ShowOptions();
  243. }
  244. else
  245. {
  246. if (eventConditionConfig.ID != 0 && _eventLinkData != null &&
  247. _eventLinkData.eventConditions.Count > 0)
  248. {
  249. foreach (var eventConditionData in _eventLinkData.eventConditions)
  250. {
  251. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  252. {
  253. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionData));
  254. return;
  255. }
  256. }
  257. }
  258. if (eventConditionConfig.optionType == 3)
  259. {
  260. Btn_Bag.gameObject.SetActive(true);
  261. List<ItemInfo> itemInfos = new List<ItemInfo>();
  262. for (var i = 0; i < eventConditionConfig.PrizeIDs.Length; i++)
  263. {
  264. DropConfig dropConfig =
  265. ConfigComponent.Instance.Get<DropConfig>(eventConditionConfig.PrizeIDs[i]);
  266. if (dropConfig.dropType == 3)
  267. {
  268. ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0],
  269. eventConditionConfig.PrizeNums[i]);
  270. itemInfos.Add(itemInfo);
  271. }
  272. else
  273. {
  274. List<ItemInfo> items = DropManager.Instance.DropItem(eventConditionConfig.PrizeIDs[i]);
  275. itemInfos.AddRange(items);
  276. }
  277. }
  278. ShowItemMoveToTargetPanel showItemMoveToTargetUiPane =
  279. UIManager.Instance.GetComponent<ShowItemMoveToTargetPanel>();
  280. Vector3 target = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  281. Btn_Bag.transform.position;
  282. Vector3 startPos = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  283. StartPos.transform.position;
  284. int maxCount = 1;
  285. foreach (var itemInfo in itemInfos)
  286. {
  287. int addValue = (int)itemInfo.count.Value / maxCount;
  288. int finishVale = (int)itemInfo.count.Value % maxCount;
  289. // ItemInfo i = itemInfo;
  290. showItemMoveToTargetUiPane.ShowPanel(startPos, target, maxCount, itemInfo.config.icon,
  291. CombatItemShowEventData.ShowType.HeroExp, (imageMove) =>
  292. {
  293. int currAddValue = addValue;
  294. if (imageMove.GroupIndex == maxCount - 1)
  295. {
  296. currAddValue += finishVale;
  297. }
  298. });
  299. }
  300. await TimerComponent.Instance.WaitAsync(2000);
  301. }
  302. // 无选项,关闭面板,返回 null
  303. UIManager.Instance.HideUIUIPanel(this);
  304. finish?.Invoke(null);
  305. }
  306. return;
  307. }
  308. _isShowNextButton = false;
  309. shownextIcon.SetActive(false);
  310. string m = LanguageManager.Instance.Text(dialogueMessaga[index], PlayerManager.Instance.PlayerName);
  311. // if (showIconName != null && index < showIconName.Length)
  312. // {
  313. // icon.icon_name = showIconName[index];
  314. // }
  315. index++;
  316. switch (messageShowType)
  317. {
  318. case ShowDialogueEventData.MessageShowType.Default:
  319. showText.text = m;
  320. ShowNextIcon();
  321. break;
  322. case ShowDialogueEventData.MessageShowType.Verbatim:
  323. _sb.Clear();
  324. _currShowMessage = m.ToCharArray();
  325. _sb.Append(_currShowMessage[0]);
  326. showText.text = _sb.ToString();
  327. _isUpdate = true;
  328. _currShowIndex = 1;
  329. _skipTyping = false;
  330. break;
  331. }
  332. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  333. }
  334. private async void ShowOptions()
  335. {
  336. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  337. _isShowingOptions = true;
  338. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  339. {
  340. OptionMarsk.SetActive(true);
  341. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  342. {
  343. DialogueOptionWidget dialogueOptionWidget =
  344. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  345. dialogueOptionWidget.CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i],
  346. eventConditionConfig.ID, SelectOption);
  347. }
  348. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  349. }
  350. }
  351. private async void SelectOption(DialogueOptionWidget obj)
  352. {
  353. if (!EventSystemManager.Instance.CeekEventGroupComplete(obj.eventLinkData.eventConditions))
  354. {
  355. bool isOk = await EventTipsPanel.OpenPanel(obj.eventLinkData);
  356. if (isOk)
  357. {
  358. UIManager.Instance.HideUIUIPanel(this);
  359. finish?.Invoke(-1);
  360. }
  361. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  362. return;
  363. }
  364. OptionMarsk.SetActive(false);
  365. int selectedOptionID = obj.eventConditionConfig.ID;
  366. UIManager.Instance.HideUIUIPanel(this);
  367. finish?.Invoke(selectedOptionID);
  368. }
  369. private void ShowNextIcon()
  370. {
  371. shownextIcon.SetActive(true);
  372. _isShowNextButton = true;
  373. }
  374. public override void Hide()
  375. {
  376. base.Hide();
  377. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  378. }
  379. public void Update()
  380. {
  381. // if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  382. // return;
  383. if (!_isUpdate) return;
  384. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  385. {
  386. if (_currShowIndex >= _currShowMessage.Length)
  387. {
  388. _isUpdate = false;
  389. ShowNextIcon();
  390. if (index >= dialogueMessaga.Length)
  391. {
  392. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  393. {
  394. ShowOptions();
  395. }
  396. }
  397. return;
  398. }
  399. _currShowTime += Time.deltaTime;
  400. if (_currShowTime > _showTime)
  401. {
  402. _currShowTime -= _showTime;
  403. _sb.Append(_currShowMessage[_currShowIndex]);
  404. _currShowIndex++;
  405. showText.text = _sb.ToString();
  406. }
  407. }
  408. }
  409. public async override CTask Close()
  410. {
  411. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  412. _isShowingOptions = false;
  413. _skipTyping = false;
  414. index = 0;
  415. eventConditionConfig = default;
  416. _eventLinkData = null;
  417. CurrentEventList = null;
  418. _currShowMessage = null;
  419. dialogueMessaga = null;
  420. // DialgueType1.gameObject.SetActive(false);
  421. // DialgueType2.gameObject.SetActive(false);
  422. // OptionMarsk.SetActive(false);
  423. isOver = false;
  424. await base.Close();
  425. }
  426. }
  427. }