EffectMgr.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Time, 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 time = 0f, float rotation = 0f)
  42. {
  43. var starttime = Time.realtimeSinceStartup;
  44. GameObject go = null;
  45. if (!effect.Name.IsNullOrWhitespace())
  46. {
  47. UnitRenderComponet render = null;
  48. if (effect.BindBody)
  49. {
  50. render = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(HostId);
  51. if (render == null)
  52. {
  53. Log.Debug($"ignore unit effect: {effect.Name}@{HostId}, cause of No Render");
  54. return 0;
  55. }
  56. }
  57. go = await GameObjectPool.Instance.Acquire("Effect_" + effect.Name);
  58. var timeComponent = go.GetComponent<EffectTime>();
  59. if (time == 0f) time = timeComponent.duration;
  60. if (time < 0f) time = EffectPlayInfo.TIME_FOREVER;
  61. setupEffect(go, effect, render, pos, rotation);
  62. }
  63. else if(!effect.SoundName.IsNullOrWhitespace())
  64. {
  65. //TODO: 音效的位置这里还有一堆逻辑的
  66. SoundManager.Instance.Play3DSound(effect.SoundName, pos).Coroutine();
  67. }
  68. if (effect.EarthQuakeMS > 0)
  69. {
  70. vecTemp.Set(effect.EarthQuakeXYZ, effect.EarthQuakeXYZ, effect.EarthQuakeXYZ);
  71. CameraMgr.ShakeMe(effect.EarthQuakeMS, vecTemp);
  72. }
  73. if (go != null)
  74. {
  75. var epi = ObjectPool.Instance.Fetch<EffectPlayInfo>();
  76. epi.Reset(go, starttime + time);
  77. playingList.Add(epi.Id, epi);
  78. return epi.Id;
  79. }
  80. return 0;
  81. }
  82. private void setupEffect(GameObject go, LaunchEffect effectData, UnitRenderComponet render, Vector3 pos, float rotation)
  83. {
  84. var gt = go.transform;
  85. if (effectData.BindBody)
  86. {
  87. if (!effectData.BindPartName.IsNullOrWhitespace())
  88. {
  89. gt.parent = render.GetBindPart(effectData.BindPartName);
  90. }
  91. else
  92. {
  93. gt.parent = render.GameObject.transform;
  94. }
  95. vecTemp.Set(effectData.EffectOffsetX, 0, effectData.EffectOffsetY);
  96. gt.localPosition = vecTemp;
  97. if (effectData.Tag == "FixStartRotaion")
  98. {
  99. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  100. }
  101. else
  102. {
  103. gt.localRotation = Quaternion.identity;
  104. }
  105. }
  106. else
  107. {
  108. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  109. gt.parent = GlobalViewComponent.Instance.Unit;
  110. }
  111. if (pos != Vector3.zero)
  112. {
  113. vecTemp.Set(effectData.EffectOffsetX, effectData.EffectHight, effectData.EffectOffsetY);
  114. gt.position = pos + vecTemp;
  115. }
  116. if (rotation != 0)
  117. {
  118. gt.rotation = RenderUtils.UnityRotationFromBattle(rotation);
  119. }
  120. var scale = effectData.ScaleToBodySize != 0 ? effectData.ScaleToBodySize : 1f;
  121. vecTemp.Set(scale, scale, scale);
  122. gt.localScale = vecTemp;
  123. if (!string.IsNullOrEmpty(effectData.Tag))
  124. {
  125. //Effect支持Tag设置Event,格式:Event:事件名(事件参数)
  126. Match m = Regex.Match(effectData.Tag, @"^Event:(\w+)\((.*)\)");
  127. if (m.Groups.Count >= 3)
  128. {
  129. string eventName = m.Groups[1].Value;
  130. string eventParam = m.Groups[2].Value;
  131. if (!string.IsNullOrEmpty(eventName))
  132. {
  133. Log.Error("Not support: Effect({0}) Triggle event:{1}", effectData.Name, effectData.Tag);
  134. /*EventManager.Fire("Event." + eventName, new Dictionary<string, object>()
  135. {
  136. {"param", eventParam},
  137. {"position", gt.position}
  138. });*/
  139. }
  140. }
  141. }
  142. go.SetActive(true);
  143. if (!effectData.SoundName.IsNullOrWhitespace())
  144. {
  145. SoundManager.Instance.Play3DSound(effectData.SoundName, gt.position).Coroutine();
  146. }
  147. }
  148. public void Clear()
  149. {
  150. foreach (var epi in playingList.Values)
  151. {
  152. GameObject.Destroy(epi.GameObj);
  153. }
  154. playingList.Clear();
  155. }
  156. public void RemoveEffect(uint id)
  157. {
  158. if (playingList.ContainsKey(id))
  159. {
  160. var info = playingList[id];
  161. playingList.Remove(id);
  162. GameObjectPool.Instance.RecycleObject(info.GameObj);
  163. }
  164. }
  165. public void ResetEffect(uint id)
  166. {
  167. if (playingList.ContainsKey(id))
  168. {
  169. //var info = playingList[id];
  170. //TODO:Reset effect
  171. }
  172. }
  173. }
  174. }