Helper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using CommonAI.RTS;
  2. using CommonLang.Vector;
  3. using CommonAI.Zone.Instance;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using CommonLang;
  8. namespace CommonAI.Zone.Helper
  9. {
  10. public delegate bool Select<T>(T obj);
  11. public class RangeValue
  12. {
  13. private int mMin = 0;
  14. private int mMax = 0;
  15. private int mValue = 0;
  16. public int Min { get { return mMin; } }
  17. public int Max { get { return mMax; } }
  18. public int Value { get { return mValue; } }
  19. public RangeValue(int value, int min, int max)
  20. {
  21. mMin = Math.Min(min, max);
  22. mMax = Math.Max(min, max);
  23. SetValue(value);
  24. }
  25. public bool SetMin(int min)
  26. {
  27. if (min != mMin && min <= mMax)
  28. {
  29. mMin = min;
  30. mValue = Math.Min(mValue, mMax);
  31. mValue = Math.Max(mValue, mMin);
  32. return true;
  33. }
  34. return false;
  35. }
  36. public bool SetMax(int max, bool autoGenValue = false)
  37. {
  38. if (max != mMax && max >= mMin)
  39. {
  40. if (autoGenValue)
  41. {
  42. float addrat = (max / (float)mMax) - 1f;
  43. mMax = max;
  44. Add(CUtils.CastInt(mValue * addrat));
  45. }
  46. else
  47. {
  48. mMax = max;
  49. mValue = Math.Min(mValue, mMax);
  50. mValue = Math.Max(mValue, mMin);
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. public bool SetValue(int value)
  57. {
  58. if (value != mValue)
  59. {
  60. value = Math.Min(value, mMax);
  61. value = Math.Max(value, mMin);
  62. if (value != mValue)
  63. {
  64. mValue = value;
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. public bool Add(int add)
  71. {
  72. if (add != 0)
  73. {
  74. return SetValue(mValue + add);
  75. }
  76. return false;
  77. }
  78. }
  79. public class RangeValueF
  80. {
  81. private float mMin = 0;
  82. private float mMax = 0;
  83. private float mValue = 0;
  84. public float Min { get { return mMin; } }
  85. public float Max { get { return mMax; } }
  86. public float Value { get { return mValue; } }
  87. public RangeValueF(float value, float min, float max)
  88. {
  89. mMin = Math.Min(min, max);
  90. mMax = Math.Max(min, max);
  91. SetValue(value);
  92. }
  93. public bool SetMin(float min)
  94. {
  95. if (min != mMin && min <= mMax)
  96. {
  97. mMin = min;
  98. mValue = Math.Min(mValue, mMax);
  99. mValue = Math.Max(mValue, mMin);
  100. return true;
  101. }
  102. return false;
  103. }
  104. public bool SetMax(float max, bool autoGenValue = false)
  105. {
  106. if (max != mMax && max >= mMin)
  107. {
  108. if (autoGenValue)
  109. {
  110. float addrat = (max / mMax) - 1f;
  111. mMax = max;
  112. Add(mValue * addrat);
  113. }
  114. else
  115. {
  116. mMax = max;
  117. mValue = Math.Min(mValue, mMax);
  118. mValue = Math.Max(mValue, mMin);
  119. }
  120. return true;
  121. }
  122. return false;
  123. }
  124. public bool SetValue(float value)
  125. {
  126. if (value != mValue)
  127. {
  128. value = Math.Min(value, mMax);
  129. value = Math.Max(value, mMin);
  130. if (value != mValue)
  131. {
  132. mValue = value;
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. public bool Add(float add)
  139. {
  140. if (add != 0)
  141. {
  142. return SetValue(mValue + add);
  143. }
  144. return false;
  145. }
  146. }
  147. /// <summary>
  148. /// 从小到大排序
  149. /// </summary>
  150. /// <typeparam name="T"></typeparam>
  151. public struct ObjectSorterNearest<T> : IComparer<T> where T : IPositionObject
  152. {
  153. private float X;
  154. private float Y;
  155. public ObjectSorterNearest(float x, float y)
  156. {
  157. this.X = x;
  158. this.Y = y;
  159. }
  160. public int Compare(T x, T y)
  161. {
  162. float d0 = MathVector.getDistanceSquare(x.X, x.Y, this.X, this.Y);
  163. float d1 = MathVector.getDistanceSquare(y.X, y.Y, this.X, this.Y);
  164. if (d0 < d1)
  165. return -1;
  166. if (d0 > d1)
  167. return 1;
  168. return 0;
  169. }
  170. }
  171. /// <summary>
  172. /// 从大到小排序
  173. /// </summary>
  174. /// <typeparam name="T"></typeparam>
  175. public struct ObjectSorterFarthest<T> : IComparer<T> where T : IPositionObject
  176. {
  177. private float X;
  178. private float Y;
  179. public ObjectSorterFarthest(float x, float y)
  180. {
  181. this.X = x;
  182. this.Y = y;
  183. }
  184. public int Compare(T x, T y)
  185. {
  186. float d0 = MathVector.getDistanceSquare(x.X, x.Y, this.X, this.Y);
  187. float d1 = MathVector.getDistanceSquare(y.X, y.Y, this.X, this.Y);
  188. if (d0 < d1)
  189. return 1;
  190. if (d0 > d1)
  191. return -1;
  192. return 0;
  193. }
  194. }
  195. /// <summary>
  196. /// 从小到大排序
  197. /// </summary>
  198. /// <typeparam name="T"></typeparam>
  199. public struct ObjectBodySorterNearest<T> : IComparer<T> where T : IPositionObject
  200. {
  201. private float X;
  202. private float Y;
  203. private float R;
  204. public ObjectBodySorterNearest(float x, float y, float r)
  205. {
  206. this.X = x;
  207. this.Y = y;
  208. this.R = r;
  209. }
  210. public int Compare(T x, T y)
  211. {
  212. float d0 = Math.Abs(MathVector.getDistance(x.X, x.Y, this.X, this.Y) - (x.RadiusSize + R));
  213. float d1 = Math.Abs(MathVector.getDistance(y.X, y.Y, this.X, this.Y) - (y.RadiusSize + R));
  214. if (d0 < d1)
  215. return -1;
  216. if (d0 > d1)
  217. return 1;
  218. return 0;
  219. }
  220. }
  221. /// <summary>
  222. /// 从大到小排序
  223. /// </summary>
  224. /// <typeparam name="T"></typeparam>
  225. public struct ObjectBodySorterFarthest<T> : IComparer<T> where T : IPositionObject
  226. {
  227. private float X;
  228. private float Y;
  229. private float R;
  230. public ObjectBodySorterFarthest(float x, float y, float r)
  231. {
  232. this.X = x;
  233. this.Y = y;
  234. this.R = r;
  235. }
  236. public int Compare(T x, T y)
  237. {
  238. float d0 = Math.Abs(MathVector.getDistance(x.X, x.Y, this.X, this.Y) - (x.RadiusSize + R));
  239. float d1 = Math.Abs(MathVector.getDistance(y.X, y.Y, this.X, this.Y) - (y.RadiusSize + R));
  240. if (d0 < d1)
  241. return 1;
  242. if (d0 > d1)
  243. return -1;
  244. return 0;
  245. }
  246. }
  247. }