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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using Preparation.Utility;
  7. namespace GameClass.GameObj
  8. {
  9. public partial class Character
  10. {
  11. private readonly BuffManager buffManager;
  12. /// <summary>
  13. /// 角色携带的buff管理器
  14. /// </summary>
  15. private class BuffManager
  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. LinkedListNode<BuffValue> buffNode;
  43. lock (buffListLock[(int)buffType])
  44. {
  45. buffNode = buffList[(int)buffType].AddLast(bf);
  46. }
  47. ReCalculateFunc();
  48. new Thread
  49. (
  50. () =>
  51. {
  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. public 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 bool TryUseShield()
  104. {
  105. if (HasShield)
  106. {
  107. lock (buffListLock[(int)BuffType.Shield])
  108. {
  109. buffList[(int)BuffType.Shield].RemoveFirst();
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. public void AddAp(int time) => AddBuff(new BuffValue(), time, BuffType.AddAp, () => { });
  116. public bool HasAp
  117. {
  118. get
  119. {
  120. lock (buffListLock[(int)BuffType.AddAp])
  121. {
  122. return buffList[(int)BuffType.AddAp].Count != 0;
  123. }
  124. }
  125. }
  126. public bool TryAddAp()
  127. {
  128. if (HasAp)
  129. {
  130. lock (buffListLock[(int)BuffType.AddAp])
  131. {
  132. buffList[(int)BuffType.AddAp].RemoveFirst();
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. public void AddLIFE(int totelTime) => AddBuff(new BuffValue(), totelTime, BuffType.AddLIFE, () =>
  139. { });
  140. public bool HasLIFE
  141. {
  142. get
  143. {
  144. lock (buffListLock[(int)BuffType.AddLIFE])
  145. {
  146. return buffList[(int)BuffType.AddLIFE].Count != 0;
  147. }
  148. }
  149. }
  150. public bool TryActivatingLIFE()
  151. {
  152. if (HasLIFE)
  153. {
  154. lock (buffListLock[(int)BuffType.AddLIFE])
  155. {
  156. buffList[(int)BuffType.AddLIFE].RemoveFirst();
  157. }
  158. return true;
  159. }
  160. return false;
  161. }
  162. public void AddSpear(int spearTime) => AddBuff(new BuffValue(), spearTime, BuffType.Spear, () =>
  163. { });
  164. public bool HasSpear
  165. {
  166. get
  167. {
  168. lock (buffListLock[(int)BuffType.Spear])
  169. {
  170. return buffList[(int)BuffType.Spear].Count != 0;
  171. }
  172. }
  173. }
  174. public bool TryUseSpear()
  175. {
  176. if (HasSpear)
  177. {
  178. lock (buffListLock[(int)BuffType.Spear])
  179. {
  180. buffList[(int)BuffType.Spear].RemoveFirst();
  181. }
  182. return true;
  183. }
  184. return false;
  185. }
  186. public void AddClairaudience(int shieldTime) => AddBuff(new BuffValue(), shieldTime, BuffType.Clairaudience, () =>
  187. { });
  188. public bool HasClairaudience
  189. {
  190. get
  191. {
  192. lock (buffListLock[(int)BuffType.Clairaudience])
  193. {
  194. return buffList[(int)BuffType.Clairaudience].Count != 0;
  195. }
  196. }
  197. }
  198. public void AddInvisible(int shieldTime) => AddBuff(new BuffValue(), shieldTime, BuffType.Invisible, () =>
  199. { });
  200. public bool HasInvisible
  201. {
  202. get
  203. {
  204. lock (buffListLock[(int)BuffType.Invisible])
  205. {
  206. return buffList[(int)BuffType.Invisible].Count != 0;
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 清除所有buff
  212. /// </summary>
  213. public void ClearAll()
  214. {
  215. for (int i = 0; i < buffList.Length; ++i)
  216. {
  217. lock (buffListLock[i])
  218. {
  219. buffList[i].Clear();
  220. }
  221. }
  222. }
  223. public BuffManager()
  224. {
  225. var buffTypeArray = Enum.GetValues(typeof(BuffType));
  226. buffList = new LinkedList<BuffValue>[buffTypeArray.Length];
  227. buffListLock = new object[buffList.Length];
  228. int i = 0;
  229. foreach (BuffType type in buffTypeArray)
  230. {
  231. buffList[i] = new LinkedList<BuffValue>();
  232. buffListLock[i++] = new object();
  233. }
  234. }
  235. }
  236. public int ReCalculateBuff(BuffType buffType, int orgVal, int maxVal, int minVal)
  237. {
  238. return buffManager.ReCalculateFloatBuff(buffType, orgVal, maxVal, minVal);
  239. }
  240. }
  241. }