EventSystemManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Excel2Json;
  4. using Fort23.Core;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using Utility;
  8. using Random = UnityEngine.Random;
  9. public class EventSystemManager : Singleton<EventSystemManager>
  10. {
  11. List<EventConfig> eventConfigs = new List<EventConfig>();
  12. List<AccountFileInfo.EventData> eventDatas = new List<AccountFileInfo.EventData>();
  13. public void CustomInit()
  14. {
  15. eventConfigs = ConfigComponent.Instance.GetAll<EventConfig>().ToList();
  16. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  17. }
  18. private float timer = 0;
  19. private void Update()
  20. {
  21. timer += Time.deltaTime;
  22. if (timer > 1)
  23. {
  24. timer = 0;
  25. AccountFileInfo.Instance.playerData.divineSensePoint += 3;
  26. EventManager.Instance.Dispatch(CustomEventType.DivineSensePointChange, null);
  27. AccountFileInfo.Instance.SavePlayerData();
  28. }
  29. }
  30. public List<EventConfig> UseDivinesense(int bigMapId)
  31. {
  32. DivineSenseConfig divineSenseConfig = ConfigComponent.Instance.Get<DivineSenseConfig>(AccountFileInfo.Instance.playerData.divineSenseLevel);
  33. int count = AccountFileInfo.Instance.playerData.divineSensePoint / PlayerManager.Instance.gameConstantConfig.DetectEventCount;
  34. if (count <= 0)
  35. {
  36. //神识值不够
  37. return null;
  38. }
  39. AccountFileInfo.Instance.playerData.divineSenseexp += AccountFileInfo.Instance.playerData.divineSensePoint;
  40. AccountFileInfo.Instance.playerData.divineSensePoint = 0;
  41. EventManager.Instance.Dispatch(CustomEventType.DivineSensePointChange, null);
  42. //todo 神识升级逻辑
  43. AccountFileInfo.Instance.SavePlayerData();
  44. return DetectEvents(bigMapId, count);
  45. }
  46. private List<EventConfig> DetectEvents(int bigMapId, int eventCount)
  47. {
  48. BigMap bigMap = ConfigComponent.Instance.Get<BigMap>(bigMapId);
  49. DivineSenseConfig divineSenseConfig = ConfigComponent.Instance.Get<DivineSenseConfig>(AccountFileInfo.Instance.playerData.divineSenseLevel);
  50. List<int> qualitys = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  51. //先掉落出品质
  52. int quality = UtilTools.GetRandomByWeight(qualitys, divineSenseConfig.QualityBonusChance);
  53. // 获取通用事件
  54. List<EventConfig> globalEvents = eventConfigs.Where(e => e.EventTriggerType == 1 && CanTriggerEvent(e.ID) && e.EventQuality == quality).ToList();
  55. //神识场景事件
  56. List<EventConfig> candidateEvents = eventConfigs.Where(e => bigMap.DivineSenseGeneralEvent.Contains(e.ID) && CanTriggerEvent(e.ID) && e.EventQuality == quality).ToList();
  57. if (candidateEvents.Count == 0 && globalEvents.Count == 0)
  58. {
  59. LogTool.Error("没有可以刷新的事件");
  60. return default;
  61. }
  62. var triggeredEvents = new List<EventConfig>();
  63. for (int i = 0; i < eventCount; i++)
  64. {
  65. int randomValue1 = Random.Range(0, 101);
  66. //刷场景事件
  67. if (randomValue1 >= bigMap.DivineSenseGeneralProbability && candidateEvents.Count > 0)
  68. {
  69. int randomValue2 = Random.Range(0, candidateEvents.Count);
  70. triggeredEvents.Add(candidateEvents[randomValue2]);
  71. candidateEvents.RemoveAll(ew => ew.ID == candidateEvents[randomValue2].ID);
  72. }
  73. else if (globalEvents.Count > 0)
  74. {
  75. int randomValue2 = Random.Range(0, globalEvents.Count);
  76. triggeredEvents.Add(globalEvents[randomValue2]);
  77. globalEvents.RemoveAll(ew => ew.ID == globalEvents[randomValue2].ID);
  78. }
  79. }
  80. if (triggeredEvents.Count == 0)
  81. {
  82. LogTool.Error("没有可以触发的事件");
  83. return default;
  84. }
  85. return triggeredEvents;
  86. }
  87. /// <summary>
  88. /// 触发随机事件
  89. /// </summary>
  90. public void DetectRandomEvents()
  91. {
  92. // 获取随机事件
  93. List<EventConfig> randomEvents = eventConfigs.Where(e => e.EventTriggerType == 2 && CanTriggerEvent(e.ID)).ToList();
  94. if (randomEvents.Count == 0)
  95. {
  96. LogTool.Warning("没有可以刷新的事件");
  97. return;
  98. }
  99. int idex = Random.Range(1, randomEvents.Count);
  100. TriggerEvent(randomEvents[idex]);
  101. }
  102. /// <summary>
  103. /// 触发事件
  104. /// </summary>
  105. public void TriggerEvent(EventConfig evt)
  106. {
  107. LogTool.Log($"触发事件: {evt.Description} (ID: {evt.ID}, 品质: {evt.EventQuality})");
  108. DialogueManager.Instance.StartDialogue(evt.EventType, evt.ID, () => { CompleteEvent(evt.ID); });
  109. }
  110. /// <summary>
  111. /// 检查事件是否满足触发条件。
  112. /// </summary>
  113. private bool CanTriggerEvent(int eventID)
  114. {
  115. var evt = eventConfigs.Find(e => e.ID == eventID);
  116. if (evt.ID == 0)
  117. return false;
  118. for (var i = 0; i < evt.EventConditionId?.Length; i++)
  119. {
  120. if (!CheckCondition(evt.EventConditionId[i], evt.EventVlaue[i]))
  121. return false;
  122. }
  123. return true;
  124. }
  125. /// <summary>
  126. /// 检查单个触发条件。
  127. /// </summary>
  128. private bool CheckCondition(int conditionid, int EventVlaue)
  129. {
  130. //>=
  131. if (EventVlaue == 1)
  132. {
  133. }
  134. //=
  135. else if (EventVlaue == 2)
  136. {
  137. }
  138. // <=
  139. else if (EventVlaue == 3)
  140. {
  141. }
  142. return true;
  143. }
  144. /// <summary>
  145. /// 完成事件
  146. /// </summary>
  147. /// <param name="eventID">事件ID</param>
  148. public void CompleteEvent(int eventID)
  149. {
  150. LogTool.Log($"完成事件{eventID}");
  151. AccountFileInfo.EventData eventData = AccountFileInfo.Instance.playerData.events.FirstOrDefault((e) => e.eventID == eventID);
  152. if (eventData == null)
  153. {
  154. eventData = new AccountFileInfo.EventData();
  155. eventData.eventID = eventID;
  156. }
  157. eventData.completeCount++;
  158. AccountFileInfo.Instance.playerData.events.Add(eventData);
  159. if (AccountFileInfo.Instance.playerData.senceExplorationProgress < 100)
  160. AccountFileInfo.Instance.playerData.senceExplorationProgress += 5;
  161. AccountFileInfo.Instance.SavePlayerData();
  162. }
  163. }