Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / Utils.cs
@小李xl 小李xl on 28 Jun 2023 5 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(204313957);
  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. /// <summary>
  37. /// 指定概率会返回 true, probability 范围 0 - 1
  38. /// </summary>
  39. public static bool RandomBoolean(float probability)
  40. {
  41. return _random.NextSingle() <= probability;
  42. }
  43.  
  44. /// <summary>
  45. /// 返回一个区间内的随机小数
  46. /// </summary>
  47. public static float RandomRangeFloat(float min, float max)
  48. {
  49. if (min == max) return min;
  50. if (min > max)
  51. return _random.NextSingle() * (min - max) + max;
  52. return _random.NextSingle() * (max - min) + min;
  53. }
  54.  
  55. /// <summary>
  56. /// 返回一个区间内的随机整数
  57. /// </summary>
  58. public static int RandomRangeInt(int min, int max)
  59. {
  60. if (min == max) return min;
  61. if (min > max)
  62. return Mathf.FloorToInt(_random.NextSingle() * (min - max + 1) + max);
  63. return Mathf.FloorToInt(_random.NextSingle() * (max - min + 1) + min);
  64. }
  65.  
  66. /// <summary>
  67. /// 随机返回其中一个参数
  68. /// </summary>
  69. public static T RandomChoose<T>(params T[] list)
  70. {
  71. if (list.Length == 0)
  72. {
  73. return default;
  74. }
  75.  
  76. return list[RandomRangeInt(0, list.Length - 1)];
  77. }
  78.  
  79. /// <summary>
  80. /// 随机返回集合中的一个元素
  81. /// </summary>
  82. public static T RandomChoose<T>(List<T> list)
  83. {
  84. if (list.Count == 0)
  85. {
  86. return default;
  87. }
  88.  
  89. return list[RandomRangeInt(0, list.Count - 1)];
  90. }
  91.  
  92. /// <summary>
  93. /// 随机返回集合中的一个元素, 并将其从集合中移除
  94. /// </summary>
  95. public static T RandomChooseAndRemove<T>(List<T> list)
  96. {
  97. if (list.Count == 0)
  98. {
  99. return default;
  100. }
  101.  
  102. var index = RandomRangeInt(0, list.Count - 1);
  103. var result = list[index];
  104. list.RemoveAt(index);
  105. return result;
  106. }
  107.  
  108. /// <summary>
  109. /// 从权重列表中随机抽取下标值
  110. /// </summary>
  111. public static int RandomWeight(List<int> weightList)
  112. {
  113. // 计算总权重
  114. var totalWeight = 0;
  115. foreach (var weight in weightList)
  116. {
  117. totalWeight += weight;
  118. }
  119. var randomNumber = _random.Next(totalWeight);
  120. var currentWeight = 0;
  121. for (var i = 0; i < weightList.Count; i++)
  122. {
  123. var value = weightList[i];
  124. currentWeight += value;
  125. if (randomNumber < currentWeight)
  126. {
  127. return i;
  128. }
  129. }
  130.  
  131. return RandomRangeInt(0, weightList.Count - 1);
  132. }
  133. /// <summary>
  134. /// 根据四个点计算出矩形
  135. /// </summary>
  136. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  137. {
  138. return new Rect2(
  139. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  140. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  141. );
  142. }
  143. /// <summary>
  144. /// 返回碰撞层 mask 是否会检测 layer
  145. /// </summary>
  146. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  147. {
  148. return (mask & layer) != 0;
  149. }
  150.  
  151. /// <summary>
  152. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  153. /// </summary>
  154. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData, int width = 1)
  155. {
  156. for (var i = 0; i < polygonData.Length; i++)
  157. {
  158. var item = polygonData[i];
  159. if (item.Points.Count >= 2)
  160. {
  161. var array = item.ConvertPointsToVector2Array().ToList();
  162. array.Add(array[0]);
  163. if (item.Type == NavigationPolygonType.In)
  164. {
  165. canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow, width);
  166. }
  167. else
  168. {
  169. canvasItem.DrawPolyline(array.ToArray(), Colors.Red, width);
  170. }
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// 将一个任意角度转为0到360度
  176. /// </summary>
  177. public static float ConvertAngle(float angle)
  178. {
  179. angle %= 360; // 取余
  180.  
  181. if (angle < 0) // 如果角度为负数,转为正数
  182. {
  183. angle += 360;
  184. }
  185.  
  186. return angle;
  187. }
  188.  
  189. /// <summary>
  190. /// 字符串首字母小写
  191. /// </summary>
  192. public static string FirstToLower(this string str)
  193. {
  194. return str.Substring(0, 1).ToLower() + str.Substring(1);
  195. }
  196. /// <summary>
  197. /// 字符串首字母大写
  198. /// </summary>
  199. public static string FirstToUpper(this string str)
  200. {
  201. return str.Substring(0, 1).ToUpper() + str.Substring(1);
  202. }
  203. }