BuffControl.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using Common.Utility.CombatEvent;
  4. using Fort23.Core;
  5. namespace GameLogic.Combat.Buff
  6. {
  7. public class BuffControl : IDisposable
  8. {
  9. private BetterList<BuffBasic> _allBuff = new BetterList<BuffBasic>();
  10. private CombatHeroEntity _combatHeroEntity;
  11. public void Init(CombatHeroEntity combatHeroEntity)
  12. {
  13. _combatHeroEntity = combatHeroEntity;
  14. }
  15. public BuffBasic AddBuff(CombatHeroEntity source, BuffInfo buffInfo,
  16. AddActionsType addActionsType = AddActionsType.Direct)
  17. {
  18. if (buffInfo.BuffConfig.IsControlBuff == 1)
  19. {
  20. b_1015 b_1015 = GetBuffBasicForType<b_1015>();
  21. if (b_1015 != null)
  22. {
  23. return null;
  24. }
  25. }
  26. StartAddBuffEventData startAddBuffEventData = StartAddBuffEventData.Create();
  27. startAddBuffEventData.BuffInfo = buffInfo;
  28. startAddBuffEventData.source = source;
  29. startAddBuffEventData.target = _combatHeroEntity;
  30. startAddBuffEventData.addActionsType = addActionsType;
  31. CombatEventManager.Instance.Dispatch(CombatEventType.StartAddBuff, startAddBuffEventData, false);
  32. if (startAddBuffEventData.isNotAddBuff)
  33. {
  34. startAddBuffEventData.Recycle();
  35. return null;
  36. }
  37. startAddBuffEventData.Recycle();
  38. BuffBasic buffBasic = GetBuffBasicForIdGroup(buffInfo.BuffConfig.buffGroup);
  39. if (buffBasic == null)
  40. {
  41. buffBasic = GetBuffBasic(buffInfo.BuffConfig.scriptsName);
  42. if (buffBasic == null)
  43. {
  44. return null;
  45. }
  46. _allBuff.Add(buffBasic);
  47. buffBasic.Init(_combatHeroEntity, source, buffInfo);
  48. if (buffBasic.buffInf != null)
  49. {
  50. BuffEventData buffEventData = BuffEventData.Create();
  51. buffEventData.BuffBasic = buffBasic;
  52. buffEventData.source = source;
  53. buffEventData.target = _combatHeroEntity;
  54. buffEventData.addActionsType = addActionsType;
  55. buffEventData.addCount = buffInfo.count;
  56. buffEventData.id = buffInfo.BuffConfig.ID;
  57. buffEventData.BuffInfo = buffInfo;
  58. CombatEventManager.Instance.Dispatch(CombatEventType.AddBuff, buffEventData);
  59. }
  60. }
  61. else
  62. {
  63. BuffEventData buffEventData = BuffEventData.Create();
  64. buffEventData.BuffBasic = buffBasic;
  65. buffEventData.source = source;
  66. buffEventData.isSuperimposing = true;
  67. buffEventData.target = _combatHeroEntity;
  68. buffEventData.addActionsType = addActionsType;
  69. buffEventData.addCount = buffInfo.count;
  70. buffEventData.id = buffInfo.BuffConfig.ID;
  71. buffEventData.BuffInfo = buffInfo;
  72. int c = buffBasic.AddBuffCount(source, buffInfo);
  73. if (c > 0 && buffBasic.buffInf != null)
  74. {
  75. buffEventData.addCount = c;
  76. CombatEventManager.Instance.Dispatch(CombatEventType.AddBuff, buffEventData);
  77. }
  78. if (buffInfo.BuffConfig.timeType == 1)
  79. {
  80. CObjectPool.Instance.Recycle(buffInfo);
  81. }
  82. }
  83. return buffBasic;
  84. }
  85. protected BuffBasic GetBuffBasic(string buffName)
  86. {
  87. string typeName = "GameLogic.Combat.Buff." + buffName;
  88. {
  89. System.Type type = System.Type.GetType(typeName);
  90. if (type == null)
  91. {
  92. return null;
  93. }
  94. BuffBasic sb = (BuffBasic)CObjectPool.Instance.Fetch(type);
  95. return sb;
  96. }
  97. }
  98. public T GetBuffBasicForType<T>() where T : BuffBasic
  99. {
  100. for (int i = 0; i < _allBuff.Count; i++)
  101. {
  102. BuffBasic b = _allBuff[i];
  103. if (b is T)
  104. {
  105. return (T)b;
  106. }
  107. }
  108. return null;
  109. }
  110. /// <summary>
  111. /// 获得对应buff
  112. /// </summary>
  113. /// <param name="buffType"> 1=buff 2=debuff 3=中立</param>
  114. /// clearType 0=全部 1=能清楚 2=不能清楚
  115. /// <returns></returns>
  116. public List<BuffBasic> GetBuffBasicForBuffType(int buffType,int clearType= 0)
  117. {
  118. List<BuffBasic> buffBasics = new List<BuffBasic>();
  119. for (int i = 0; i < _allBuff.Count; i++)
  120. {
  121. BuffBasic b = _allBuff[i];
  122. if (b.buffInf.BuffConfig.buffType == buffType)
  123. {
  124. if (clearType ==1&& b.buffInf.BuffConfig.dispelType != 1)
  125. {
  126. continue;
  127. }
  128. if (clearType ==2&& b.buffInf.BuffConfig.dispelType != 2)
  129. {
  130. continue;
  131. }
  132. buffBasics.Add(b);
  133. }
  134. }
  135. return buffBasics;
  136. }
  137. public BuffBasic GetBuffBasicForIdGroup(int idGroup)
  138. {
  139. for (int i = 0; i < _allBuff.Count; i++)
  140. {
  141. BuffBasic b = _allBuff[i];
  142. if (b.buffInf.BuffConfig.buffGroup == idGroup)
  143. {
  144. return b;
  145. }
  146. }
  147. return null;
  148. }
  149. /// <summary>
  150. /// 获得对应buff的数量
  151. /// </summary>
  152. /// <param name="buffType">1=buff,2=debuff 0=全部</param>
  153. /// <returns></returns>
  154. public int GetBuffCountForType(int buffType)
  155. {
  156. if (buffType == 0)
  157. {
  158. return _allBuff.Count;
  159. }
  160. int c = 0;
  161. for (int i = 0; i < _allBuff.Count; i++)
  162. {
  163. if (_allBuff[i].buffInf.BuffConfig.buffType == buffType)
  164. {
  165. c++;
  166. }
  167. }
  168. return c;
  169. }
  170. public void RemoveBuff(BuffBasic buffBasic)
  171. {
  172. if (buffBasic == null)
  173. {
  174. return;
  175. }
  176. _allBuff.Remove(buffBasic);
  177. BuffEventData buffEventData = BuffEventData.Create();
  178. buffEventData.BuffBasic = buffBasic;
  179. buffEventData.source = null;
  180. buffEventData.target = _combatHeroEntity;
  181. CombatEventManager.Instance.Dispatch(CombatEventType.RemoveBuff, buffEventData);
  182. buffBasic.Dispose();
  183. CObjectPool.Instance.Recycle(buffBasic);
  184. }
  185. public void Update(float t)
  186. {
  187. for (int i = 0; i < _allBuff.Count; i++)
  188. {
  189. _allBuff[i].Update(t);
  190. }
  191. }
  192. public void Dispose()
  193. {
  194. for (int i = 0; i < _allBuff.Count; i++)
  195. {
  196. BuffBasic buffBasic = _allBuff[i];
  197. buffBasic.Dispose();
  198. CObjectPool.Instance.Recycle(buffBasic);
  199. }
  200. _allBuff.Clear();
  201. _combatHeroEntity = null;
  202. }
  203. }
  204. }