EventSystemManager.cs 4.9 KB

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