ComAIUnit.Buff.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using CommonAI.Zone.Helper;
  2. using CommonAI.ZoneClient;
  3. using CommonLang;
  4. using System;
  5. using System.Collections;
  6. using UnityEngine;
  7. using CommonLang.Concurrent;
  8. using CommonAI.Zone;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using CommonAIClient.Unity.Utils;
  12. namespace CommonAIClient.Unity.Battle
  13. {
  14. public partial class ComAIUnit
  15. {
  16. HashMap<ZoneUnit.BuffState, Buff> mBuffs = new HashMap<ZoneUnit.BuffState, Buff>();
  17. private void UpdateBuff(float deltaTime)
  18. {
  19. foreach (var elem in mBuffs)
  20. {
  21. elem.Value.Update(deltaTime);
  22. }
  23. }
  24. protected virtual void ZUnit_OnBuffAdded(ZoneUnit unit, ZoneUnit.BuffState state)
  25. {
  26. Buff buff = new Buff(this, state);
  27. mBuffs.Add(state, buff);
  28. }
  29. protected virtual void ZUnit_OnBuffChanged(ZoneUnit unit, ZoneUnit.BuffState state)
  30. {
  31. Buff buff = null;
  32. if (mBuffs.TryGetValue(state, out buff))
  33. {
  34. buff.OnChange(state);
  35. }
  36. }
  37. protected virtual void ZUnit_OnBuffRemoved(ZoneUnit unit, ZoneUnit.BuffState state)
  38. {
  39. Buff buff = mBuffs.RemoveByKey(state);
  40. if (buff != null)
  41. {
  42. buff.Dispose();
  43. }
  44. }
  45. public class Buff
  46. {
  47. #region RefCount
  48. private static AtomicInteger s_alloc_count = new AtomicInteger(0);
  49. private static AtomicInteger s_active_count = new AtomicInteger(0);
  50. /// <summary>
  51. /// 分配实例数量
  52. /// </summary>
  53. public static int AllocCount { get { return s_alloc_count.Value; } }
  54. /// <summary>
  55. /// 未释放实例数量
  56. /// </summary>
  57. public static int ActiveCount { get { return s_active_count.Value; } }
  58. #endregion
  59. private bool mDisposed = false;
  60. private List<AssetObjectExt> mModels = new List<AssetObjectExt>();
  61. private List<Audio> mAudios = new List<Audio>();
  62. private AssetObjectExt mOverlayModel;
  63. private Audio mOverlayAudio;
  64. private Avatar mAvatar;
  65. internal readonly ComAIUnit mOwner;
  66. internal readonly ZoneUnit.BuffState mState;
  67. public bool IsDisposed { get { return mDisposed; } }
  68. protected ComAIUnit Owner { get { return mOwner; } }
  69. protected ZoneUnit.BuffState State { get { return State; } }
  70. internal Buff(ComAIUnit owner, ZoneUnit.BuffState state)
  71. {
  72. s_alloc_count++;
  73. s_active_count++;
  74. this.mOwner = owner;
  75. this.mState = state;
  76. if (state.Data.MakeAvatar
  77. && !string.IsNullOrEmpty(state.Data.UnitFileName))
  78. {
  79. mAvatar = owner.AddAvatar(state.Data.UnitFileName
  80. , Path.GetFileNameWithoutExtension(state.Data.UnitFileName));
  81. }
  82. if (state.Data.BindingEffect != null)
  83. {
  84. InitBindingEffect(state.Data.BindingEffect);
  85. }
  86. if (state.Data.BindingEffectList != null
  87. && state.Data.BindingEffectList.Count > 0)
  88. {
  89. foreach (var elem in state.Data.BindingEffectList)
  90. {
  91. InitBindingEffect(elem);
  92. }
  93. }
  94. OnChange(state);
  95. }
  96. //只在初始化阶段调用
  97. private void InitBindingEffect(LaunchEffect eff)
  98. {
  99. if (!string.IsNullOrEmpty(eff.Name))
  100. {
  101. BattleFactroy.Instance.GameObjectAdapter.Load(eff.Name
  102. , System.IO.Path.GetFileNameWithoutExtension(eff.Name)
  103. , (succ, aoe) =>
  104. {
  105. if (IsDisposed)
  106. {
  107. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  108. return;
  109. }
  110. aoe.gameObject.ParentRoot(Owner.EffectRoot);
  111. mModels.Add(aoe);
  112. });
  113. }
  114. if (!string.IsNullOrEmpty(eff.SoundName))
  115. {
  116. if (eff.IsLoop)
  117. {
  118. Audio audio = BattleFactroy.Instance.SoundAdapter.PlaySoundLoop(eff.SoundName, mOwner.ObjectRoot);
  119. mAudios.Add(audio);
  120. }
  121. else
  122. {
  123. BattleFactroy.Instance.SoundAdapter.PlaySound(eff.SoundName, mOwner.ObjectRoot.Position());
  124. }
  125. }
  126. }
  127. ~Buff()
  128. {
  129. s_alloc_count--;
  130. }
  131. //多层叠加
  132. internal void OnChange(ZoneUnit.BuffState state)
  133. {
  134. if (state.OverlayLevel < state.Data.OverlayBindingEffect.Count)
  135. {
  136. var eff = state.Data.OverlayBindingEffect[state.OverlayLevel - 1];
  137. BattleFactroy.Instance.GameObjectAdapter.Load(eff.Name
  138. , System.IO.Path.GetFileNameWithoutExtension(eff.Name)
  139. , (succ, aoe) =>
  140. {
  141. if (IsDisposed)
  142. {
  143. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  144. return;
  145. }
  146. if (mOverlayModel != null)
  147. {
  148. BattleFactroy.Instance.GameObjectAdapter.Unload(mOverlayModel);
  149. }
  150. mOverlayModel = aoe;
  151. mOverlayModel.gameObject.ParentRoot(Owner.EffectRoot);
  152. });
  153. if (!string.IsNullOrEmpty(eff.SoundName))
  154. {
  155. if (eff.IsLoop)
  156. {
  157. mOverlayAudio = BattleFactroy.Instance.SoundAdapter.PlaySoundLoop(eff.SoundName, mOwner.ObjectRoot);
  158. }
  159. else
  160. {
  161. BattleFactroy.Instance.SoundAdapter.PlaySound(eff.SoundName, mOwner.ObjectRoot.Position());
  162. }
  163. }
  164. }
  165. }
  166. internal void Dispose()
  167. {
  168. if (!mDisposed)
  169. {
  170. OnDispose();
  171. mDisposed = true;
  172. s_active_count--;
  173. }
  174. }
  175. protected virtual void OnDispose()
  176. {
  177. if (mAvatar != null)
  178. {
  179. mOwner.RemoveAvatar(mAvatar);
  180. }
  181. foreach (var elem in mModels)
  182. {
  183. BattleFactroy.Instance.GameObjectAdapter.Unload(elem);
  184. }
  185. foreach (var elem in mAudios)
  186. {
  187. BattleFactroy.Instance.SoundAdapter.StopSoundLoop(elem);
  188. }
  189. if (mOverlayModel != null)
  190. {
  191. BattleFactroy.Instance.GameObjectAdapter.Unload(mOverlayModel);
  192. }
  193. if (mOverlayAudio != null)
  194. {
  195. BattleFactroy.Instance.SoundAdapter.StopSoundLoop(mOverlayAudio);
  196. }
  197. }
  198. internal protected virtual void Update(float deltaTime)
  199. {
  200. }
  201. }
  202. }
  203. }