BuffsComponent.Attribute.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using FL.Battle.Buffs;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame.Database;
  5. namespace FL.Battle.Components
  6. {
  7. /// <summary>
  8. /// 该脚本主要处理属性类Buff
  9. /// </summary>
  10. public partial class BuffsComponent
  11. {
  12. /// <summary>
  13. /// 加速buff
  14. /// </summary>
  15. private List<Buff> _speedUpBuffs;
  16. /// <summary>
  17. /// 减速buff
  18. /// </summary>
  19. private List<Buff> _speedCutBuffs;
  20. /// <summary>
  21. /// key:buffId
  22. /// value:buff增加/减少的属性值
  23. /// </summary>
  24. private Dictionary<int, int> _buffAttributeMap;
  25. private EAttributeType GetAttributeType(Buff buff)
  26. {
  27. return (EAttributeType)buff.Table.BuffTypeNum[0];
  28. }
  29. private int GetAttributeValue(Buff buff)
  30. {
  31. return buff.Table.BuffTypeNum[1] * buff.Layers;
  32. }
  33. /// <summary>
  34. /// 增加属性buff
  35. /// </summary>
  36. /// <param name="buff"></param>
  37. private void AddAttributeBuff(Buff buff)
  38. {
  39. //if (buff.BuffType != EBuffType.Attribute)
  40. // return;
  41. var attrType = GetAttributeType(buff);
  42. if (attrType == EAttributeType.SpeedAdd)
  43. { // 加/减速的buff同时只能各有一个生效
  44. AddSpeedBuff(buff);
  45. return;
  46. }
  47. SaveAttributeValue(buff);
  48. }
  49. /// <summary>
  50. /// 移除属性buff
  51. /// </summary>
  52. /// <param name="buff"></param>
  53. private void RemoveAttributeBuff(Buff buff)
  54. {
  55. //if (buff.BuffType != EBuffType.Attribute)
  56. // return;
  57. var attrType = GetAttributeType(buff);
  58. if (attrType == EAttributeType.SpeedAdd)
  59. { // 加/减速的buff同时只能各有一个生效
  60. RemoveSpeedBuff(buff);
  61. return;
  62. }
  63. RemoveAttributeValue(buff);
  64. }
  65. private void SaveAttributeValue(Buff buff)
  66. {
  67. var buffId = buff.TableId;
  68. var addVal = GetAttributeValue(buff);
  69. var result = addVal;
  70. if (_buffAttributeMap == null)
  71. _buffAttributeMap = new Dictionary<int, int>();
  72. else if (_buffAttributeMap.TryGetValue(buffId, out var lastVal))
  73. {
  74. result -= lastVal;
  75. }
  76. _buffAttributeMap[buffId] = addVal;
  77. var attrType = GetAttributeType(buff);
  78. Context.Entity.Attr.Dynamic.AddValue(attrType, result);
  79. }
  80. private void RemoveAttributeValue(Buff buff)
  81. {
  82. if (_buffAttributeMap?.TryGetValue(buff.TableId, out var value) ?? false)
  83. {
  84. _buffAttributeMap.Remove(buff.TableId);
  85. Context.Entity.Attr.Dynamic.AddValue(GetAttributeType(buff), -value);
  86. }
  87. }
  88. private void AddSpeedBuff(Buff buff)
  89. {
  90. var buffId = buff.TableId;
  91. var addVal = GetAttributeValue(buff);
  92. var speedBuffs = GetSpeedBuffs(addVal > 0);
  93. if (speedBuffs.Count > 0)
  94. { // 已有buff
  95. var firstLast = speedBuffs[0];
  96. var buffIdx = speedBuffs.FindIndex((a) => a.TableId == buffId);
  97. if (buffIdx != -1)
  98. {
  99. speedBuffs.RemoveAt(buffIdx);
  100. }
  101. buffIdx = -1;
  102. for (int i = 0; i < speedBuffs.Count; i++)
  103. {
  104. var tempBuff = speedBuffs[i];
  105. var tempVal = GetAttributeValue(tempBuff);
  106. if (Mathf.Abs(tempVal) < Mathf.Abs(addVal))
  107. { // 找到第一个数值比当前小的,数值一样的,新的在后面
  108. speedBuffs.Insert(i, buff);
  109. buffIdx = i;
  110. break;
  111. }
  112. }
  113. if (buffIdx == -1)
  114. {
  115. speedBuffs.Add(buff);
  116. buffIdx = speedBuffs.Count - 1;
  117. }
  118. var firstNext = speedBuffs[0];
  119. if (firstNext.TableId == firstLast.TableId)
  120. { // 第一位的buff没变化,是同一个buff则需要刷新数值,否则不刷新
  121. if (buffIdx > 0)
  122. {
  123. return;
  124. }
  125. }
  126. else
  127. { // 先删除旧buff的数值
  128. RemoveAttributeValue(firstLast);
  129. SaveAttributeValue(firstNext);
  130. RefreshMoveTimeScale();
  131. return;
  132. }
  133. }
  134. else
  135. {
  136. speedBuffs.Add(buff);
  137. }
  138. SaveAttributeValue(buff);
  139. RefreshMoveTimeScale();
  140. }
  141. private void RemoveSpeedBuff(Buff buff)
  142. {
  143. var buffId = buff.TableId;
  144. var addVal = GetAttributeValue(buff);
  145. var speedBuffs = GetSpeedBuffs(addVal > 0);
  146. var buffIdx = speedBuffs.FindIndex((a) => a.TableId == buffId);
  147. if (buffIdx < 0)
  148. {
  149. return;
  150. }
  151. speedBuffs.RemoveAt(buffIdx);
  152. if (buffIdx > 0)
  153. { // 删除的不是第一个
  154. return;
  155. }
  156. //删除的是第一个buff
  157. RemoveAttributeValue(buff);
  158. if (speedBuffs.Count > 0)
  159. { // 还有加/减速buff
  160. SaveAttributeValue(speedBuffs[0]);
  161. }
  162. RefreshMoveTimeScale();
  163. }
  164. private List<Buff> GetSpeedBuffs(bool isSpeedUp)
  165. {
  166. if (isSpeedUp)
  167. return _speedUpBuffs ??= new List<Buff>();
  168. return _speedCutBuffs ??= new List<Buff>();
  169. }
  170. private void ClearAttributes()
  171. {
  172. _buffAttributeMap?.Clear();
  173. _speedUpBuffs?.Clear();
  174. _speedCutBuffs?.Clear();
  175. }
  176. private void RefreshMoveTimeScale()
  177. {
  178. Context.Move.TimeScale = Context.Entity.Attr.MoveSpeedScale;
  179. }
  180. }
  181. }