EffectMgr.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using CommonAI.Zone;
  2. using UnityEngine;
  3. using System.Text.RegularExpressions;
  4. using CommonLang;
  5. using Sirenix.Utilities;
  6. using ET.EventType;
  7. using System.Collections.Generic;
  8. namespace ET.Client
  9. {
  10. [Event(SceneType.None)]
  11. public class PlayEffectEventHandler : BEvent<EventType.PlayEffectEvent>
  12. {
  13. private static Vector3 vecTemp = new();
  14. public override void OnEvent(PlayEffectEvent args)
  15. {
  16. vecTemp.Set(args.Pos.X, args.Pos.Y, args.Pos.Z);
  17. EffectMgr.Instance.PlayEffect(args.Effect, args.HostId, vecTemp, args.Rotation).Coroutine();
  18. }
  19. }
  20. public class EffectMgr : Singleton<EffectMgr>, ISingletonUpdate
  21. {
  22. private HashMap<uint, EffectPlayInfo> playingList = new();
  23. private static Vector3 vecTemp = new();
  24. private List<uint> listTemp = new();
  25. public void Update(int timeMS)
  26. {
  27. listTemp.Clear();
  28. var now = Time.realtimeSinceStartup;
  29. foreach (var epi in playingList.Values)
  30. {
  31. if (epi.EndTime <= now)
  32. {
  33. listTemp.Add(epi.Id);
  34. }
  35. }
  36. foreach(var id in listTemp)
  37. {
  38. RemoveEffect(id);
  39. }
  40. }
  41. public async ETTask<uint> PlayEffect(LaunchEffect effect, uint HostId, Vector3 pos, float rotation = 0f)
  42. {
  43. var starttime = Time.realtimeSinceStartup;
  44. float time = EffectPlayInfo.TIME_DEFAULT;
  45. GameObject go = null;
  46. if (!effect.Name.IsNullOrWhitespace())
  47. {
  48. UnitRenderComponet render = null;
  49. if (effect.BindBody)
  50. {
  51. render = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(HostId);
  52. if (render == null)
  53. {
  54. Log.Debug($"ignore unit effect: {effect.Name}@{HostId}, cause of No Render");
  55. return 0;
  56. }
  57. }
  58. go = await GameObjectPool.Instance.Acquire("Effect_" + effect.Name);
  59. if (effect.EffectTimeMS > 0)
  60. {
  61. time = effect.EffectTimeMS / 1000.0f;
  62. }
  63. else if (effect.EffectTimeMS < 0)
  64. {
  65. time = EffectPlayInfo.TIME_FOREVER;
  66. }
  67. else
  68. {
  69. var timeComponent = go.GetComponent<EffectTime>();
  70. if (timeComponent != null && timeComponent.duration > 0)
  71. {
  72. time = timeComponent.duration;
  73. }
  74. }
  75. setupEffect(go, effect, render, pos, rotation);
  76. }
  77. else if(!effect.SoundName.IsNullOrWhitespace())
  78. {
  79. //TODO: 音效的位置这里还有一堆逻辑的
  80. SoundManager.Instance.Play3DSound(effect.SoundName, pos).Coroutine();
  81. }
  82. if (effect.EarthQuakeMS > 0)
  83. {
  84. vecTemp.Set(effect.EarthQuakeXYZ, effect.EarthQuakeXYZ, effect.EarthQuakeXYZ);
  85. CameraMgr.ShakeMe(effect.EarthQuakeMS, vecTemp);
  86. }
  87. if (go != null)
  88. {
  89. var epi = ObjectPool.Instance.Fetch<EffectPlayInfo>();
  90. epi.Reset(go, starttime + time);
  91. playingList.Add(epi.Id, epi);
  92. return epi.Id;
  93. }
  94. return 0;
  95. }
  96. private void setupEffect(GameObject go, LaunchEffect effectData, UnitRenderComponet render, Vector3 pos, float rotation)
  97. {
  98. var gt = go.transform;
  99. if (effectData.BindBody)
  100. {
  101. if (!effectData.BindPartName.IsNullOrWhitespace())
  102. {
  103. gt.parent = render.GetBindPart(effectData.BindPartName);
  104. }
  105. else
  106. {
  107. gt.parent = render.GameObject.transform;
  108. }
  109. vecTemp.Set(effectData.EffectOffsetX, 0, effectData.EffectOffsetY);
  110. gt.localPosition = vecTemp;
  111. if (effectData.Tag == "FixStartRotaion")
  112. {
  113. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  114. }
  115. else
  116. {
  117. gt.localRotation = Quaternion.identity;
  118. }
  119. }
  120. else
  121. {
  122. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  123. gt.parent = GlobalViewComponent.Instance.Unit;
  124. }
  125. if (pos != Vector3.zero)
  126. {
  127. vecTemp.Set(effectData.EffectOffsetX, effectData.EffectHight, effectData.EffectOffsetY);
  128. gt.position = RenderUtils.UnityPosFromBattle(pos + vecTemp);
  129. }
  130. if (rotation != 0)
  131. {
  132. gt.rotation = RenderUtils.UnityRotationFromBattle(rotation);
  133. }
  134. var scale = effectData.ScaleToBodySize != 0 ? effectData.ScaleToBodySize : 1f;
  135. vecTemp.Set(scale, scale, scale);
  136. gt.localScale = vecTemp;
  137. if (!string.IsNullOrEmpty(effectData.Tag))
  138. {
  139. //Effect支持Tag设置Event,格式:Event:事件名(事件参数)
  140. Match m = Regex.Match(effectData.Tag, @"^Event:(\w+)\((.*)\)");
  141. if (m.Groups.Count >= 3)
  142. {
  143. string eventName = m.Groups[1].Value;
  144. string eventParam = m.Groups[2].Value;
  145. if (!string.IsNullOrEmpty(eventName))
  146. {
  147. Log.Error("Not support: Effect({0}) Triggle event:{1}", effectData.Name, effectData.Tag);
  148. /*EventManager.Fire("Event." + eventName, new Dictionary<string, object>()
  149. {
  150. {"param", eventParam},
  151. {"position", gt.position}
  152. });*/
  153. }
  154. }
  155. }
  156. go.SetActive(true);
  157. if (!effectData.SoundName.IsNullOrWhitespace())
  158. {
  159. SoundManager.Instance.Play3DSound(effectData.SoundName, gt.position).Coroutine();
  160. }
  161. }
  162. public void Clear()
  163. {
  164. foreach (var epi in playingList.Values)
  165. {
  166. GameObject.Destroy(epi.GameObj);
  167. }
  168. playingList.Clear();
  169. }
  170. public void RemoveEffect(uint id)
  171. {
  172. if (playingList.ContainsKey(id))
  173. {
  174. var info = playingList[id];
  175. playingList.Remove(id);
  176. GameObjectPool.Instance.RecycleObject(info.GameObj);
  177. }
  178. }
  179. public void ResetEffect(uint id)
  180. {
  181. if (playingList.ContainsKey(id))
  182. {
  183. //var info = playingList[id];
  184. //TODO:Reset effect
  185. }
  186. }
  187. }
  188. }