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