BuffControl.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. CombatEventManager.Instance.Dispatch(CombatEventType.AddBuff, buffEventData);
  58. }
  59. }
  60. else
  61. {
  62. BuffEventData buffEventData = BuffEventData.Create();
  63. buffEventData.BuffBasic = buffBasic;
  64. buffEventData.source = source;
  65. buffEventData.target = _combatHeroEntity;
  66. buffEventData.addActionsType = addActionsType;
  67. buffEventData.addCount = buffInfo.count;
  68. buffEventData.id = buffInfo.BuffConfig.ID;
  69. int c = buffBasic.AddBuffCount(source, buffInfo);
  70. if (c > 0 && buffBasic.buffInf != null)
  71. {
  72. CombatEventManager.Instance.Dispatch(CombatEventType.AddBuff, buffEventData);
  73. }
  74. }
  75. return buffBasic;
  76. }
  77. protected BuffBasic GetBuffBasic(string buffName)
  78. {
  79. string typeName = "GameLogic.Combat.Buff." + buffName;
  80. {
  81. System.Type type = System.Type.GetType(typeName);
  82. if (type == null)
  83. {
  84. return null;
  85. }
  86. BuffBasic sb = (BuffBasic)CObjectPool.Instance.Fetch(type);
  87. return sb;
  88. }
  89. }
  90. public T GetBuffBasicForType<T>() where T : BuffBasic
  91. {
  92. for (int i = 0; i < _allBuff.Count; i++)
  93. {
  94. BuffBasic b = _allBuff[i];
  95. if (b is T)
  96. {
  97. return (T)b;
  98. }
  99. }
  100. return null;
  101. }
  102. /// <summary>
  103. /// 获得对应buff
  104. /// </summary>
  105. /// <param name="buffType"> 1=buff 2=debuff 3=中立</param>
  106. /// <returns></returns>
  107. public List<BuffBasic> GetBuffBasicForBuffType(int buffType)
  108. {
  109. List<BuffBasic> buffBasics = new List<BuffBasic>();
  110. for (int i = 0; i < _allBuff.Count; i++)
  111. {
  112. BuffBasic b = _allBuff[i];
  113. if (b.buffInf.BuffConfig.buffType == buffType)
  114. {
  115. buffBasics.Add(b);
  116. }
  117. }
  118. return buffBasics;
  119. }
  120. public BuffBasic GetBuffBasicForIdGroup(int idGroup)
  121. {
  122. for (int i = 0; i < _allBuff.Count; i++)
  123. {
  124. BuffBasic b = _allBuff[i];
  125. if (b.buffInf.BuffConfig.buffGroup == idGroup)
  126. {
  127. return b;
  128. }
  129. }
  130. return null;
  131. }
  132. /// <summary>
  133. /// 获得对应buff的数量
  134. /// </summary>
  135. /// <param name="buffType">1=buff,2=debuff 0=全部</param>
  136. /// <returns></returns>
  137. public int GetBuffCountForType(int buffType)
  138. {
  139. if (buffType == 0)
  140. {
  141. return _allBuff.Count;
  142. }
  143. int c = 0;
  144. for (int i = 0; i < _allBuff.Count; i++)
  145. {
  146. if (_allBuff[i].buffInf.BuffConfig.buffType == buffType)
  147. {
  148. c++;
  149. }
  150. }
  151. return c;
  152. }
  153. public void RemoveBuff(BuffBasic buffBasic)
  154. {
  155. if (buffBasic == null)
  156. {
  157. return;
  158. }
  159. _allBuff.Remove(buffBasic);
  160. BuffEventData buffEventData = BuffEventData.Create();
  161. buffEventData.BuffBasic = buffBasic;
  162. buffEventData.source = null;
  163. buffEventData.target = _combatHeroEntity;
  164. CombatEventManager.Instance.Dispatch(CombatEventType.RemoveBuff, buffEventData);
  165. buffBasic.Dispose();
  166. CObjectPool.Instance.Recycle(buffBasic);
  167. }
  168. public void Update(float t)
  169. {
  170. for (int i = 0; i < _allBuff.Count; i++)
  171. {
  172. _allBuff[i].Update(t);
  173. }
  174. }
  175. public void Dispose()
  176. {
  177. for (int i = 0; i < _allBuff.Count; i++)
  178. {
  179. BuffBasic buffBasic = _allBuff[i];
  180. buffBasic.Dispose();
  181. CObjectPool.Instance.Recycle(buffBasic);
  182. }
  183. _allBuff.Clear();
  184. _combatHeroEntity = null;
  185. }
  186. }
  187. }