Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// 常用函数工具类
  8. /// </summary>
  9. public static class Utils
  10. {
  11.  
  12. private static readonly Random _random;
  13. static Utils()
  14. {
  15. _random = new Random();
  16. }
  17.  
  18. /// <summary>
  19. /// 返回一个随机的double值
  20. /// </summary>
  21. public static double RandomDouble()
  22. {
  23. return _random.NextDouble();
  24. }
  25. /// <summary>
  26. /// 返回随机 boolean 值
  27. /// </summary>
  28. public static bool RandomBoolean()
  29. {
  30. return _random.NextSingle() >= 0.5f;
  31. }
  32.  
  33. /// <summary>
  34. /// 返回一个区间内的随机小数
  35. /// </summary>
  36. public static float RandomRangeFloat(float min, float max)
  37. {
  38. if (min == max) return min;
  39. if (min > max)
  40. return _random.NextSingle() * (min - max) + max;
  41. return _random.NextSingle() * (max - min) + min;
  42. }
  43.  
  44. /// <summary>
  45. /// 返回一个区间内的随机整数
  46. /// </summary>
  47. public static int RandomRangeInt(int min, int max)
  48. {
  49. if (min == max) return min;
  50. if (min > max)
  51. return Mathf.FloorToInt(_random.NextSingle() * (min - max + 1) + max);
  52. return Mathf.FloorToInt(_random.NextSingle() * (max - min + 1) + min);
  53. }
  54.  
  55. /// <summary>
  56. /// 随机返回其中一个参数
  57. /// </summary>
  58. public static T RandomChoose<T>(params T[] list)
  59. {
  60. if (list.Length == 0)
  61. {
  62. return default;
  63. }
  64.  
  65. return list[RandomRangeInt(0, list.Length - 1)];
  66. }
  67.  
  68. /// <summary>
  69. /// 随机返回集合中的一个元素
  70. /// </summary>
  71. public static T RandomChoose<T>(List<T> list)
  72. {
  73. if (list.Count == 0)
  74. {
  75. return default;
  76. }
  77.  
  78. return list[RandomRangeInt(0, list.Count - 1)];
  79. }
  80.  
  81. /// <summary>
  82. /// 随机返回集合中的一个元素, 并将其从集合中移除
  83. /// </summary>
  84. public static T RandomChooseAndRemove<T>(List<T> list)
  85. {
  86. if (list.Count == 0)
  87. {
  88. return default;
  89. }
  90.  
  91. var index = RandomRangeInt(0, list.Count - 1);
  92. var result = list[index];
  93. list.RemoveAt(index);
  94. return result;
  95. }
  96. /// <summary>
  97. /// 根据四个点计算出矩形
  98. /// </summary>
  99. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  100. {
  101. return new Rect2(
  102. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  103. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  104. );
  105. }
  106. /// <summary>
  107. /// 返回碰撞层 mask 是否会检测 layer
  108. /// </summary>
  109. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  110. {
  111. return (mask & layer) != 0;
  112. }
  113.  
  114. /// <summary>
  115. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  116. /// </summary>
  117. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, int width = 1)
  118. {
  119. for (var i = 0; i < polygonData.Length; i++)
  120. {
  121. var item = polygonData[i];
  122. if (item.Points.Count >= 2)
  123. {
  124. var array = item.ConvertPointsToVector2Array().ToList();
  125. array.Add(array[0]);
  126. if (item.Type == NavigationPolygonType.In)
  127. {
  128. canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow, width);
  129. }
  130. else
  131. {
  132. canvasItem.DrawPolyline(array.ToArray(), Colors.Red, width);
  133. }
  134. }
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// 字符串首字母小写
  140. /// </summary>
  141. public static string FirstToLower(this string str)
  142. {
  143. return str.Substring(0, 1).ToLower() + str.Substring(1);
  144. }
  145. }