Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 28 Jul 2023 4 KB 创建门区域, 开发中
  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. }