DialoguePanel.cs 19 KB

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