ExtraTool.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ShotSimulator.Tool
  5. {
  6. public class ExtraTool
  7. {
  8. public static float Sphere_Diameter1_ScreenOffsetX = 0.15f;
  9. public static float Sphere_Diameter1_ScreenOffsetY = 0.25f;
  10. public static Vector2 Sphere_Diameter1_ScreenLeftBottomOffset = new Vector2(Sphere_Diameter1_ScreenOffsetX, Sphere_Diameter1_ScreenOffsetY);
  11. public static Vector2 Sphere_Diameter1_ScreenRightTopOffset = new Vector2(1 - Sphere_Diameter1_ScreenOffsetX, 1 - Sphere_Diameter1_ScreenOffsetY);
  12. public static Vector3 GetWorldPositionWithResolutionPercent(float widthPercent, float heightPercent, float distance, Vector2 widthLimt = default, Vector2 heightLimt = default)
  13. {
  14. float x = UnityEngine.Screen.width * widthPercent;
  15. float y = UnityEngine.Screen.height * heightPercent;
  16. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(x, y, distance));
  17. return new Vector3(Mathf.Clamp(pos.x, widthLimt.x, widthLimt.y), Mathf.Clamp(pos.y, heightLimt.x, heightLimt.y), pos.z);
  18. }
  19. public static Vector3 GetScreenOffsetPosition(Vector2 offset, float distance, Vector2 widthLimt = default, Vector2 heightLimt = default)
  20. {
  21. return GetWorldPositionWithResolutionPercent(offset.x, offset.y, distance, widthLimt, heightLimt);
  22. }
  23. public static bool IsProbabolityInclusive(float probabolity)
  24. {
  25. float value = Random.Range(0, 1f);
  26. return value <= probabolity;
  27. }
  28. public static Vector3 RandomPosition(Vector3 min, Vector3 max)
  29. {
  30. float x = Random.Range(min.x, max.x);
  31. float y = Random.Range(min.y, max.y);
  32. float z = Random.Range(min.z, max.z);
  33. return new Vector3(x, y, z);
  34. }
  35. public static bool TryGetNonOverlapRamdonPos(List<Vector3> existPos, Vector3 minRange, Vector3 maxRange, float diameter, out Vector3 randomPos, bool isInt)
  36. {
  37. bool isOverlap;
  38. int maxAttempts = 50;
  39. int attempts = 0;
  40. do
  41. {
  42. attempts++;
  43. isOverlap = false;
  44. if (attempts <= maxAttempts)
  45. {
  46. Vector3 pos = RandomPosition(minRange, maxRange);
  47. randomPos = isInt ? pos.ToVector3Int() : pos;
  48. for (int i = 0; i < existPos.Count; i++)
  49. {
  50. if (Vector3.Distance(existPos[i], randomPos) < diameter)
  51. {
  52. isOverlap = true;
  53. break;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. randomPos = Vector3.zero;
  60. break;
  61. }
  62. } while (isOverlap);
  63. return attempts <= maxAttempts;
  64. }
  65. }
  66. }