DialoguePanel.cs 19 KB

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