Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 24 Dec 2023 15 KB 测试动态烘焙导航网格
  1. using System.Collections.Generic;
  2. using Godot;
  3. using UI.TileSetEditorCombination;
  4.  
  5. /// <summary>
  6. /// 常用函数工具类
  7. /// </summary>
  8. public static class Utils
  9. {
  10. /// <summary>
  11. /// 默认随机数对象
  12. /// </summary>
  13. public static SeedRandom Random { get; private set; }
  14. /// <summary>
  15. /// 初始化随机种子
  16. /// </summary>
  17. public static void InitRandom()
  18. {
  19. Random = new SeedRandom();
  20. Debug.Log("随机种子为: ", Random.Seed);
  21. }
  22.  
  23. /// <summary>
  24. /// 根据两个点计算出矩形
  25. /// </summary>
  26. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  27. {
  28. return new Rect2(
  29. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  30. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  31. );
  32. }
  33. /// <summary>
  34. /// 根据两个点计算出矩形
  35. /// </summary>
  36. public static Rect2I CalcRect(int start1, int end1, int start2, int end2)
  37. {
  38. return new Rect2I(
  39. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  40. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  41. );
  42. }
  43. /// <summary>
  44. /// 返回碰撞层 mask 是否会检测 layer
  45. /// </summary>
  46. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  47. {
  48. return (mask & layer) != 0;
  49. }
  50.  
  51. /// <summary>
  52. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  53. /// </summary>
  54. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, float width = 1)
  55. {
  56. for (var i = 0; i < polygonData.Length; i++)
  57. {
  58. var item = polygonData[i];
  59. var points = item.GetPoints();
  60. if (points.Length>= 2)
  61. {
  62. var array = new Vector2[points.Length + 1];
  63. for (var j = 0; j < points.Length; j++)
  64. {
  65. array[j] = points[j];
  66. }
  67. array[array.Length - 1] = points[0];
  68. if (item.Type == NavigationPolygonType.In)
  69. {
  70. canvasItem.DrawPolyline(points, Colors.Orange, width);
  71. }
  72. else
  73. {
  74. canvasItem.DrawPolyline(points, Colors.Orange, width);
  75. }
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 将一个任意角度转为0到360度
  81. /// </summary>
  82. public static float ConvertAngle(float angle)
  83. {
  84. angle %= 360; // 取余
  85.  
  86. if (angle < 0) // 如果角度为负数,转为正数
  87. {
  88. angle += 360;
  89. }
  90.  
  91. return angle;
  92. }
  93. /// <summary>
  94. /// 根据步长吸附值
  95. /// </summary>
  96. /// <param name="value">原数值</param>
  97. /// <param name="step">吸附步长</param>
  98. public static float Adsorption(this float value, float step)
  99. {
  100. var f = Mathf.Round(value / step);
  101. return f * step;
  102. }
  103. /// <summary>
  104. /// 根据步长吸附值
  105. /// </summary>
  106. /// <param name="value">原数值</param>
  107. /// <param name="step">吸附步长</param>
  108. public static int Adsorption(this float value, int step)
  109. {
  110. var f = Mathf.RoundToInt(value / step);
  111. return f * step;
  112. }
  113. /// <summary>
  114. /// 根据步长吸附值
  115. /// </summary>
  116. /// <param name="value">原数值</param>
  117. /// <param name="step">吸附步长</param>
  118. public static Vector2 Adsorption(this Vector2 value, Vector2 step)
  119. {
  120. var x = Mathf.Round(value.X / step.X);
  121. var y = Mathf.Round(value.Y / step.Y);
  122. return new Vector2(x * step.X, y * step.Y);
  123. }
  124. /// <summary>
  125. /// 根据步长吸附值
  126. /// </summary>
  127. /// <param name="value">原数值</param>
  128. /// <param name="step">吸附步长</param>
  129. public static Vector2I Adsorption(this Vector2 value, Vector2I step)
  130. {
  131. var x = Mathf.RoundToInt(value.X / step.X);
  132. var y = Mathf.RoundToInt(value.Y / step.Y);
  133. return new Vector2I(x * step.X, y * step.Y);
  134. }
  135. /// <summary>
  136. /// 根据步长按照 Floor() 函数吸附值
  137. /// </summary>
  138. /// <param name="value">原数值</param>
  139. /// <param name="step">吸附步长</param>
  140. public static float FloorAdsorption(this float value, float step)
  141. {
  142. var f = Mathf.Floor(value / step);
  143. return f * step;
  144. }
  145. /// <summary>
  146. /// 根据步长按照 Floor() 函数吸附值
  147. /// </summary>
  148. /// <param name="value">原数值</param>
  149. /// <param name="step">吸附步长</param>
  150. public static int FloorAdsorption(this float value, int step)
  151. {
  152. var f = Mathf.FloorToInt(value / step);
  153. return f * step;
  154. }
  155.  
  156. /// <summary>
  157. /// 根据步长按照 Floor() 函数吸附值
  158. /// </summary>
  159. /// <param name="value">原数值</param>
  160. /// <param name="step">吸附步长</param>
  161. public static Vector2 FloorAdsorption(this Vector2 value, Vector2 step)
  162. {
  163. var x = Mathf.Floor(value.X / step.X);
  164. var y = Mathf.Floor(value.Y / step.Y);
  165. return new Vector2(x * step.X, y * step.Y);
  166. }
  167. /// <summary>
  168. /// 根据步长按照 Floor() 函数吸附值
  169. /// </summary>
  170. /// <param name="value">原数值</param>
  171. /// <param name="step">吸附步长</param>
  172. public static Vector2I FloorAdsorption(this Vector2 value, Vector2I step)
  173. {
  174. var x = Mathf.FloorToInt(value.X / step.X);
  175. var y = Mathf.FloorToInt(value.Y / step.Y);
  176. return new Vector2I(x * step.X, y * step.Y);
  177. }
  178.  
  179. /// <summary>
  180. /// 字符串首字母小写
  181. /// </summary>
  182. public static string FirstToLower(this string str)
  183. {
  184. return str.Substring(0, 1).ToLower() + str.Substring(1);
  185. }
  186. /// <summary>
  187. /// 字符串首字母大写
  188. /// </summary>
  189. public static string FirstToUpper(this string str)
  190. {
  191. return str.Substring(0, 1).ToUpper() + str.Substring(1);
  192. }
  193.  
  194. /// <summary>
  195. /// 将 Vector2 类型转为 Vector2I 类型
  196. /// </summary>
  197. public static Vector2I AsVector2I(this Vector2 vector2)
  198. {
  199. return new Vector2I((int)vector2.X, (int)vector2.Y);
  200. }
  201.  
  202. /// <summary>
  203. /// 返回指定坐标是否在UI节范围点内
  204. /// </summary>
  205. public static bool IsPositionOver(this Control control, Vector2 position)
  206. {
  207. var globalPosition = control.GlobalPosition;
  208. var size = control.Size * control.Scale;
  209. return position.X >= globalPosition.X && position.X <= (globalPosition.X + size.X) &&
  210. position.Y >= globalPosition.Y && position.Y <= (globalPosition.Y + size.Y);
  211. }
  212.  
  213. /// <summary>
  214. /// 判断点是否在区域内
  215. /// </summary>
  216. public static bool IsPositionInRect(Vector2 pos, Rect2 rect2)
  217. {
  218. return pos.X >= rect2.Position.X && pos.X <= rect2.Position.X + rect2.Size.X &&
  219. pos.Y >= rect2.Position.Y && pos.Y <= rect2.Position.Y + rect2.Size.Y;
  220. }
  221.  
  222. /// <summary>
  223. /// 返回区域起始值, 用于获取配置表范围配置数据
  224. /// </summary>
  225. public static int GetConfigRangeStart(int[] range)
  226. {
  227. return range[0];
  228. }
  229. /// <summary>
  230. /// 返回区域结束值, 用于获取配置表范围配置数据
  231. /// </summary>
  232. public static int GetConfigRangeEnd(int[] range)
  233. {
  234. if (range.Length > 1)
  235. {
  236. return range[1];
  237. }
  238.  
  239. return range[0];
  240. }
  241. /// <summary>
  242. /// 返回区域起始值, 用于获取配置表范围配置数据
  243. /// </summary>
  244. public static float GetConfigRangeStart(float[] range)
  245. {
  246. return range[0];
  247. }
  248. /// <summary>
  249. /// 返回区域结束值, 用于获取配置表范围配置数据
  250. /// </summary>
  251. public static float GetConfigRangeEnd(float[] range)
  252. {
  253. if (range.Length > 1)
  254. {
  255. return range[1];
  256. }
  257.  
  258. return range[0];
  259. }
  260.  
  261. /// <summary>
  262. /// 创建扇形多边形区域数据, 返回坐标点
  263. /// </summary>
  264. /// <param name="centerAngle">中心角度, 角度制</param>
  265. /// <param name="radius">扇形半径</param>
  266. /// <param name="range">扇形开口角度, 角度制</param>
  267. /// <param name="edgesCount">扇形弧度边的数量</param>
  268. /// <param name="offset">整体偏移坐标, 默认0</param>
  269. public static Vector2[] CreateSectorPolygon(float centerAngle, float radius, float range, uint edgesCount, Vector2? offset = null)
  270. {
  271. var point = new Vector2[edgesCount + 2];
  272. var edgesAngle = range / edgesCount;
  273. var startAngle = centerAngle - range * 0.5f;
  274. var temp = new Vector2(radius, 0);
  275.  
  276. for (var i = 0; i <= edgesCount; i++)
  277. {
  278. if (offset == null)
  279. {
  280. point[i] = temp.Rotated(Mathf.DegToRad(startAngle + edgesAngle * i));
  281. }
  282. else
  283. {
  284. point[i] = temp.Rotated(Mathf.DegToRad(startAngle + edgesAngle * i)) + offset.Value;
  285. }
  286. }
  287.  
  288. if (offset == null)
  289. {
  290. point[point.Length - 1] = Vector2.Zero;
  291. }
  292. else
  293. {
  294. point[point.Length - 1] = offset.Value;
  295. }
  296. return point;
  297. }
  298.  
  299. /// <summary>
  300. /// 将 point 位置限制在 anchor 的周围, 最大距离为 distance, 并返回新的位置
  301. /// </summary>
  302. public static Vector2 ConstrainDistance(Vector2 point, Vector2 anchor, float distance)
  303. {
  304. return (point - anchor).Normalized() * distance + anchor;
  305. }
  306. /// <summary>
  307. /// 返回一个点是否在 Polygon 内部
  308. /// </summary>
  309. /// <param name="polygon">多边形顶点</param>
  310. /// <param name="point">目标点</param>
  311. public static bool IsPointInPolygon(Vector2[] polygon, Vector2 point)
  312. {
  313. var isInside = false;
  314. for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++)
  315. {
  316. if ((polygon[i].Y > point.Y) != (polygon[j].Y > point.Y) &&
  317. point.X < (polygon[j].X - polygon[i].X) * (point.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) +
  318. polygon[i].X)
  319. {
  320. isInside = !isInside;
  321. }
  322. }
  323.  
  324. return isInside;
  325. }
  326.  
  327. /// <summary>
  328. /// 根据法线翻转向量
  329. /// </summary>
  330. public static Vector2 ReflectByNormal(Vector2 vector, Vector2 normal)
  331. {
  332. return vector.Reflect(normal.Rotated(Mathf.Pi * 0.5f));
  333. }
  334. /// <summary>
  335. /// 根据法线翻转角度, 弧度制
  336. /// </summary>
  337. public static float ReflectByNormal(float rotation, Vector2 normal)
  338. {
  339. return ReflectByNormal(Vector2.FromAngle(rotation), normal).Angle();
  340. }
  341.  
  342. /// <summary>
  343. /// 计算TileSet Cell所占用的区域
  344. /// </summary>
  345. public static Rect2I CalcTileRect(IEnumerable<Vector2I> cells)
  346. {
  347. //单位: 像素
  348. var canvasXStart = int.MaxValue;
  349. var canvasYStart = int.MaxValue;
  350. var canvasXEnd = int.MinValue;
  351. var canvasYEnd = int.MinValue;
  352.  
  353. foreach (var pos in cells)
  354. {
  355. canvasXStart = Mathf.Min(pos.X, canvasXStart);
  356. canvasYStart = Mathf.Min(pos.Y, canvasYStart);
  357. canvasXEnd = Mathf.Max(pos.X + GameConfig.TileCellSize, canvasXEnd);
  358. canvasYEnd = Mathf.Max(pos.Y + GameConfig.TileCellSize, canvasYEnd);
  359. }
  360.  
  361. return new Rect2I(
  362. canvasXStart,
  363. canvasYStart,
  364. canvasXEnd - canvasXStart,
  365. canvasYEnd - canvasYStart
  366. );
  367. }
  368. /// <summary>
  369. /// 计算TileSet Cell所占用的区域
  370. /// </summary>
  371. public static Rect2I CalcTileRect(IEnumerable<SerializeVector2> cells)
  372. {
  373. //单位: 像素
  374. var canvasXStart = float.MaxValue;
  375. var canvasYStart = float.MaxValue;
  376. var canvasXEnd = float.MinValue;
  377. var canvasYEnd = float.MinValue;
  378.  
  379. foreach (var pos in cells)
  380. {
  381. canvasXStart = Mathf.Min(pos.X, canvasXStart);
  382. canvasYStart = Mathf.Min(pos.Y, canvasYStart);
  383. canvasXEnd = Mathf.Max(pos.X + GameConfig.TileCellSize, canvasXEnd);
  384. canvasYEnd = Mathf.Max(pos.Y + GameConfig.TileCellSize, canvasYEnd);
  385. }
  386.  
  387. return new Rect2I(
  388. (int)canvasXStart,
  389. (int)canvasYStart,
  390. (int)(canvasXEnd - canvasXStart),
  391. (int)(canvasYEnd - canvasYStart)
  392. );
  393. }
  394.  
  395. /// <summary>
  396. /// 根据鼠标位置执行单步放大逻辑
  397. /// </summary>
  398. public static bool DoMagnifyByMousePosition(Control control, float maxXScale)
  399. {
  400. var offset = control.GetLocalMousePosition();
  401. var prevScale = control.Scale;
  402. var newScale = prevScale * 1.1f;
  403. if (newScale.X <= maxXScale)
  404. {
  405. control.Scale = newScale;
  406. var position = control.Position - offset * 0.1f * prevScale;
  407. control.Position = position;
  408. return true;
  409. }
  410.  
  411. return false;
  412. }
  413. /// <summary>
  414. /// 根据鼠标位置执行单步放大逻辑
  415. /// </summary>
  416. public static bool DoMagnifyByMousePosition(Node2D node, float maxXScale)
  417. {
  418. var offset = node.GetLocalMousePosition();
  419. var prevScale = node.Scale;
  420. var newScale = prevScale * 1.1f;
  421. if (newScale.X <= maxXScale)
  422. {
  423. node.Scale = newScale;
  424. var position = node.Position - offset * 0.1f * prevScale;
  425. node.Position = position;
  426. return true;
  427. }
  428.  
  429. return false;
  430. }
  431.  
  432. /// <summary>
  433. /// 根据鼠标位置执行单步缩小逻辑
  434. /// </summary>
  435. public static bool DoShrinkByMousePosition(Control control, float minXScale)
  436. {
  437. var offset = control.GetLocalMousePosition();
  438. var prevScale = control.Scale;
  439. var newScale = prevScale / 1.1f;
  440. if (newScale.X >= minXScale)
  441. {
  442. control.Scale = newScale;
  443. var position = control.Position + offset * 0.1f * newScale;
  444. control.Position = position;
  445. return true;
  446. }
  447.  
  448. return false;
  449. }
  450. /// <summary>
  451. /// 根据鼠标位置执行单步缩小逻辑
  452. /// </summary>
  453. public static bool DoShrinkByMousePosition(Node2D node, float minXScale)
  454. {
  455. var offset = node.GetLocalMousePosition();
  456. var prevScale = node.Scale;
  457. var newScale = prevScale / 1.1f;
  458. if (newScale.X >= minXScale)
  459. {
  460. node.Scale = newScale;
  461. var position = node.Position + offset * 0.1f * newScale;
  462. node.Position = position;
  463. return true;
  464. }
  465.  
  466. return false;
  467. }
  468.  
  469. /// <summary>
  470. /// 聚焦Ui节点
  471. /// </summary>
  472. /// <param name="control">需要聚焦的节点</param>
  473. /// <param name="parentSize">父节点容器大小</param>
  474. /// <param name="selfSize">当前节点容器大小</param>
  475. public static void DoFocusNode(Control control, Vector2 parentSize, Vector2 selfSize)
  476. {
  477. control.Position = parentSize / 2 - selfSize * 0.5f * control.Scale;
  478. }
  479.  
  480. /// <summary>
  481. /// 返回鼠标所在的单元格位置, 相对于Ui节点左上角
  482. /// </summary>
  483. public static Vector2I GetMouseCellPosition(Control control)
  484. {
  485. var pos = control.GetLocalMousePosition() / GameConfig.TileCellSize;
  486. return pos.AsVector2I();
  487. }
  488.  
  489. /// <summary>
  490. /// 创建一个数组, 并填充该对象
  491. /// </summary>
  492. public static T[] MakeArray<T>(this T data, int len)
  493. {
  494. var arr = new T[len];
  495. for (var i = 0; i < len; i++)
  496. {
  497. arr[i] = data;
  498. }
  499. return arr;
  500. }
  501. }