Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 15 Sep 2023 7 KB 动态创建扇形Api
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 常用函数工具类
  5. /// </summary>
  6. public static class Utils
  7. {
  8. /// <summary>
  9. /// 默认随机数对象
  10. /// </summary>
  11. public static SeedRandom Random { get; }
  12. static Utils()
  13. {
  14. Random = new SeedRandom();
  15. GD.Print("随机种子为: ", Random.Seed);
  16. }
  17.  
  18. /// <summary>
  19. /// 根据四个点计算出矩形
  20. /// </summary>
  21. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  22. {
  23. return new Rect2(
  24. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  25. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  26. );
  27. }
  28. /// <summary>
  29. /// 返回碰撞层 mask 是否会检测 layer
  30. /// </summary>
  31. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  32. {
  33. return (mask & layer) != 0;
  34. }
  35.  
  36. /// <summary>
  37. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  38. /// </summary>
  39. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, float width = 1)
  40. {
  41. for (var i = 0; i < polygonData.Length; i++)
  42. {
  43. var item = polygonData[i];
  44. var points = item.GetPoints();
  45. if (points.Length>= 2)
  46. {
  47. var array = new Vector2[points.Length + 1];
  48. for (var j = 0; j < points.Length; j++)
  49. {
  50. array[j] = points[j];
  51. }
  52.  
  53. array[array.Length - 1] = points[0];
  54. if (item.Type == NavigationPolygonType.In)
  55. {
  56. canvasItem.DrawPolyline(array, Colors.Orange, width);
  57. }
  58. else
  59. {
  60. canvasItem.DrawPolyline(array, Colors.Orange, width);
  61. }
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// 将一个任意角度转为0到360度
  67. /// </summary>
  68. public static float ConvertAngle(float angle)
  69. {
  70. angle %= 360; // 取余
  71.  
  72. if (angle < 0) // 如果角度为负数,转为正数
  73. {
  74. angle += 360;
  75. }
  76.  
  77. return angle;
  78. }
  79. /// <summary>
  80. /// 根据步长吸附值
  81. /// </summary>
  82. /// <param name="value">原数值</param>
  83. /// <param name="step">吸附步长</param>
  84. public static float Adsorption(float value, float step)
  85. {
  86. var f = Mathf.Round(value / step);
  87. return f * step;
  88. }
  89. /// <summary>
  90. /// 根据步长吸附值
  91. /// </summary>
  92. /// <param name="value">原数值</param>
  93. /// <param name="step">吸附步长</param>
  94. public static int Adsorption(float value, int step)
  95. {
  96. var f = Mathf.RoundToInt(value / step);
  97. return f * step;
  98. }
  99. /// <summary>
  100. /// 根据步长吸附值
  101. /// </summary>
  102. /// <param name="value">原数值</param>
  103. /// <param name="step">吸附步长</param>
  104. public static Vector2 Adsorption(Vector2 value, Vector2 step)
  105. {
  106. var x = Mathf.Round(value.X / step.X);
  107. var y = Mathf.Round(value.Y / step.Y);
  108. return new Vector2(x * step.X, y * step.Y);
  109. }
  110. /// <summary>
  111. /// 根据步长吸附值
  112. /// </summary>
  113. /// <param name="value">原数值</param>
  114. /// <param name="step">吸附步长</param>
  115. public static Vector2I Adsorption(Vector2 value, Vector2I step)
  116. {
  117. var x = Mathf.RoundToInt(value.X / step.X);
  118. var y = Mathf.RoundToInt(value.Y / step.Y);
  119. return new Vector2I(x * step.X, y * step.Y);
  120. }
  121.  
  122. /// <summary>
  123. /// 字符串首字母小写
  124. /// </summary>
  125. public static string FirstToLower(this string str)
  126. {
  127. return str.Substring(0, 1).ToLower() + str.Substring(1);
  128. }
  129. /// <summary>
  130. /// 字符串首字母大写
  131. /// </summary>
  132. public static string FirstToUpper(this string str)
  133. {
  134. return str.Substring(0, 1).ToUpper() + str.Substring(1);
  135. }
  136.  
  137. /// <summary>
  138. /// 将 Vector2 类型转为 Vector2I 类型
  139. /// </summary>
  140. public static Vector2I AsVector2I(this Vector2 vector2)
  141. {
  142. return new Vector2I((int)vector2.X, (int)vector2.Y);
  143. }
  144.  
  145. /// <summary>
  146. /// 返回指定坐标是否在UI节范围点内
  147. /// </summary>
  148. public static bool IsPositionOver(this Control control, Vector2 position)
  149. {
  150. var globalPosition = control.GlobalPosition;
  151. var size = control.Size * control.Scale;
  152. return position.X >= globalPosition.X && position.X <= (globalPosition.X + size.X) &&
  153. position.Y >= globalPosition.Y && position.Y <= (globalPosition.Y + size.Y);
  154. }
  155.  
  156. /// <summary>
  157. /// 判断点是否在区域内
  158. /// </summary>
  159. public static bool IsPositionInRect(Vector2 pos, Rect2 rect2)
  160. {
  161. return pos.X >= rect2.Position.X && pos.X <= rect2.Position.X + rect2.Size.X &&
  162. pos.Y >= rect2.Position.Y && pos.Y <= rect2.Position.Y + rect2.Size.Y;
  163. }
  164.  
  165. /// <summary>
  166. /// 返回区域起始值, 用于获取配置表范围配置数据
  167. /// </summary>
  168. public static int GetConfigRangeStart(int[] range)
  169. {
  170. return range[0];
  171. }
  172. /// <summary>
  173. /// 返回区域结束值, 用于获取配置表范围配置数据
  174. /// </summary>
  175. public static int GetConfigRangeEnd(int[] range)
  176. {
  177. if (range.Length > 1)
  178. {
  179. return range[1];
  180. }
  181.  
  182. return range[0];
  183. }
  184. /// <summary>
  185. /// 返回区域起始值, 用于获取配置表范围配置数据
  186. /// </summary>
  187. public static float GetConfigRangeStart(float[] range)
  188. {
  189. return range[0];
  190. }
  191. /// <summary>
  192. /// 返回区域结束值, 用于获取配置表范围配置数据
  193. /// </summary>
  194. public static float GetConfigRangeEnd(float[] range)
  195. {
  196. if (range.Length > 1)
  197. {
  198. return range[1];
  199. }
  200.  
  201. return range[0];
  202. }
  203.  
  204. /// <summary>
  205. /// 创建扇形多边形区域数据, 返回坐标点
  206. /// </summary>
  207. /// <param name="centerAngle">中心角度, 角度制</param>
  208. /// <param name="radius">扇形半径</param>
  209. /// <param name="range">扇形开口角度, 角度制</param>
  210. /// <param name="edgesCount">扇形弧度边的数量</param>
  211. /// <param name="offset">整体偏移坐标, 默认0</param>
  212. public static Vector2[] CreateSectorPolygon(float centerAngle, float radius, float range, uint edgesCount, Vector2? offset = null)
  213. {
  214. var point = new Vector2[edgesCount + 2];
  215. var edgesAngle = range / edgesCount;
  216. var startAngle = centerAngle - range * 0.5f;
  217. var temp = new Vector2(radius, 0);
  218.  
  219. for (var i = 0; i <= edgesCount; i++)
  220. {
  221. if (offset == null)
  222. {
  223. point[i] = temp.Rotated(Mathf.DegToRad(startAngle + edgesAngle * i));
  224. }
  225. else
  226. {
  227. point[i] = temp.Rotated(Mathf.DegToRad(startAngle + edgesAngle * i)) + offset.Value;
  228. }
  229. }
  230.  
  231. if (offset == null)
  232. {
  233. point[point.Length - 1] = Vector2.Zero;
  234. }
  235. else
  236. {
  237. point[point.Length - 1] = offset.Value;
  238. }
  239. return point;
  240. }
  241. }