EffectManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Linq;
  6. public class EffectManager : Singleton<EffectManager>
  7. {
  8. const int MAX_OWN_SAME_EFFECT_COUNT = 15;
  9. const int MAX_TARGET_SAME_EFFECT_COUNT = 15;
  10. Dictionary<int, Effect> mEffectMap = new Dictionary<int, Effect> ();
  11. Dictionary<Transform, Dictionary<int, List<int>>> mOwnerEffectMap = new Dictionary<Transform, Dictionary<int, List<int>>> ();
  12. Dictionary<Transform, Dictionary<int, List<int>>> mTargetEffectMap = new Dictionary<Transform, Dictionary<int, List<int>>> ();
  13. List<int> mRemoveList = new List<int> (50);
  14. public override void Init()
  15. {
  16. base.Init();
  17. }
  18. public override void UnInit()
  19. {
  20. base.UnInit();
  21. }
  22. public void Update (float deltaTime)
  23. {
  24. var e = mEffectMap.GetEnumerator ();
  25. while (e.MoveNext ())
  26. {
  27. if (e.Current.Value.IsDisposed)
  28. mRemoveList.Add (e.Current.Key);
  29. else
  30. e.Current.Value.Update (deltaTime);
  31. }
  32. for (int i = 0; i < mRemoveList.Count; i++)
  33. {
  34. Effect eft = mEffectMap [mRemoveList [i]];
  35. if (eft.OwnerTransform != null)
  36. mOwnerEffectMap [eft.OwnerTransform] [eft.EffectID].Remove (eft.InstanceID);
  37. if (eft.TargetTransform != null)
  38. mTargetEffectMap [eft.TargetTransform] [eft.EffectID].Remove (eft.InstanceID);
  39. mEffectMap.Remove (mRemoveList [i]);
  40. }
  41. mRemoveList.Clear ();
  42. }
  43. public void Clear ()
  44. {
  45. for (int i = 0; i < mEffectMap.Count; i++)
  46. {
  47. var element = mEffectMap.ElementAt (i);
  48. element.Value.Destroy();
  49. }
  50. mEffectMap.Clear ();
  51. mOwnerEffectMap.Clear ();
  52. mTargetEffectMap.Clear ();
  53. }
  54. #region PublicMethods
  55. public void PauseEffectOfOwnerFighter (Fighter fighter)
  56. {
  57. if (fighter.Ctrl == null)
  58. return;
  59. if (mOwnerEffectMap.ContainsKey (fighter.Ctrl.transform)) {
  60. Dictionary<int, List<int>> ownEffects = mOwnerEffectMap [fighter.Ctrl.transform];
  61. for (int i = 0; i < ownEffects.Count; i++) {
  62. var e = ownEffects.ElementAt (i);
  63. for (int j = 0; j < e.Value.Count; j++)
  64. mEffectMap [e.Value [j]].Pause ();
  65. }
  66. }
  67. }
  68. public void ResumeEffectOfOwnerFighter (Fighter fighter)
  69. {
  70. if (fighter.Ctrl == null)
  71. return;
  72. if (mOwnerEffectMap.ContainsKey (fighter.Ctrl.transform)) {
  73. Dictionary<int, List<int>> ownEffects = mOwnerEffectMap [fighter.Ctrl.transform];
  74. for (int i = 0; i < ownEffects.Count; i++) {
  75. var e = ownEffects.ElementAt (i);
  76. for (int j = 0; j < e.Value.Count; j++)
  77. mEffectMap [e.Value [j]].ResumeFromPause ();
  78. }
  79. }
  80. }
  81. public void RemoveEffectByOwnerFighter(Fighter fighter)
  82. {
  83. if (fighter.Ctrl == null)
  84. return;
  85. if (mOwnerEffectMap.ContainsKey(fighter.Ctrl.transform))
  86. {
  87. Dictionary<int, List<int>> ownEffects = mOwnerEffectMap[fighter.Ctrl.transform];
  88. for(int i = ownEffects.Count -1; i>=0; i--)
  89. {
  90. var e = ownEffects.ElementAt(i);
  91. for (int j = 0; j < e.Value.Count; j++)
  92. {
  93. if(mEffectMap.ContainsKey(e.Value[j]))
  94. {
  95. mEffectMap[e.Value[j]].Destroy();
  96. }
  97. }
  98. e.Value.Clear();
  99. }
  100. }
  101. }
  102. public void PauseAllEffects()
  103. {
  104. foreach(var p in mEffectMap)
  105. {
  106. p.Value.Pause();
  107. }
  108. }
  109. public void ResumeAllEffects()
  110. {
  111. foreach (var p in mEffectMap)
  112. {
  113. p.Value.ResumeFromPause();
  114. }
  115. }
  116. public void RemoveEffectByOwnerID (Transform t, int id)
  117. {
  118. if (mOwnerEffectMap.ContainsKey (t)) {
  119. Dictionary<int, List<int>> ownEffects = mOwnerEffectMap [t];
  120. if (ownEffects.ContainsKey (id)) {
  121. List<int> effects = ownEffects [id];
  122. for (int i = 0; i < effects.Count; i++)
  123. mEffectMap [effects [i]].Destroy ();
  124. effects.Clear();
  125. }
  126. }
  127. }
  128. public void RemoveOneEffectByOwnerID (Transform t, int id)
  129. {
  130. if (mOwnerEffectMap.ContainsKey (t)) {
  131. Dictionary<int, List<int>> ownEffects = mOwnerEffectMap [t];
  132. if (ownEffects.ContainsKey (id) && ownEffects [id].Count > 0)
  133. {
  134. if (mEffectMap.ContainsKey(ownEffects[id][0]))
  135. {
  136. mEffectMap[ownEffects[id][0]].Destroy();
  137. ownEffects[id].RemoveAt(0);
  138. }
  139. else
  140. {
  141. ownEffects[id].RemoveAt(0);
  142. }
  143. }
  144. }
  145. }
  146. public void RemoveOneEffectByTargetID (Transform t, int id)
  147. {
  148. if (mTargetEffectMap.ContainsKey (t)) {
  149. Dictionary<int, List<int>> targetEffects = mTargetEffectMap [t];
  150. if (targetEffects.ContainsKey (id) && targetEffects [id].Count > 0) {
  151. if (mEffectMap.ContainsKey(targetEffects[id][0])) {
  152. mEffectMap [targetEffects[id][0]].Destroy();
  153. targetEffects[id].RemoveAt(0);
  154. }
  155. else
  156. {
  157. targetEffects[id].RemoveAt(0);
  158. }
  159. }
  160. }
  161. }
  162. public GameObject CreateEffectGo (string effectName)
  163. {
  164. return BattlePrepareManager.Instance.PopAssetGo (Constants.EffectPath,effectName);
  165. }
  166. int GetOwnerEffectCount (Transform t, int id)
  167. {
  168. if (mOwnerEffectMap.ContainsKey (t) && mOwnerEffectMap [t].ContainsKey (id))
  169. return mOwnerEffectMap [t] [id].Count;
  170. return 0;
  171. }
  172. int GetTargetEffectCount (Transform t, int id)
  173. {
  174. if (mTargetEffectMap.ContainsKey (t) && mTargetEffectMap [t].ContainsKey (id))
  175. return mTargetEffectMap [t] [id].Count;
  176. return 0;
  177. }
  178. #endregion
  179. public int PlayEffect (int effectId, Fighter owner, Fighter target, Bullet bullet = null
  180. , bool ignoreTimescale = false, bool fixFloor = false, int lookType = 0, bool mirrorable = true, string forceLayer = "")
  181. {
  182. return PlayEffect (effectId, owner != null && owner.Ctrl != null ? owner.Ctrl.transform : null,
  183. target != null && target.Ctrl != null ? target.Ctrl.transform : null,
  184. bullet != null && bullet.Ctrl != null ? bullet.Ctrl.transform : null,
  185. ignoreTimescale, fixFloor, lookType, mirrorable, forceLayer);
  186. }
  187. public int PlayEffect (int effectId, Transform ownerTransform, Transform targetTransform, Transform bulletTransform = null
  188. , bool ignoreTimescale = false, bool fixFloor = false, int lookType = 0, bool mirrorable = true, string forceLayer = "")
  189. {
  190. if (ownerTransform != null && GetOwnerEffectCount(ownerTransform, effectId) >= MAX_OWN_SAME_EFFECT_COUNT)
  191. RemoveOneEffectByOwnerID(ownerTransform, effectId);
  192. if (targetTransform != null && GetTargetEffectCount(targetTransform, effectId) >= MAX_TARGET_SAME_EFFECT_COUNT)
  193. RemoveOneEffectByTargetID(targetTransform, effectId);
  194. Effect effect = PopEffect (effectId);
  195. if (effect == null || !effect.Valid) {
  196. return -1;
  197. }
  198. effect.Play (ownerTransform, targetTransform, bulletTransform, ignoreTimescale, fixFloor, lookType, mirrorable, forceLayer);
  199. mEffectMap.Add (effect.InstanceID, effect);
  200. if (ownerTransform != null)
  201. {
  202. if (!mOwnerEffectMap.ContainsKey (ownerTransform))
  203. mOwnerEffectMap.Add (ownerTransform, new Dictionary<int, List<int>> ());
  204. if (!mOwnerEffectMap [ownerTransform].ContainsKey (effect.EffectID))
  205. mOwnerEffectMap [ownerTransform].Add (effect.EffectID, new List<int> ());
  206. mOwnerEffectMap [ownerTransform] [effect.EffectID].Add (effect.InstanceID);
  207. }
  208. if (targetTransform != null)
  209. {
  210. if (!mTargetEffectMap.ContainsKey (targetTransform))
  211. mTargetEffectMap.Add (targetTransform, new Dictionary<int, List<int>> ());
  212. if (!mTargetEffectMap [targetTransform].ContainsKey (effect.EffectID))
  213. mTargetEffectMap [targetTransform].Add (effect.EffectID, new List<int> ());
  214. mTargetEffectMap [targetTransform] [effect.EffectID].Add (effect.InstanceID);
  215. }
  216. return effect.InstanceID;
  217. }
  218. public int PlayEffect(int effectId)
  219. {
  220. Effect effect = PopEffect(effectId);
  221. if (effect == null || !effect.Valid)
  222. return -1;
  223. effect.Play(null, null, null, false, false, 0, true, "");
  224. mEffectMap.Add(effect.InstanceID, effect);
  225. return effect.InstanceID;
  226. }
  227. public void RemoveEffectByID (int effectID)
  228. {
  229. for (int i = 0; i < mEffectMap.Count; i++)
  230. if (mEffectMap.ElementAt (i).Value.EffectID == effectID) {
  231. mEffectMap.ElementAt (i).Value.Destroy();
  232. }
  233. }
  234. public void RemoveAllEffect ()
  235. {
  236. Clear ();
  237. }
  238. Effect PopEffect (int effectId)
  239. {
  240. EffectData data = BattlePrepareManager.Instance.PopEffectData (effectId);
  241. return data != null ? new Effect (data) : null;
  242. }
  243. public void RemoveEffectByInstanceID (int instanceID, bool selfOnly = false)
  244. {
  245. if (mEffectMap.ContainsKey (instanceID)) {
  246. Effect eft = mEffectMap [instanceID];
  247. //if (selfOnly && IsEffectSelf (eft))
  248. // return;
  249. eft.Destroy();
  250. }
  251. }
  252. void Dispose ()
  253. {
  254. Clear ();
  255. }
  256. public void HideEffectByInstanceId (int instanceid)
  257. {
  258. if (mEffectMap.ContainsKey (instanceid))
  259. mEffectMap [instanceid].Hide ();
  260. }
  261. public void UnHideEffectByInstanceId (int instanceid)
  262. {
  263. if (mEffectMap.ContainsKey (instanceid))
  264. mEffectMap [instanceid].UnHide ();
  265. }
  266. public void SetEffectScaleByInstanceId(int instanceId,float scale)
  267. {
  268. if (mEffectMap.ContainsKey(instanceId))
  269. mEffectMap[instanceId].SetScale(scale);
  270. }
  271. public void OnFullScreenEffectStart ()
  272. {
  273. //foreach (var efx in mEffectMap.Values)
  274. // if (efx.RunningOption != null && efx.RunningOption.hideOnUltraSkill)
  275. // efx.Hide ();
  276. }
  277. public void OnFullScreenEffectEnd ()
  278. {
  279. //foreach (var efx in mEffectMap.Values)
  280. // if (efx.RunningOption != null && efx.RunningOption.hideOnUltraSkill)
  281. // efx.UnHide ();
  282. }
  283. }