EffectMgr.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (effect.EarthQuakeMS > 0)
  64. {
  65. vecTemp.Set(effect.EarthQuakeXYZ, effect.EarthQuakeXYZ, effect.EarthQuakeXYZ);
  66. CameraMgr.ShakeMe(effect.EarthQuakeMS, vecTemp);
  67. }
  68. if (!effect.SoundName.IsNullOrWhitespace())
  69. {
  70. //TODO: Sound
  71. }
  72. if (go != null)
  73. {
  74. var epi = ObjectPool.Instance.Fetch<EffectPlayInfo>();
  75. epi.Reset(go, starttime + time);
  76. playingList.Add(epi.Id, epi);
  77. return epi.Id;
  78. }
  79. return 0;
  80. }
  81. private void setupEffect(GameObject go, LaunchEffect effectData, UnitRenderComponet render, Vector3 pos, float rotation)
  82. {
  83. var gt = go.transform;
  84. if (effectData.BindBody)
  85. {
  86. if (!effectData.BindPartName.IsNullOrWhitespace())
  87. {
  88. gt.parent = render.GetBindPart(effectData.BindPartName);
  89. }
  90. else
  91. {
  92. gt.parent = render.GameObject.transform;
  93. }
  94. vecTemp.Set(effectData.EffectOffsetX, 0, effectData.EffectOffsetY);
  95. gt.localPosition = vecTemp;
  96. if (effectData.Tag == "FixStartRotaion")
  97. {
  98. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  99. }
  100. else
  101. {
  102. gt.localRotation = Quaternion.identity;
  103. }
  104. }
  105. else
  106. {
  107. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  108. gt.parent = GlobalViewComponent.Instance.Unit;
  109. }
  110. if (pos != Vector3.zero)
  111. {
  112. vecTemp.Set(effectData.EffectOffsetX, effectData.EffectHight, effectData.EffectOffsetY);
  113. gt.position = pos + vecTemp;
  114. }
  115. if (rotation != 0)
  116. {
  117. gt.rotation = RenderUtils.UnityRotationFromBattle(rotation);
  118. }
  119. var scale = effectData.ScaleToBodySize != 0 ? effectData.ScaleToBodySize : 1f;
  120. vecTemp.Set(scale, scale, scale);
  121. gt.localScale = vecTemp;
  122. if (!string.IsNullOrEmpty(effectData.Tag))
  123. {
  124. //Effect支持Tag设置Event,格式:Event:事件名(事件参数)
  125. Match m = Regex.Match(effectData.Tag, @"^Event:(\w+)\((.*)\)");
  126. if (m.Groups.Count >= 3)
  127. {
  128. string eventName = m.Groups[1].Value;
  129. string eventParam = m.Groups[2].Value;
  130. if (!string.IsNullOrEmpty(eventName))
  131. {
  132. Log.Error("Not support: Effect({0}) Triggle event:{1}", effectData.Name, effectData.Tag);
  133. /*EventManager.Fire("Event." + eventName, new Dictionary<string, object>()
  134. {
  135. {"param", eventParam},
  136. {"position", gt.position}
  137. });*/
  138. }
  139. }
  140. }
  141. go.SetActive(true);
  142. }
  143. public void Clear()
  144. {
  145. foreach (var epi in playingList.Values)
  146. {
  147. GameObject.Destroy(epi.GameObj);
  148. }
  149. playingList.Clear();
  150. }
  151. public void RemoveEffect(uint id)
  152. {
  153. if (playingList.ContainsKey(id))
  154. {
  155. var info = playingList[id];
  156. playingList.Remove(id);
  157. GameObjectPool.Instance.RecycleObject(info.GameObj);
  158. }
  159. }
  160. }
  161. }