Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 27 May 2023 4 KB 打印随机种子, 测试用
  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. var dateTime = DateTime.Now;
  16. var num = dateTime.Year * 100000 + dateTime.Month * 100000 + dateTime.Day * 100000 + dateTime.Hour * 10000 + dateTime.Minute * 100 + dateTime.Second;
  17. _random = new Random(num);
  18. GD.Print("随机种子为: ", num);
  19. }
  20.  
  21. /// <summary>
  22. /// 返回一个随机的double值
  23. /// </summary>
  24. public static double RandomDouble()
  25. {
  26. return _random.NextDouble();
  27. }
  28. /// <summary>
  29. /// 返回随机 boolean 值
  30. /// </summary>
  31. public static bool RandomBoolean()
  32. {
  33. return _random.NextSingle() >= 0.5f;
  34. }
  35.  
  36. /// <summary>
  37. /// 返回一个区间内的随机小数
  38. /// </summary>
  39. public static float RandomRangeFloat(float min, float max)
  40. {
  41. if (min == max) return min;
  42. if (min > max)
  43. return _random.NextSingle() * (min - max) + max;
  44. return _random.NextSingle() * (max - min) + min;
  45. }
  46.  
  47. /// <summary>
  48. /// 返回一个区间内的随机整数
  49. /// </summary>
  50. public static int RandomRangeInt(int min, int max)
  51. {
  52. if (min == max) return min;
  53. if (min > max)
  54. return Mathf.FloorToInt(_random.NextSingle() * (min - max + 1) + max);
  55. return Mathf.FloorToInt(_random.NextSingle() * (max - min + 1) + min);
  56. }
  57.  
  58. /// <summary>
  59. /// 随机返回其中一个参数
  60. /// </summary>
  61. public static T RandomChoose<T>(params T[] list)
  62. {
  63. if (list.Length == 0)
  64. {
  65. return default;
  66. }
  67.  
  68. return list[RandomRangeInt(0, list.Length - 1)];
  69. }
  70.  
  71. /// <summary>
  72. /// 随机返回集合中的一个元素
  73. /// </summary>
  74. public static T RandomChoose<T>(List<T> list)
  75. {
  76. if (list.Count == 0)
  77. {
  78. return default;
  79. }
  80.  
  81. return list[RandomRangeInt(0, list.Count - 1)];
  82. }
  83.  
  84. /// <summary>
  85. /// 随机返回集合中的一个元素, 并将其从集合中移除
  86. /// </summary>
  87. public static T RandomChooseAndRemove<T>(List<T> list)
  88. {
  89. if (list.Count == 0)
  90. {
  91. return default;
  92. }
  93.  
  94. var index = RandomRangeInt(0, list.Count - 1);
  95. var result = list[index];
  96. list.RemoveAt(index);
  97. return result;
  98. }
  99.  
  100. /// <summary>
  101. /// 从权重列表中随机抽取下标值
  102. /// </summary>
  103. public static int RandomWeight(List<int> weightList)
  104. {
  105. // 计算总权重
  106. var totalWeight = 0;
  107. foreach (var weight in weightList)
  108. {
  109. totalWeight += weight;
  110. }
  111. var randomNumber = _random.Next(totalWeight);
  112. var currentWeight = 0;
  113. for (var i = 0; i < weightList.Count; i++)
  114. {
  115. var value = weightList[i];
  116. currentWeight += value;
  117. if (randomNumber < currentWeight)
  118. {
  119. return i;
  120. }
  121. }
  122.  
  123. return RandomRangeInt(0, weightList.Count - 1);
  124. }
  125. /// <summary>
  126. /// 根据四个点计算出矩形
  127. /// </summary>
  128. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  129. {
  130. return new Rect2(
  131. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  132. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  133. );
  134. }
  135. /// <summary>
  136. /// 返回碰撞层 mask 是否会检测 layer
  137. /// </summary>
  138. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  139. {
  140. return (mask & layer) != 0;
  141. }
  142.  
  143. /// <summary>
  144. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  145. /// </summary>
  146. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, int width = 1)
  147. {
  148. for (var i = 0; i < polygonData.Length; i++)
  149. {
  150. var item = polygonData[i];
  151. if (item.Points.Count >= 2)
  152. {
  153. var array = item.ConvertPointsToVector2Array().ToList();
  154. array.Add(array[0]);
  155. if (item.Type == NavigationPolygonType.In)
  156. {
  157. canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow, width);
  158. }
  159. else
  160. {
  161. canvasItem.DrawPolyline(array.ToArray(), Colors.Red, width);
  162. }
  163. }
  164. }
  165. }
  166.  
  167. /// <summary>
  168. /// 字符串首字母小写
  169. /// </summary>
  170. public static string FirstToLower(this string str)
  171. {
  172. return str.Substring(0, 1).ToLower() + str.Substring(1);
  173. }
  174. }