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.  
  97. /// <summary>
  98. /// 从权重列表中随机抽取下标值
  99. /// </summary>
  100. public static int RandomWeight(List<int> weightList)
  101. {
  102. // 计算总权重
  103. var totalWeight = 0;
  104. foreach (var weight in weightList)
  105. {
  106. totalWeight += weight;
  107. }
  108. var randomNumber = _random.Next(totalWeight);
  109. var currentWeight = 0;
  110. for (var i = 0; i < weightList.Count; i++)
  111. {
  112. var value = weightList[i];
  113. currentWeight += value;
  114. if (randomNumber < currentWeight)
  115. {
  116. return i;
  117. }
  118. }
  119.  
  120. return RandomRangeInt(0, weightList.Count - 1);
  121. }
  122. /// <summary>
  123. /// 根据四个点计算出矩形
  124. /// </summary>
  125. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  126. {
  127. return new Rect2(
  128. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  129. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  130. );
  131. }
  132. /// <summary>
  133. /// 返回碰撞层 mask 是否会检测 layer
  134. /// </summary>
  135. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  136. {
  137. return (mask & layer) != 0;
  138. }
  139.  
  140. /// <summary>
  141. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  142. /// </summary>
  143. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, int width = 1)
  144. {
  145. for (var i = 0; i < polygonData.Length; i++)
  146. {
  147. var item = polygonData[i];
  148. if (item.Points.Count >= 2)
  149. {
  150. var array = item.ConvertPointsToVector2Array().ToList();
  151. array.Add(array[0]);
  152. if (item.Type == NavigationPolygonType.In)
  153. {
  154. canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow, width);
  155. }
  156. else
  157. {
  158. canvasItem.DrawPolyline(array.ToArray(), Colors.Red, width);
  159. }
  160. }
  161. }
  162. }
  163.  
  164. /// <summary>
  165. /// 字符串首字母小写
  166. /// </summary>
  167. public static string FirstToLower(this string str)
  168. {
  169. return str.Substring(0, 1).ToLower() + str.Substring(1);
  170. }
  171. }