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 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. private readonly BuffManager buffManager;
  11. /// <summary>
  12. /// 角色携带的buff管理器
  13. /// </summary>
  14. private 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. LinkedListNode<BuffValue> buffNode;
  42. lock (buffListLock[(int)buffType])
  43. {
  44. buffNode = buffList[(int)buffType].AddLast(bf);
  45. }
  46. ReCalculateFunc();
  47. new Thread
  48. (
  49. () =>
  50. {
  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 bool TryUseShield()
  103. {
  104. if (HasShield)
  105. {
  106. lock (buffListLock[(int)BuffType.Shield])
  107. {
  108. buffList[(int)BuffType.Shield].RemoveFirst();
  109. }
  110. return true;
  111. }
  112. return false;
  113. }
  114. public void AddAp(int time) => AddBuff(new BuffValue(), time, BuffType.AddAp, () => { });
  115. public bool HasAp
  116. {
  117. get
  118. {
  119. lock (buffListLock[(int)BuffType.AddAp])
  120. {
  121. return buffList[(int)BuffType.AddAp].Count != 0;
  122. }
  123. }
  124. }
  125. public bool TryAddAp()
  126. {
  127. if (HasAp)
  128. {
  129. lock (buffListLock[(int)BuffType.AddAp])
  130. {
  131. buffList[(int)BuffType.AddAp].RemoveFirst();
  132. }
  133. return true;
  134. }
  135. return false;
  136. }
  137. public void AddLIFE(int totelTime) => AddBuff(new BuffValue(), totelTime, BuffType.AddLIFE, () =>
  138. { });
  139. public bool HasLIFE
  140. {
  141. get
  142. {
  143. lock (buffListLock[(int)BuffType.AddLIFE])
  144. {
  145. return buffList[(int)BuffType.AddLIFE].Count != 0;
  146. }
  147. }
  148. }
  149. public bool TryActivatingLIFE()
  150. {
  151. if (HasLIFE)
  152. {
  153. lock (buffListLock[(int)BuffType.AddLIFE])
  154. {
  155. buffList[(int)BuffType.AddLIFE].RemoveFirst();
  156. }
  157. return true;
  158. }
  159. return false;
  160. }
  161. public void AddSpear(int spearTime) => AddBuff(new BuffValue(), spearTime, BuffType.Spear, () =>
  162. { });
  163. public bool HasSpear
  164. {
  165. get
  166. {
  167. lock (buffListLock[(int)BuffType.Spear])
  168. {
  169. return buffList[(int)BuffType.Spear].Count != 0;
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 清除所有buff
  175. /// </summary>
  176. public void ClearAll()
  177. {
  178. for (int i = 0; i < buffList.Length; ++i)
  179. {
  180. lock (buffListLock[i])
  181. {
  182. buffList[i].Clear();
  183. }
  184. }
  185. }
  186. public BuffManager()
  187. {
  188. var buffTypeArray = Enum.GetValues(typeof(BuffType));
  189. buffList = new LinkedList<BuffValue>[buffTypeArray.Length];
  190. buffListLock = new object[buffList.Length];
  191. int i = 0;
  192. foreach (BuffType type in buffTypeArray)
  193. {
  194. buffList[i] = new LinkedList<BuffValue>();
  195. buffListLock[i++] = new object();
  196. }
  197. }
  198. }
  199. public int ReCalculateBuff(BuffType buffType, int orgVal, int maxVal, int minVal)
  200. {
  201. return buffManager.ReCalculateFloatBuff(buffType, orgVal, maxVal, minVal);
  202. }
  203. }
  204. }