DialogueManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Common.Utility.CombatEvent;
  5. using Core.Event.Event;
  6. using Excel2Json;
  7. using Fort23.Core;
  8. using Fort23.Mono;
  9. using Fort23.UTool;
  10. using GameLogic.Bag;
  11. using GameLogic.Combat;
  12. using GameLogic.Combat.CombatTool;
  13. using GameLogic.Player;
  14. using JetBrains.Annotations;
  15. using UnityEngine;
  16. using Utility;
  17. using Random = UnityEngine.Random;
  18. public class DialogueManager : Singleton<DialogueManager>
  19. {
  20. private int currentDialogueID;
  21. private Action onDialogueComplete;
  22. private Action onCancel;
  23. private EventConfig eventConfig;
  24. private AccountFileInfo.EventList CurrentEventList;
  25. /// <summary>
  26. /// 开始对话
  27. /// </summary>
  28. public void StartDialogue(AccountFileInfo.EventList CurrentEventList, int dialogueID, int eventId, Action onComplete = null, Action onCancel = null)
  29. {
  30. this.onCancel = onCancel;
  31. this.CurrentEventList = CurrentEventList;
  32. currentDialogueID = dialogueID;
  33. onDialogueComplete = onComplete;
  34. eventConfig = ConfigComponent.Instance.Get<EventConfig>(eventId);
  35. if (eventConfig.EventType == 2)
  36. {
  37. EndDialogue1();
  38. }
  39. else
  40. {
  41. PlayDialogue(dialogueID);
  42. }
  43. // EndDialogue1();
  44. }
  45. private void UpdateDiaLogutLinkId(int dialogueID)
  46. {
  47. currentDialogueID = dialogueID;
  48. //神识探索的事件记录步骤
  49. if (EventSystemManager.Instance.CurrentEventList != null)
  50. {
  51. EventConfig eventConfig = ConfigComponent.Instance.Get<EventConfig>(EventSystemManager.Instance.CurrentEventList.eventID);
  52. if (eventConfig.EventLinksId.Contains(currentDialogueID))
  53. {
  54. EventSystemManager.Instance.CurrentEventList.curStep = currentDialogueID;
  55. //记录步骤 日志使用
  56. if (!EventSystemManager.Instance.CurrentEventList.selectEventLinkIds.Contains(currentDialogueID))
  57. EventSystemManager.Instance.CurrentEventList.selectEventLinkIds.Add(currentDialogueID);
  58. }
  59. }
  60. }
  61. private void PlayDialogue(int dialogueID)
  62. {
  63. currentDialogueID = dialogueID;
  64. UpdateDiaLogutLinkId(dialogueID);
  65. // 找到当前对话组
  66. var dialogueConfig = ConfigComponent.Instance.Get<EventLinkConfig>(dialogueID);
  67. if (CurrentEventList != null)
  68. {
  69. var eventLinkDatas = CurrentEventList.eventLinks.FirstOrDefault(el => el.eventLinkId == dialogueID);
  70. if (eventLinkDatas != null && !EventSystemManager.Instance.CeekEventGroupComplete(eventLinkDatas.eventConditions))
  71. {
  72. EventTipsPanel.OpenPanel(eventLinkDatas);
  73. onCancel?.Invoke();
  74. return;
  75. }
  76. }
  77. if (dialogueConfig.ID == 0)
  78. {
  79. EndDialogue();
  80. return;
  81. }
  82. if (dialogueConfig.NPCID != 0)
  83. {
  84. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(dialogueConfig.NPCID);
  85. if (eventNpc.ID != 0 && eventNpc.DaoyouID != 0)
  86. {
  87. if (AccountFileInfo.Instance.playerData.daoYouDatas.FirstOrDefault(dy => dy.id == eventNpc.DaoyouID) == null)
  88. {
  89. AccountFileInfo.DaoYouData daoYouData = new AccountFileInfo.DaoYouData();
  90. daoYouData.id = eventNpc.DaoyouID;
  91. daoYouData.favorabilityLv = 1;
  92. daoYouData.emotion = Random.Range(1, 4);
  93. daoYouData.emotionTime = TimeHelper.ClientNow();
  94. daoYouData.emotionValue = (int)(100 / (float)daoYouData.emotion);
  95. AccountFileInfo.Instance.playerData.daoYouDatas.Add(daoYouData);
  96. AccountFileInfo.Instance.SavePlayerData();
  97. }
  98. }
  99. }
  100. AccountFileInfo.Instance.SavePlayerData();
  101. //挂机事件 弹出简单气泡对话
  102. if (eventConfig.EventTriggerType != 2)
  103. {
  104. if (dialogueConfig.LanID != null)
  105. {
  106. DialoguePanel.OpenDialoguePanel(CurrentEventList, dialogueConfig.ID, null, ShowDialogueEventData.MessageShowType.Verbatim,
  107. FishDialogue);
  108. }
  109. else
  110. {
  111. FishDialogue(null);
  112. }
  113. }
  114. else
  115. {
  116. DialogueBubblePanel.OpenDialoguePanel(dialogueConfig.ID, FishDialogue);
  117. }
  118. }
  119. public void FishDialogue(int? selectedOptionID)
  120. {
  121. if (selectedOptionID.HasValue && selectedOptionID.Value != -1)
  122. {
  123. // 玩家选择了选项,处理结果
  124. SelectOption(selectedOptionID.Value);
  125. }
  126. //取消事件
  127. else if (selectedOptionID.HasValue && selectedOptionID.Value == -1)
  128. {
  129. onCancel?.Invoke();
  130. }
  131. else
  132. {
  133. // 无选
  134. EndDialogue1();
  135. }
  136. }
  137. /// <summary>
  138. /// 处理选项选择
  139. /// </summary>
  140. public void SelectOption(int optionID)
  141. {
  142. PlayDialogue(optionID);
  143. }
  144. /// <summary>
  145. /// 结束对话
  146. /// </summary>
  147. private async void EndDialogue1()
  148. {
  149. if (CurrentEventList == null)
  150. return;
  151. EventLinkConfig dialogueConfig = ConfigComponent.Instance.Get<EventLinkConfig>(currentDialogueID);
  152. bool isCombatWin = false;
  153. switch (dialogueConfig.optionType)
  154. {
  155. //选项在这里不处理 在ui层处理 所有这里不是走到1 直接return
  156. case 1:
  157. EndDialogue();
  158. return;
  159. //进入战斗
  160. case 2:
  161. LogTool.Log("对话结束,进入战斗");
  162. //不是一次性事件弹出boss界面
  163. if (eventConfig.EventTriggerType != 2)
  164. {
  165. bool relust = await BossInfoPanel.OpenPanel(dialogueConfig.optionPara1[0]);
  166. if (relust)
  167. {
  168. CTask cTask = CTask.Create();
  169. CombatDrive.Instance.LoadLevelBattleCombat(dialogueConfig.optionPara1[0], true,
  170. delegate(bool isWin)
  171. {
  172. LogTool.Log("战斗完成" + isWin);
  173. isCombatWin = isWin;
  174. cTask.SetResult();
  175. });
  176. await cTask;
  177. CombatDrive.Instance.CombatController.ChangeState(CombatController.idle);
  178. }
  179. else
  180. {
  181. onCancel?.Invoke();
  182. return;
  183. }
  184. }
  185. else
  186. {
  187. CTask cTask = CTask.Create();
  188. CombatDrive.Instance.LoadLevelBattleCombat(dialogueConfig.optionPara1[0], false,
  189. delegate(bool isWin)
  190. {
  191. LogTool.Log("战斗完成" + isWin);
  192. isCombatWin = isWin;
  193. cTask.SetResult();
  194. });
  195. await cTask;
  196. CombatDrive.Instance.CombatController.ChangeState(CombatController.idle);
  197. }
  198. break;
  199. //获得奖励
  200. case 3:
  201. List<ItemInfo> itemInfos = new List<ItemInfo>();
  202. for (var i = 0; i < dialogueConfig.PrizeIDs.Length; i++)
  203. {
  204. DropConfig dropConfig = ConfigComponent.Instance.Get<DropConfig>(dialogueConfig.PrizeIDs[i]);
  205. if (dropConfig.dropType == 3)
  206. {
  207. ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0], dialogueConfig.PrizeNums[i]);
  208. itemInfos.Add(itemInfo);
  209. }
  210. else
  211. {
  212. List<ItemInfo> items = DropManager.Instance.DropItem(dialogueConfig.PrizeIDs[i]);
  213. itemInfos.AddRange(items);
  214. }
  215. }
  216. await OpenRewardsPanel(eventConfig.ID, itemInfos);
  217. LogTool.Log("对话结束,获得奖励");
  218. break;
  219. // //扣除道具
  220. // case 4:
  221. // PlayerManager.Instance.BagController.DeductItem(dialogueConfig.ConditionPara[0], dialogueConfig.finishCount);
  222. // break;
  223. //关闭任务
  224. case 5:
  225. if (dialogueConfig.ResultType == 1)
  226. {
  227. UpdateDiaLogutLinkId(dialogueConfig.ResultOptions[0]);
  228. }
  229. onCancel?.Invoke();
  230. return;
  231. break;
  232. }
  233. if (CurrentEventList != null)
  234. {
  235. AccountFileInfo.EventLinkData eventLinkData = CurrentEventList.eventLinks.FirstOrDefault(el => el.eventLinkId == currentDialogueID);
  236. if (eventLinkData != null)
  237. {
  238. foreach (var eventConditionData in eventLinkData?.eventConditions)
  239. {
  240. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  241. {
  242. onCancel?.Invoke();
  243. return;
  244. }
  245. }
  246. }
  247. }
  248. //如果当前链条没有在事件链条里,不走结束逻辑
  249. if (!eventConfig.EventLinksId.Contains(currentDialogueID))
  250. {
  251. onCancel?.Invoke();
  252. return;
  253. }
  254. switch (dialogueConfig.ResultType)
  255. {
  256. //直接走下一个id
  257. case 1:
  258. PlayDialogue(dialogueConfig.ResultOptions[0]);
  259. break;
  260. //根据战斗结果判断走哪一个事件id
  261. case 2:
  262. if (isCombatWin)
  263. {
  264. PlayDialogue(dialogueConfig.ResultOptions[0]);
  265. }
  266. else
  267. {
  268. if (dialogueConfig.ResultOptions.Length > 1)
  269. {
  270. PlayDialogue(dialogueConfig.ResultOptions[1]);
  271. }
  272. else
  273. {
  274. onCancel?.Invoke();
  275. }
  276. }
  277. break;
  278. //触发新的事件 eventConfigId
  279. case 3:
  280. // if ()
  281. // {
  282. //
  283. // }
  284. AccountFileInfo.EventList eventList = EventSystemManager.Instance.AddEvent(dialogueConfig.ResultOptions[0]);
  285. if (eventList != null)
  286. {
  287. AccountFileInfo.Instance.playerData.eventList.Add(eventList);
  288. AccountFileInfo.Instance.SavePlayerData();
  289. }
  290. EndDialogue();
  291. break;
  292. //结束事件
  293. default:
  294. // if (eventConfig.EventLinksId.Contains(dialogueConfig.ID))
  295. {
  296. EndDialogue();
  297. }
  298. break;
  299. }
  300. }
  301. /// <summary>
  302. /// 结束对话
  303. /// </summary>
  304. private void EndDialogue()
  305. {
  306. onDialogueComplete?.Invoke();
  307. }
  308. public async CTask OpenRewardsPanel(int eventId, List<ItemInfo> rewardsDic, Action onClose = null, int titleId = 0)
  309. {
  310. EventConfig eventConfig = ConfigComponent.Instance.Get<EventConfig>(eventId);
  311. if (eventConfig.EventTriggerType == 2)
  312. {
  313. Vector3 worldPos = CombatDrive.Instance.CombatController.playerHeroEntity.combatHeroGameObject.hpTransform.position;
  314. worldPos.y += 0.5f;
  315. await ShowItemNumberCom.Open(worldPos, rewardsDic[0]);
  316. await TimerComponent.Instance.WaitAsync(100);
  317. }
  318. else
  319. {
  320. foreach (var itemInfo in rewardsDic)
  321. {
  322. CurrentEventList.itemInfos.Add(itemInfo.ToItemData());
  323. }
  324. AccountFileInfo.Instance.SavePlayerData();
  325. // RewardsPanel rewardsPanel = await RewardsPanel.OpenPanel(rewardsDic, onClose, titleId);
  326. // await rewardsPanel.UIClosed();
  327. }
  328. }
  329. public void ChancleEvent()
  330. {
  331. CurrentEventList = null;
  332. onDialogueComplete = null;
  333. onCancel = null;
  334. currentDialogueID = 0;
  335. eventConfig = default;
  336. UIManager.Instance.HideUIUIPanel<DialogueBubblePanel>();
  337. }
  338. }