BuffControl.cs 6.7 KB

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