EffectMgr.cs 9.3 KB

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