BuffControl.cs 4.2 KB

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