BuffControl.cs 4.8 KB

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