DialoguePanel.cs 18 KB

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