VfxComponent.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame.Framework;
  5. using XGame.Framework.Asyncs;
  6. using XGame.Framework.Components;
  7. using XGame.Framework.Interfaces;
  8. using XGame.Framework.Time;
  9. namespace FL.Battle.Vfxs
  10. {
  11. /// <summary>
  12. /// 特效播放组件
  13. /// TODO 世界跟随类型需要将特效转到地图播放,以防止角色死亡时特效播放被打断
  14. /// </summary>
  15. public class VfxComponent : Component<IVfxContext>
  16. {
  17. class VfxPlayingDto : IReference
  18. {
  19. public IAsync loadAsync;
  20. public ITimer timer;
  21. public GameObject vfxGo;
  22. void IReference.Clear()
  23. {
  24. loadAsync = null;
  25. timer = null;
  26. vfxGo = null;
  27. }
  28. }
  29. public override int Order => 0;
  30. private Dictionary<VfxPlayingDto, VfxArgs> _playingVfxMap = new Dictionary<VfxPlayingDto, VfxArgs>();
  31. protected override void OnEnable(object intent)
  32. {
  33. if (Context.WorldTr != null)
  34. {
  35. EventSingle.Instance.AddListener(EventDefine.VfxComponentWorldVfx, OnWorldVfx);
  36. }
  37. }
  38. protected override void OnDisable()
  39. {
  40. if (Context.WorldTr != null)
  41. {
  42. EventSingle.Instance.RemoveListener(EventDefine.VfxComponentWorldVfx, OnWorldVfx);
  43. }
  44. }
  45. protected override void OnDispose()
  46. {
  47. foreach (var item in _playingVfxMap)
  48. {
  49. RecycleDto(item.Key, false);
  50. RecycleArgs(item.Value);
  51. }
  52. _playingVfxMap.Clear();
  53. }
  54. public void Play(VfxArgs args)
  55. {
  56. Assert.IsTrue(args != null, "特效播放参数不能为空.");
  57. Assert.IsFalse(string.IsNullOrEmpty(args.vfxName), "特效名字不能为空.");
  58. Assert.IsTrue(args.duration > 0, "特效播放时长必须大于零.");
  59. if (args.followType == EVfxFollowType.World && Context.WorldTr == null)
  60. {
  61. EventSingle.Instance.Notify(EventDefine.VfxComponentWorldVfx, args);
  62. return;
  63. }
  64. var dto = AcquireDto(args);
  65. void DelayPlay()
  66. {
  67. var loadAsync = Context.Asset.LoadAsync<GameObject>(args.vfxName);
  68. dto.loadAsync = loadAsync;
  69. loadAsync.On(_ =>
  70. {
  71. dto.loadAsync = null;
  72. var vfxGo = loadAsync.GetResult();
  73. if (vfxGo == null)
  74. {
  75. RecycleDto(dto);
  76. args.onFinish.SafeInvoke();
  77. RecycleArgs(args);
  78. return;
  79. }
  80. dto.vfxGo = vfxGo;
  81. SetParent(vfxGo.transform, args.followType);
  82. vfxGo.SetActive(true);
  83. args.action?.Start(vfxGo.transform);
  84. dto.timer = Context.Time.AddDelayTimer(args.duration, () =>
  85. {
  86. dto.timer = null;
  87. dto.vfxGo = null;
  88. //回收GameObject
  89. Context.Asset.Recycle(vfxGo);
  90. //回收dto
  91. RecycleDto(dto);
  92. args.onFinish.SafeInvoke();
  93. var next = args.next;
  94. if (next != null)
  95. {
  96. args.next = null;
  97. //播放下一个
  98. Play(next);
  99. }
  100. RecycleArgs(args);
  101. });
  102. });
  103. }
  104. if (args.delay > 0)
  105. {
  106. dto.timer = Context.Time.AddDelayTimer(args.delay, () =>
  107. {
  108. dto.timer = null;
  109. DelayPlay();
  110. });
  111. }
  112. else
  113. {
  114. DelayPlay();
  115. }
  116. }
  117. private void SetParent(Transform vfxTr, EVfxFollowType followType)
  118. {
  119. Transform parent;
  120. switch (followType)
  121. {
  122. case EVfxFollowType.World:
  123. parent = Context.WorldTr;
  124. break;
  125. case EVfxFollowType.CastPoint:
  126. parent = Context.CastTr != null ? Context.CastTr : Context.CenterTr;
  127. break;
  128. default:
  129. parent = Context.CenterTr;
  130. break;
  131. }
  132. vfxTr.SetParent(parent, false);
  133. if (followType != EVfxFollowType.World)
  134. {
  135. vfxTr.localPosition = Vector3.zero;
  136. }
  137. //TODO 技能特效需要缩小尺寸
  138. vfxTr.localScale = Vector3.one * 0.7f;
  139. }
  140. //public VfxArgs AcquireArgs()
  141. //{
  142. // return ObjectPool.Acquire<VfxArgs>();
  143. //}
  144. public void RecycleArgs(VfxArgs args)
  145. {
  146. if (args == null)
  147. return;
  148. if (args.next != null)
  149. {
  150. RecycleArgs(args.next);
  151. }
  152. if (args.action != null)
  153. {
  154. ObjectPool.Recycle(args.action);
  155. args.action = null;
  156. }
  157. ObjectPool.Recycle(args);
  158. }
  159. private VfxPlayingDto AcquireDto(VfxArgs args)
  160. {
  161. var dto = ObjectPool.Acquire<VfxPlayingDto>();
  162. _playingVfxMap.Add(dto, args);
  163. return dto;
  164. }
  165. private void RecycleDto(VfxPlayingDto dto, bool removeMap = true)
  166. {
  167. dto.loadAsync?.RemoveAll();
  168. dto.timer?.Cancel();
  169. if (dto.vfxGo != null)
  170. {
  171. Context.Asset.Recycle(dto.vfxGo);
  172. }
  173. if (removeMap)
  174. {
  175. _playingVfxMap.Remove(dto);
  176. }
  177. ObjectPool.Recycle(dto);
  178. }
  179. #region 事件监听
  180. private void OnWorldVfx(int eventId, object args)
  181. {
  182. var vfxArgs = args as VfxArgs;
  183. Play(vfxArgs);
  184. }
  185. #endregion
  186. }
  187. }