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. /// <summary>
  18. /// 返回随机 boolean 值
  19. /// </summary>
  20. public static bool RandomBoolean()
  21. {
  22. return _random.NextSingle() >= 0.5f;
  23. }
  24.  
  25. /// <summary>
  26. /// 返回一个区间内的随机小数
  27. /// </summary>
  28. public static float RandomRangeFloat(float min, float max)
  29. {
  30. if (min == max) return min;
  31. if (min > max)
  32. return _random.NextSingle() * (min - max) + max;
  33. return _random.NextSingle() * (max - min) + min;
  34. }
  35.  
  36. /// <summary>
  37. /// 返回一个区间内的随机整数
  38. /// </summary>
  39. public static int RandomRangeInt(int min, int max)
  40. {
  41. if (min == max) return min;
  42. if (min > max)
  43. return Mathf.FloorToInt(_random.NextSingle() * (min - max + 1) + max);
  44. return Mathf.FloorToInt(_random.NextSingle() * (max - min + 1) + min);
  45. }
  46.  
  47. /// <summary>
  48. /// 随机返回其中一个参数
  49. /// </summary>
  50. public static T RandomChoose<T>(params T[] list)
  51. {
  52. if (list.Length == 0)
  53. {
  54. return default;
  55. }
  56.  
  57. return list[RandomRangeInt(0, list.Length - 1)];
  58. }
  59.  
  60. /// <summary>
  61. /// 随机返回集合中的一个元素
  62. /// </summary>
  63. public static T RandomChoose<T>(List<T> list)
  64. {
  65. if (list.Count == 0)
  66. {
  67. return default;
  68. }
  69.  
  70. return list[RandomRangeInt(0, list.Count - 1)];
  71. }
  72.  
  73. /// <summary>
  74. /// 随机返回集合中的一个元素, 并将其从集合中移除
  75. /// </summary>
  76. public static T RandomChooseAndRemove<T>(List<T> list)
  77. {
  78. if (list.Count == 0)
  79. {
  80. return default;
  81. }
  82.  
  83. var index = RandomRangeInt(0, list.Count - 1);
  84. var result = list[index];
  85. list.RemoveAt(index);
  86. return result;
  87. }
  88. /// <summary>
  89. /// 根据四个点计算出矩形
  90. /// </summary>
  91. public static Rect2 CalcRect(float start1, float end1, float start2, float end2)
  92. {
  93. return new Rect2(
  94. Mathf.Min(start1, start2), Mathf.Min(end1, end2),
  95. Mathf.Abs(start1 - start2), Mathf.Abs(end1 - end2)
  96. );
  97. }
  98. /// <summary>
  99. /// 返回碰撞层 mask 是否会检测 layer
  100. /// </summary>
  101. public static bool CollisionMaskWithLayer(uint mask, uint layer)
  102. {
  103. return (mask & layer) != 0;
  104. }
  105.  
  106. /// <summary>
  107. /// 使用定的 canvasItem 绘制导航区域, 注意, 该函数只能在 draw 函数中调用
  108. /// </summary>
  109. public static void DrawNavigationPolygon(CanvasItem canvasItem, NavigationPolygonData[] polygonData)
  110. {
  111. for (var i = 0; i < polygonData.Length; i++)
  112. {
  113. var item = polygonData[i];
  114. if (item.Points.Count >= 2)
  115. {
  116. var array = item.ConvertPointsToVector2Array().ToList();
  117. array.Add(array[0]);
  118. if (item.Type == NavigationPolygonType.In)
  119. {
  120. canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow);
  121. }
  122. else
  123. {
  124. canvasItem.DrawPolyline(array.ToArray(), Colors.Red);
  125. }
  126. }
  127. }
  128. }
  129. }