Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 29 Aug 2023 4 KB 预生成房间数据
  1. using System;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 常用函数工具类
  7. /// </summary>
  8. public static class Utils
  9. {
  10. /// <summary>
  11. /// 默认随机数对象
  12. /// </summary>
  13. public static SeedRandom Random { get; }
  14. static Utils()
  15. {
  16. Random = new SeedRandom();
  17. GD.Print("随机种子为: ", Random.Seed);
  18. }
  19.  
  20. /// <summary>
  21. /// 根据四个点计算出矩形
  22. /// </summary>
  23. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  24. {
  25. return new Rect2(
  26. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  27. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  28. );
  29. }
  30. /// <summary>
  31. /// 返回碰撞层 mask 是否会检测 layer
  32. /// </summary>
  33. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  34. {
  35. return (mask & layer) != 0;
  36. }
  37.  
  38. /// <summary>
  39. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  40. /// </summary>
  41. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, float width = 1)
  42. {
  43. for (var i = 0; i < polygonData.Length; i++)
  44. {
  45. var item = polygonData[i];
  46. var points = item.GetPoints();
  47. if (points.Length>= 2)
  48. {
  49. var array = new Vector2[points.Length + 1];
  50. for (var j = 0; j < points.Length; j++)
  51. {
  52. array[j] = points[j];
  53. }
  54.  
  55. array[array.Length - 1] = points[0];
  56. if (item.Type == NavigationPolygonType.In)
  57. {
  58. canvasItem.DrawPolyline(array, Colors.Orange, width);
  59. }
  60. else
  61. {
  62. canvasItem.DrawPolyline(array, Colors.Orange, width);
  63. }
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 将一个任意角度转为0到360度
  69. /// </summary>
  70. public static float ConvertAngle(float angle)
  71. {
  72. angle %= 360; // 取余
  73.  
  74. if (angle < 0) // 如果角度为负数,转为正数
  75. {
  76. angle += 360;
  77. }
  78.  
  79. return angle;
  80. }
  81. /// <summary>
  82. /// 根据步长吸附值
  83. /// </summary>
  84. /// <param name="value">原数值</param>
  85. /// <param name="step">吸附步长</param>
  86. public static float Adsorption(float value, float step)
  87. {
  88. var f = Mathf.Round(value / step);
  89. return f * step;
  90. }
  91. /// <summary>
  92. /// 根据步长吸附值
  93. /// </summary>
  94. /// <param name="value">原数值</param>
  95. /// <param name="step">吸附步长</param>
  96. public static int Adsorption(float value, int step)
  97. {
  98. var f = Mathf.RoundToInt(value / step);
  99. return f * step;
  100. }
  101. /// <summary>
  102. /// 根据步长吸附值
  103. /// </summary>
  104. /// <param name="value">原数值</param>
  105. /// <param name="step">吸附步长</param>
  106. public static Vector2 Adsorption(Vector2 value, Vector2 step)
  107. {
  108. var x = Mathf.Round(value.X / step.X);
  109. var y = Mathf.Round(value.Y / step.Y);
  110. return new Vector2(x * step.X, y * step.Y);
  111. }
  112. /// <summary>
  113. /// 根据步长吸附值
  114. /// </summary>
  115. /// <param name="value">原数值</param>
  116. /// <param name="step">吸附步长</param>
  117. public static Vector2I Adsorption(Vector2 value, Vector2I step)
  118. {
  119. var x = Mathf.RoundToInt(value.X / step.X);
  120. var y = Mathf.RoundToInt(value.Y / step.Y);
  121. return new Vector2I(x * step.X, y * step.Y);
  122. }
  123.  
  124. /// <summary>
  125. /// 字符串首字母小写
  126. /// </summary>
  127. public static string FirstToLower(this string str)
  128. {
  129. return str.Substring(0, 1).ToLower() + str.Substring(1);
  130. }
  131. /// <summary>
  132. /// 字符串首字母大写
  133. /// </summary>
  134. public static string FirstToUpper(this string str)
  135. {
  136. return str.Substring(0, 1).ToUpper() + str.Substring(1);
  137. }
  138.  
  139. /// <summary>
  140. /// 将 Vector2 类型转为 Vector2I 类型
  141. /// </summary>
  142. public static Vector2I AsVector2I(this Vector2 vector2)
  143. {
  144. return new Vector2I((int)vector2.X, (int)vector2.Y);
  145. }
  146.  
  147. /// <summary>
  148. /// 返回指定坐标是否在UI节范围点内
  149. /// </summary>
  150. public static bool IsPositionOver(this Control control, Vector2 position)
  151. {
  152. var globalPosition = control.GlobalPosition;
  153. var size = control.Size * control.Scale;
  154. return position.X >= globalPosition.X && position.X <= (globalPosition.X + size.X) &&
  155. position.Y >= globalPosition.Y && position.Y <= (globalPosition.Y + size.Y);
  156. }
  157.  
  158. /// <summary>
  159. /// 判断点是否在区域内
  160. /// </summary>
  161. public static bool IsPositionInRect(Vector2 pos, Rect2 rect2)
  162. {
  163. return pos.X >= rect2.Position.X && pos.X <= rect2.Position.X + rect2.Size.X &&
  164. pos.Y >= rect2.Position.Y && pos.Y <= rect2.Position.Y + rect2.Size.Y;
  165. }
  166. }