DialoguePanel.cs 18 KB

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