EntitiesComponent.Update.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using FL.Battle.Buffs;
  2. using System;
  3. using System.Collections.Generic;
  4. using XGame.Framework;
  5. using XGame.Framework.Interfaces;
  6. namespace FL.Map.TestMainMap
  7. {
  8. public partial class EntitiesComponent : ILateUpdate
  9. {
  10. private HashSet<long> _buffOwnerIds;
  11. private bool _isElementBuffDirty;
  12. private BuffPreviewDto _buffPreview;
  13. void ILateUpdate.LateUpdate(int millisecond)
  14. {
  15. Context.VM.UpdateMapMoveTime(millisecond);
  16. Context.VM.Camera.OnLateUpdate(millisecond);
  17. RefreshElementBuffs();
  18. }
  19. #region 元素buff
  20. private void RefreshElementBuffs()
  21. {
  22. if (!_isElementBuffDirty) return;
  23. _isElementBuffDirty = false;
  24. CollectElementBuffs((elementBuffs, partners) =>
  25. {
  26. var isMatch = false;
  27. if (elementBuffs != null)
  28. {
  29. foreach (var buff in elementBuffs)
  30. {
  31. foreach (var partner in partners)
  32. {
  33. if (MatchTargetBuff(partner, buff))
  34. {
  35. isMatch = true;
  36. break;
  37. }
  38. }
  39. if (isMatch)
  40. {
  41. break;
  42. }
  43. }
  44. }
  45. if (!isMatch)
  46. { // 没有匹配的项
  47. ResetBuffPreview();
  48. }
  49. });
  50. }
  51. private void CollectElementBuffs(Action<List<IBuff>, List<PartnerEntityView>> callback)
  52. {
  53. if (_buffOwnerIds == null || _buffOwnerIds.Count == 0)
  54. {
  55. callback.SafeInvoke(null, null);
  56. return;
  57. }
  58. var buffOwnerIds = ListPool.Acquire<long>();
  59. buffOwnerIds.AddRange(_buffOwnerIds);
  60. var elementBuffs = ListPool.Acquire<IBuff>();
  61. foreach (var ownerId in buffOwnerIds)
  62. {
  63. if (_monsterMap.TryGetValue(ownerId, out var monster) &&
  64. monster.IsDead == false &&
  65. monster.Buffs.GetElementBuffs(ref elementBuffs))
  66. {
  67. continue;
  68. }
  69. //非怪物 || 没有满足要求的元素buff
  70. _buffOwnerIds.Remove(ownerId);
  71. }
  72. elementBuffs.Sort((a, b) =>
  73. { // 层数高的排前面
  74. var orderA = a.Layers * 1000 / a.LayerLimit;
  75. var orderB = b.Layers * 1000 / b.LayerLimit;
  76. return orderB - orderA;
  77. });
  78. // 随从 按照技能冷却时间排序
  79. var partners = ListPool.Acquire<PartnerEntityView>();
  80. partners.AddRange(_partnerMap.Values);
  81. var skillCdMap = new Dictionary<long, int>();
  82. partners.Sort((a, b) =>
  83. {
  84. var id_a = a.Entity.EntityId;
  85. if (!skillCdMap.TryGetValue(id_a, out var cd_a))
  86. {
  87. cd_a = a.Skill.GetMinCooldown();
  88. skillCdMap.Add(id_a, cd_a);
  89. }
  90. var id_b = b.Entity.EntityId;
  91. if (!skillCdMap.TryGetValue(id_b, out var cd_b))
  92. {
  93. cd_b = a.Skill.GetMinCooldown();
  94. skillCdMap.Add(id_b, cd_b);
  95. }
  96. // 冷却时间短的排前面
  97. return cd_a - cd_b;
  98. });
  99. //回调
  100. callback.SafeInvoke(elementBuffs, partners);
  101. //回收临时变量
  102. ListPool.Recycle(partners);
  103. ListPool.Recycle(buffOwnerIds);
  104. ListPool.Recycle(elementBuffs);
  105. }
  106. private bool MatchTargetBuff(PartnerEntityView partner, IBuff buff)
  107. {
  108. if (partner.Epigraphics.IsTargetBuff(buff.TableId, out var epigrapId))
  109. {
  110. NotifyBuffPreview(buff, partner.Entity.Attr.UID, false, epigrapId);
  111. return true;
  112. }
  113. if (partner.Skill.FindSkillByElementType(buff.ElementType, out var skillId))
  114. {
  115. NotifyBuffPreview(buff, partner.Entity.Attr.UID, true, skillId);
  116. return true;
  117. }
  118. return false;
  119. }
  120. private void NotifyBuffPreview(IBuff buff, long partnerUID, bool isSkill, int skillId)
  121. {
  122. _buffPreview = new BuffPreviewDto()
  123. {
  124. ownerId = buff.OwnerId,
  125. buffTableId = buff.TableId,
  126. partnerUID = partnerUID,
  127. isSkill = isSkill,
  128. skillId = skillId,
  129. layer = buff.Layers,
  130. layerLimit = buff.LayerLimit,
  131. };
  132. EventSingle.Instance.Notify(EventDefine.GameMainMapPreviewBuff, _buffPreview);
  133. }
  134. private void ResetBuffPreview()
  135. {
  136. _buffPreview = default;
  137. EventSingle.Instance.Notify(EventDefine.GameMainMapPreviewBuff, _buffPreview);
  138. }
  139. private void OnAddBuff(int eventId, object args)
  140. {
  141. var dto = (BuffEventDto)args;
  142. var buffOwners = _buffOwnerIds ??= new HashSet<long>();
  143. buffOwners.Add(dto.ownerId);
  144. _isElementBuffDirty = true;
  145. }
  146. private void OnRemoveBuff(int eventId, object args)
  147. {
  148. var dto = (BuffEventDto)args;
  149. if (_buffPreview.ownerId == dto.ownerId && _buffPreview.buffTableId == dto.buffTableId)
  150. {
  151. ResetBuffPreview();
  152. _isElementBuffDirty = true;
  153. }
  154. }
  155. private void OnRequestBuffList(int eventId, object args)
  156. {
  157. var buffPreviewDtos = new List<BuffPreviewDto>();
  158. CollectElementBuffs((elementBuffs, partners) =>
  159. {
  160. if (elementBuffs != null)
  161. {
  162. foreach (var buff in elementBuffs)
  163. {
  164. foreach (var partner in partners)
  165. {
  166. var partnerUID = partner.Entity.Attr.UID;
  167. if (partner.Epigraphics.IsTargetBuff(buff.TableId, out var epigrapId))
  168. {
  169. var dto = new BuffPreviewDto()
  170. {
  171. ownerId = buff.OwnerId,
  172. buffTableId = buff.TableId,
  173. partnerUID = partnerUID,
  174. isSkill = false,
  175. skillId = epigrapId,
  176. layer = buff.Layers,
  177. layerLimit = buff.LayerLimit,
  178. };
  179. buffPreviewDtos.Add(dto);
  180. }
  181. if (partner.Skill.FindSkillByElementType(buff.ElementType, out var skillId))
  182. {
  183. var dto = new BuffPreviewDto()
  184. {
  185. ownerId = buff.OwnerId,
  186. buffTableId = buff.TableId,
  187. partnerUID = partnerUID,
  188. isSkill = true,
  189. skillId = skillId,
  190. layer = buff.Layers,
  191. layerLimit = buff.LayerLimit,
  192. };
  193. buffPreviewDtos.Add(dto);
  194. }
  195. }
  196. }
  197. }
  198. EventSingle.Instance.Notify(EventDefine.GameMainMapPushPreviewBuffs, buffPreviewDtos);
  199. });
  200. }
  201. private void RemoveElementBuff(long ownerId)
  202. {
  203. if (_buffPreview.ownerId == ownerId)
  204. {
  205. ResetBuffPreview();
  206. }
  207. if (_buffOwnerIds?.Remove(ownerId) ?? false)
  208. {
  209. _isElementBuffDirty = true;
  210. }
  211. }
  212. private void ClearElementBuffs()
  213. {
  214. _buffOwnerIds?.Clear();
  215. _isElementBuffDirty = false;
  216. _buffPreview = default;
  217. }
  218. #endregion
  219. }
  220. }