You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Character.BuffManager.cs 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using Preparation.Utility;
  6. namespace GameClass.GameObj
  7. {
  8. public partial class Character
  9. {
  10. public readonly BuffManager buffManager;
  11. /// <summary>
  12. /// 角色携带的buff管理器
  13. /// </summary>
  14. public class BuffManager
  15. {
  16. [StructLayout(LayoutKind.Explicit, Size = 8)]
  17. private struct BuffValue // buff参数联合体类型,可能是int或double
  18. {
  19. [FieldOffset(0)]
  20. public int iValue;
  21. [FieldOffset(0)]
  22. public double lfValue;
  23. public BuffValue(int intValue)
  24. {
  25. this.lfValue = 0.0;
  26. this.iValue = intValue;
  27. }
  28. public BuffValue(double longFloatValue)
  29. {
  30. this.iValue = 0;
  31. this.lfValue = longFloatValue;
  32. }
  33. }
  34. /// <summary>
  35. /// buff列表
  36. /// </summary>
  37. private readonly LinkedList<BuffValue>[] buffList;
  38. private readonly object[] buffListLock;
  39. private void AddBuff(BuffValue bf, int buffTime, BuffType buffType, Action ReCalculateFunc)
  40. {
  41. new Thread
  42. (
  43. () =>
  44. {
  45. LinkedListNode<BuffValue> buffNode;
  46. lock (buffListLock[(int)buffType])
  47. {
  48. buffNode = buffList[(int)buffType].AddLast(bf);
  49. }
  50. ReCalculateFunc();
  51. Thread.Sleep(buffTime);
  52. try
  53. {
  54. lock (buffListLock[(int)buffType])
  55. {
  56. buffList[(int)buffType].Remove(buffNode);
  57. }
  58. }
  59. catch
  60. {
  61. }
  62. ReCalculateFunc();
  63. }
  64. )
  65. { IsBackground = true }.Start();
  66. }
  67. public int ReCalculateFloatBuff(BuffType buffType, int orgVal, int maxVal, int minVal)
  68. {
  69. double times = 1.0;
  70. lock (buffListLock[(int)buffType])
  71. {
  72. foreach (var add in buffList[(int)buffType])
  73. {
  74. times *= add.lfValue;
  75. }
  76. }
  77. return Math.Max(Math.Min((int)Math.Round(orgVal * times), maxVal), minVal);
  78. }
  79. public void AddMoveSpeed(double add, int buffTime, Action<int> SetNewMoveSpeed, int orgMoveSpeed) => AddBuff(new BuffValue(add), buffTime, BuffType.AddSpeed, () => SetNewMoveSpeed(ReCalculateFloatBuff(BuffType.AddSpeed, orgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed)));
  80. public bool HasFasterSpeed
  81. {
  82. get
  83. {
  84. lock (buffListLock[(int)BuffType.AddSpeed])
  85. {
  86. return buffList[(int)BuffType.AddSpeed].Count != 0;
  87. }
  88. }
  89. }
  90. public void AddShield(int shieldTime) => AddBuff(new BuffValue(), shieldTime, BuffType.Shield, () =>
  91. { });
  92. public bool HasShield
  93. {
  94. get
  95. {
  96. lock (buffListLock[(int)BuffType.Shield])
  97. {
  98. return buffList[(int)BuffType.Shield].Count != 0;
  99. }
  100. }
  101. }
  102. public void AddLIFE(int totelTime) => AddBuff(new BuffValue(), totelTime, BuffType.AddLIFE, () =>
  103. { });
  104. public bool HasLIFE
  105. {
  106. get
  107. {
  108. lock (buffListLock[(int)BuffType.AddLIFE])
  109. {
  110. return buffList[(int)BuffType.AddLIFE].Count != 0;
  111. }
  112. }
  113. }
  114. public bool TryActivatingLIFE()
  115. {
  116. if (HasLIFE)
  117. {
  118. lock (buffListLock[(int)BuffType.AddLIFE])
  119. {
  120. buffList[(int)BuffType.AddLIFE].Clear();
  121. }
  122. return true;
  123. }
  124. return false;
  125. }
  126. public void AddSpear(int spearTime) => AddBuff(new BuffValue(), spearTime, BuffType.Spear, () =>
  127. { });
  128. public bool HasSpear
  129. {
  130. get
  131. {
  132. lock (buffListLock[(int)BuffType.Spear])
  133. {
  134. return buffList[(int)BuffType.Spear].Count != 0;
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 清除所有buff
  140. /// </summary>
  141. public void ClearAll()
  142. {
  143. for (int i = 0; i < buffList.Length; ++i)
  144. {
  145. lock (buffListLock[i])
  146. {
  147. buffList[i].Clear();
  148. }
  149. }
  150. }
  151. public BuffManager()
  152. {
  153. var buffTypeArray = Enum.GetValues(typeof(BuffType));
  154. buffList = new LinkedList<BuffValue>[buffTypeArray.Length];
  155. buffListLock = new object[buffList.Length];
  156. int i = 0;
  157. foreach (BuffType type in buffTypeArray)
  158. {
  159. buffList[i] = new LinkedList<BuffValue>();
  160. buffListLock[i++] = new object();
  161. }
  162. }
  163. }
  164. }
  165. }