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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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. lock (buffListLock[(int)BuffType.Shield])
  96. {
  97. return buffList[(int)BuffType.Shield].Count != 0;
  98. }
  99. }
  100. }
  101. public void AddLIFE(int totelTime) => AddBuff(new BuffValue(), totelTime, BuffType.AddLIFE, () =>
  102. {});
  103. public bool HasLIFE
  104. {
  105. get {
  106. lock (buffListLock[(int)BuffType.AddLIFE])
  107. {
  108. return buffList[(int)BuffType.AddLIFE].Count != 0;
  109. }
  110. }
  111. }
  112. public bool TryActivatingLIFE()
  113. {
  114. if (HasLIFE)
  115. {
  116. lock (buffListLock[(int)BuffType.AddLIFE])
  117. {
  118. buffList[(int)BuffType.AddLIFE].Clear();
  119. }
  120. return true;
  121. }
  122. return false;
  123. }
  124. public void AddSpear(int spearTime) => AddBuff(new BuffValue(), spearTime, BuffType.Spear, () =>
  125. {});
  126. public bool HasSpear
  127. {
  128. get {
  129. lock (buffListLock[(int)BuffType.Spear])
  130. {
  131. return buffList[(int)BuffType.Spear].Count != 0;
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 清除所有buff
  137. /// </summary>
  138. public void ClearAll()
  139. {
  140. for (int i = 0; i < buffList.Length; ++i)
  141. {
  142. lock (buffListLock[i])
  143. {
  144. buffList[i].Clear();
  145. }
  146. }
  147. }
  148. public BuffManeger()
  149. {
  150. var buffTypeArray = Enum.GetValues(typeof(BuffType));
  151. buffList = new LinkedList<BuffValue>[buffTypeArray.Length];
  152. buffListLock = new object[buffList.Length];
  153. int i = 0;
  154. foreach (BuffType type in buffTypeArray)
  155. {
  156. buffList[i] = new LinkedList<BuffValue>();
  157. buffListLock[i++] = new object();
  158. }
  159. }
  160. }
  161. }
  162. }