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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /// <summary>
  18. /// buff列表
  19. /// </summary>
  20. private readonly LinkedList<double>[] buffList;
  21. private readonly object[] buffListLock;
  22. private void AddBuff(double bf, int buffTime, BuffType buffType, Action ReCalculateFunc)
  23. {
  24. LinkedListNode<double> buffNode;
  25. lock (buffListLock[(int)buffType])
  26. {
  27. buffNode = buffList[(int)buffType].AddLast(bf);
  28. }
  29. ReCalculateFunc();
  30. new Thread
  31. (
  32. () =>
  33. {
  34. Thread.Sleep(buffTime);
  35. try
  36. {
  37. lock (buffListLock[(int)buffType])
  38. {
  39. buffList[(int)buffType].Remove(buffNode);
  40. }
  41. }
  42. catch
  43. {
  44. }
  45. ReCalculateFunc();
  46. }
  47. )
  48. { IsBackground = true }.Start();
  49. }
  50. public int ReCalculateFloatBuff(BuffType buffType, int orgVal, int maxVal, int minVal)
  51. {
  52. double times = 1.0;
  53. lock (buffListLock[(int)buffType])
  54. {
  55. foreach (var add in buffList[(int)buffType])
  56. {
  57. times *= add;
  58. }
  59. }
  60. return Math.Max(Math.Min((int)Math.Round(orgVal * times), maxVal), minVal);
  61. }
  62. public void AddMoveSpeed(double add, int buffTime, Action<int> SetNewMoveSpeed, int orgMoveSpeed) => AddBuff(add, buffTime, BuffType.AddSpeed, () => SetNewMoveSpeed(ReCalculateFloatBuff(BuffType.AddSpeed, orgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed)));
  63. public bool HasFasterSpeed
  64. {
  65. get
  66. {
  67. lock (buffListLock[(int)BuffType.AddSpeed])
  68. {
  69. return buffList[(int)BuffType.AddSpeed].Count != 0;
  70. }
  71. }
  72. }
  73. public void AddShield(int shieldTime) => AddBuff(0, shieldTime, BuffType.Shield, () =>
  74. { });
  75. public bool HasShield
  76. {
  77. get
  78. {
  79. lock (buffListLock[(int)BuffType.Shield])
  80. {
  81. return buffList[(int)BuffType.Shield].Count != 0;
  82. }
  83. }
  84. }
  85. public bool TryUseShield()
  86. {
  87. if (HasShield)
  88. {
  89. lock (buffListLock[(int)BuffType.Shield])
  90. {
  91. buffList[(int)BuffType.Shield].RemoveFirst();
  92. }
  93. return true;
  94. }
  95. return false;
  96. }
  97. public void AddAp(int time) => AddBuff(0, time, BuffType.AddAp, () => { });
  98. public bool HasAp
  99. {
  100. get
  101. {
  102. lock (buffListLock[(int)BuffType.AddAp])
  103. {
  104. return buffList[(int)BuffType.AddAp].Count != 0;
  105. }
  106. }
  107. }
  108. public bool TryAddAp()
  109. {
  110. if (HasAp)
  111. {
  112. lock (buffListLock[(int)BuffType.AddAp])
  113. {
  114. buffList[(int)BuffType.AddAp].RemoveFirst();
  115. }
  116. return true;
  117. }
  118. return false;
  119. }
  120. public void AddLife(int totelTime) => AddBuff(0, totelTime, BuffType.AddLife, () =>
  121. { });
  122. public bool HasLIFE
  123. {
  124. get
  125. {
  126. lock (buffListLock[(int)BuffType.AddLife])
  127. {
  128. return buffList[(int)BuffType.AddLife].Count != 0;
  129. }
  130. }
  131. }
  132. public bool TryActivatingLIFE()
  133. {
  134. if (HasLIFE)
  135. {
  136. lock (buffListLock[(int)BuffType.AddLife])
  137. {
  138. buffList[(int)BuffType.AddLife].RemoveFirst();
  139. }
  140. return true;
  141. }
  142. return false;
  143. }
  144. public void AddSpear(int spearTime) => AddBuff(0, spearTime, BuffType.Spear, () =>
  145. { });
  146. public bool HasSpear
  147. {
  148. get
  149. {
  150. lock (buffListLock[(int)BuffType.Spear])
  151. {
  152. return buffList[(int)BuffType.Spear].Count != 0;
  153. }
  154. }
  155. }
  156. public bool TryUseSpear()
  157. {
  158. if (HasSpear)
  159. {
  160. lock (buffListLock[(int)BuffType.Spear])
  161. {
  162. buffList[(int)BuffType.Spear].RemoveFirst();
  163. }
  164. return true;
  165. }
  166. return false;
  167. }
  168. public bool TryDeleteInvisible()
  169. {
  170. if (HasInvisible)
  171. {
  172. lock (buffListLock[(int)BuffType.Invisible])
  173. {
  174. buffList[(int)BuffType.Invisible].RemoveFirst();
  175. }
  176. return true;
  177. }
  178. return false;
  179. }
  180. public void AddClairaudience(int shieldTime) => AddBuff(0, shieldTime, BuffType.Clairaudience, () =>
  181. { });
  182. public bool HasClairaudience
  183. {
  184. get
  185. {
  186. lock (buffListLock[(int)BuffType.Clairaudience])
  187. {
  188. return buffList[(int)BuffType.Clairaudience].Count != 0;
  189. }
  190. }
  191. }
  192. public void AddInvisible(int shieldTime) => AddBuff(0, shieldTime, BuffType.Invisible, () =>
  193. { });
  194. public bool HasInvisible
  195. {
  196. get
  197. {
  198. lock (buffListLock[(int)BuffType.Invisible])
  199. {
  200. return buffList[(int)BuffType.Invisible].Count != 0;
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 清除所有buff
  206. /// </summary>
  207. public void ClearAll()
  208. {
  209. for (int i = 0; i < buffList.Length; ++i)
  210. {
  211. lock (buffListLock[i])
  212. {
  213. buffList[i].Clear();
  214. }
  215. }
  216. }
  217. public BuffManager()
  218. {
  219. var buffTypeArray = Enum.GetValues(typeof(BuffType));
  220. buffList = new LinkedList<double>[buffTypeArray.Length];
  221. buffListLock = new object[buffList.Length];
  222. int i = 0;
  223. foreach (BuffType type in buffTypeArray)
  224. {
  225. buffList[i] = new LinkedList<double>();
  226. buffListLock[i++] = new object();
  227. }
  228. }
  229. }
  230. public int ReCalculateBuff(BuffType buffType, int orgVal, int maxVal, int minVal)
  231. {
  232. return buffManager.ReCalculateFloatBuff(buffType, orgVal, maxVal, minVal);
  233. }
  234. }
  235. }