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.4 kB

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