EffectMgr.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. using System;
  9. namespace ET.Client
  10. {
  11. [Event(SceneType.None)]
  12. public class PlayEffectEventHandler : BEvent<EventType.PlayEffectEvent>
  13. {
  14. private static Vector3 vecTemp = new();
  15. public override void OnEvent(PlayEffectEvent args)
  16. {
  17. vecTemp.Set(args.Pos.X, args.Pos.Y, args.Pos.Z);
  18. EffectMgr.Instance.PlayEffect(args.Effect, args.HostId, vecTemp, args.Rotation).Coroutine();
  19. var tag = args.Effect.Tag;
  20. if (!tag.IsNullOrWhitespace())
  21. {
  22. ProcessLauncheffectTag(args.HostId, args.Effect.Tag).Coroutine();
  23. }
  24. }
  25. public async ETTask ProcessLauncheffectTag(uint objectid, string tag)
  26. {
  27. //Effect支持Tag设置Event,格式:Event:事件名(事件参数)
  28. Match m = Regex.Match(tag, @"^@(\w+)\(([\w\d\s_,]+)\)");
  29. if (m.Groups.Count >= 3)
  30. {
  31. string name = m.Groups[1].Value;
  32. string param = m.Groups[2].Value;
  33. switch (name)
  34. {
  35. case "ChangeMode":
  36. var ps = param.Split(',');
  37. if (ps.Length > 1)
  38. {
  39. try
  40. {
  41. var delay = Convert.ToInt32(ps[1]);
  42. await TimerComponent.Instance.WaitAsync(delay);
  43. EventSystem.Instance.Publish(ChangeModeEvent.Static.Clone(objectid, ps[0]));
  44. }
  45. catch
  46. {
  47. Log.Error($"effect tag param error: {tag}");
  48. }
  49. }
  50. else
  51. {
  52. EventSystem.Instance.Publish(ChangeModeEvent.Static.Clone(objectid, param));
  53. }
  54. return;
  55. default:
  56. Log.Error($"effect unknow tag: {tag}");
  57. return;
  58. }
  59. }
  60. Log.Error($"effect illegal tag: {tag}");
  61. /*if (!string.IsNullOrEmpty(effectData.Tag))
  62. {
  63. //Effect支持Tag设置Event,格式:Event:事件名(事件参数)
  64. Match m = Regex.Match(effectData.Tag, @"^Event:(\w+)\((.*)\)");
  65. if (m.Groups.Count >= 3)
  66. {
  67. string eventName = m.Groups[1].Value;
  68. string eventParam = m.Groups[2].Value;
  69. if (!string.IsNullOrEmpty(eventName))
  70. {
  71. Log.Error("Not support: Effect({0}) Triggle event:{1}", effectData.Name, effectData.Tag);
  72. EventManager.Fire("Event." + eventName, new Dictionary<string, object>()
  73. {
  74. {"param", eventParam},
  75. {"position", gt.position}
  76. });
  77. }
  78. }
  79. }*/
  80. }
  81. }
  82. public class EffectMgr : Singleton<EffectMgr>, ISingletonUpdate
  83. {
  84. private HashMap<uint, EffectPlayInfo> playingList = new();
  85. private static Vector3 vecTemp = new();
  86. private List<uint> listTemp = new();
  87. public void Update(int timeMS)
  88. {
  89. listTemp.Clear();
  90. var now = Time.realtimeSinceStartup;
  91. foreach (var epi in playingList.Values)
  92. {
  93. if (epi.EndTime <= now)
  94. {
  95. listTemp.Add(epi.Id);
  96. }
  97. }
  98. foreach(var id in listTemp)
  99. {
  100. RemoveEffect(id);
  101. }
  102. }
  103. public async ETTask<uint> PlayEffect(LaunchEffect effect, uint HostId, Vector3 pos, float rotation = 0f)
  104. {
  105. var starttime = Time.realtimeSinceStartup;
  106. float time = EffectPlayInfo.TIME_DEFAULT;
  107. GameObject go = null;
  108. if (!effect.Name.IsNullOrWhitespace())
  109. {
  110. UnitRenderComponet render = null;
  111. if (effect.BindBody)
  112. {
  113. render = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(HostId);
  114. if (render == null)
  115. {
  116. Log.Debug($"ignore unit effect: {effect.Name}@{HostId}, cause of No Render");
  117. return 0;
  118. }
  119. }
  120. else if(pos == Vector3.zero)
  121. {
  122. var unit = UnitMgr.Instance.GetUnit(HostId);
  123. if(unit != null)
  124. {
  125. var zu = unit.ZoneObject;
  126. pos.Set(zu.X, zu.Y, zu.Z);
  127. }
  128. }
  129. go = await GameObjectPool.Instance.Acquire("Effect_" + effect.Name);
  130. if (effect.EffectTimeMS > 0)
  131. {
  132. time = effect.EffectTimeMS / 1000.0f;
  133. }
  134. else if (effect.EffectTimeMS < 0)
  135. {
  136. time = EffectPlayInfo.TIME_FOREVER;
  137. }
  138. else
  139. {
  140. var timeComponent = go.GetComponent<EffectTime>();
  141. if (timeComponent != null && timeComponent.duration > 0)
  142. {
  143. time = timeComponent.duration;
  144. }
  145. }
  146. setupEffect(go, effect, render, pos, rotation);
  147. }
  148. else if(!effect.SoundName.IsNullOrWhitespace())
  149. {
  150. //TODO: 音效的位置这里还有一堆逻辑的
  151. SoundManager.Instance.Play3DSound(effect.SoundName, pos).Coroutine();
  152. }
  153. if (effect.EarthQuakeMS > 0)
  154. {
  155. vecTemp.Set(effect.EarthQuakeXYZ, effect.EarthQuakeXYZ, effect.EarthQuakeXYZ);
  156. CameraMgr.ShakeMe(effect.EarthQuakeMS /1000.0f, vecTemp);
  157. }
  158. if (go != null)
  159. {
  160. var epi = ObjectPool.Instance.Fetch<EffectPlayInfo>();
  161. epi.Reset(go, starttime + time);
  162. playingList.Add(epi.Id, epi);
  163. return epi.Id;
  164. }
  165. return 0;
  166. }
  167. private void setupEffect(GameObject go, LaunchEffect effectData, UnitRenderComponet render, Vector3 pos, float rotation)
  168. {
  169. var gt = go.transform;
  170. if (effectData.BindBody)
  171. {
  172. if (!effectData.BindPartName.IsNullOrWhitespace())
  173. {
  174. gt.parent = render.GetBindPart(effectData.BindPartName);
  175. }
  176. else
  177. {
  178. gt.parent = render.GameObject.transform;
  179. }
  180. vecTemp.Set(effectData.EffectOffsetX, effectData.EffectHight, effectData.EffectOffsetY);
  181. gt.localPosition = vecTemp;
  182. if (effectData.Tag == "FixStartRotaion")
  183. {
  184. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  185. }
  186. else
  187. {
  188. gt.localRotation = Quaternion.identity;
  189. }
  190. }
  191. else
  192. {
  193. gt.rotation = RenderUtils.UnityRotationFromBattle(0);
  194. gt.parent = GlobalViewComponent.Instance.Unit;
  195. }
  196. if (pos != Vector3.zero)
  197. {
  198. vecTemp.Set(effectData.EffectOffsetX, effectData.EffectHight, effectData.EffectOffsetY);
  199. gt.position = RenderUtils.UnityPosFromBattle(pos + vecTemp);
  200. }
  201. if (rotation != 0)
  202. {
  203. gt.rotation = RenderUtils.UnityRotationFromBattle(rotation);
  204. }
  205. var scale = effectData.ScaleToBodySize != 0 ? effectData.ScaleToBodySize : 1f;
  206. vecTemp.Set(scale, scale, scale);
  207. gt.localScale = vecTemp;
  208. go.SetActive(true);
  209. if (!effectData.SoundName.IsNullOrWhitespace())
  210. {
  211. SoundManager.Instance.Play3DSound(effectData.SoundName, gt.position).Coroutine();
  212. }
  213. }
  214. public void Clear()
  215. {
  216. foreach (var epi in playingList.Values)
  217. {
  218. GameObject.Destroy(epi.GameObj);
  219. }
  220. playingList.Clear();
  221. }
  222. public void RemoveEffect(uint id)
  223. {
  224. if (playingList.ContainsKey(id))
  225. {
  226. var info = playingList[id];
  227. playingList.Remove(id);
  228. GameObjectPool.Instance.RecycleObject(info.GameObj);
  229. }
  230. }
  231. public void ResetEffect(uint id)
  232. {
  233. if (playingList.ContainsKey(id))
  234. {
  235. //var info = playingList[id];
  236. //TODO:Reset effect
  237. }
  238. }
  239. }
  240. }