BuffControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. {
  17. StartAddBuffEventData startAddBuffEventData = StartAddBuffEventData.Create();
  18. startAddBuffEventData.BuffInfo = buffInfo;
  19. startAddBuffEventData.source = source;
  20. startAddBuffEventData.target = _combatHeroEntity;
  21. CombatEventManager.Instance.Dispatch(CombatEventType.StartAddBuff, startAddBuffEventData, false);
  22. if (startAddBuffEventData.isNotAddBuff)
  23. {
  24. startAddBuffEventData.Recycle();
  25. return null;
  26. }
  27. startAddBuffEventData.Recycle();
  28. BuffBasic buffBasic = GetBuffBasicForIdGroup(buffInfo.BuffConfig.buffGroup);
  29. if (buffBasic == null)
  30. {
  31. buffBasic = GetBuffBasic(buffInfo.BuffConfig.scriptsName);
  32. if (buffBasic == null)
  33. {
  34. return null;
  35. }
  36. _allBuff.Add(buffBasic);
  37. buffBasic.Init(_combatHeroEntity, source, buffInfo);
  38. if (buffBasic.buffInf != null)
  39. {
  40. BuffEventData buffEventData = BuffEventData.Create();
  41. buffEventData.BuffBasic = buffBasic;
  42. buffEventData.source = source;
  43. buffEventData.target = _combatHeroEntity;
  44. CombatEventManager.Instance.Dispatch(CombatEventType.AddBuff, buffEventData);
  45. }
  46. }
  47. else
  48. {
  49. buffBasic.AddBuffCount(source, buffInfo);
  50. }
  51. return buffBasic;
  52. }
  53. protected BuffBasic GetBuffBasic(string buffName)
  54. {
  55. string typeName = "GameLogic.Combat.Buff." + buffName;
  56. {
  57. System.Type type = System.Type.GetType(typeName);
  58. if (type == null)
  59. {
  60. return null;
  61. }
  62. BuffBasic sb = (BuffBasic)CObjectPool.Instance.Fetch(type);
  63. return sb;
  64. }
  65. }
  66. public T GetBuffBasicForType<T>() where T : BuffBasic
  67. {
  68. for (int i = 0; i < _allBuff.Count; i++)
  69. {
  70. BuffBasic b = _allBuff[i];
  71. if (b is T)
  72. {
  73. return (T)b;
  74. }
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// 获得对应buff
  80. /// </summary>
  81. /// <param name="buffType"> 1=buff 2=debuff 3=中立</param>
  82. /// <returns></returns>
  83. public List<BuffBasic> GetBuffBasicForBuffType(int buffType)
  84. {
  85. List<BuffBasic> buffBasics = new List<BuffBasic>();
  86. for (int i = 0; i < _allBuff.Count; i++)
  87. {
  88. BuffBasic b = _allBuff[i];
  89. if (b.buffInf.BuffConfig.buffType == buffType)
  90. {
  91. buffBasics.Add(b);
  92. }
  93. }
  94. return buffBasics;
  95. }
  96. public BuffBasic GetBuffBasicForIdGroup(int idGroup)
  97. {
  98. for (int i = 0; i < _allBuff.Count; i++)
  99. {
  100. BuffBasic b = _allBuff[i];
  101. if (b.buffInf.BuffConfig.buffGroup == idGroup)
  102. {
  103. return b;
  104. }
  105. }
  106. return null;
  107. }
  108. /// <summary>
  109. /// 获得对应buff的数量
  110. /// </summary>
  111. /// <param name="buffType">1=buff,2=debuff 0=全部</param>
  112. /// <returns></returns>
  113. public int GetBuffCountForType(int buffType)
  114. {
  115. if (buffType == 0)
  116. {
  117. return _allBuff.Count;
  118. }
  119. int c = 0;
  120. for (int i = 0; i < _allBuff.Count; i++)
  121. {
  122. if (_allBuff[i].buffInf.BuffConfig.buffType == buffType)
  123. {
  124. c++;
  125. }
  126. }
  127. return c;
  128. }
  129. public void RemoveBuff(BuffBasic buffBasic)
  130. {
  131. if (buffBasic == null)
  132. {
  133. return;
  134. }
  135. _allBuff.Remove(buffBasic);
  136. BuffEventData buffEventData = BuffEventData.Create();
  137. buffEventData.BuffBasic = buffBasic;
  138. buffEventData.source = null;
  139. buffEventData.target = _combatHeroEntity;
  140. CombatEventManager.Instance.Dispatch(CombatEventType.RemoveBuff, buffEventData);
  141. buffBasic.Dispose();
  142. CObjectPool.Instance.Recycle(buffBasic);
  143. }
  144. public void Update(float t)
  145. {
  146. for (int i = 0; i < _allBuff.Count; i++)
  147. {
  148. _allBuff[i].Update(t);
  149. }
  150. }
  151. public void Dispose()
  152. {
  153. for (int i = 0; i < _allBuff.Count; i++)
  154. {
  155. BuffBasic buffBasic = _allBuff[i];
  156. buffBasic.Dispose();
  157. CObjectPool.Instance.Recycle(buffBasic);
  158. }
  159. _allBuff.Clear();
  160. _combatHeroEntity = null;
  161. }
  162. }
  163. }