| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace ShotSimulator.Tool
- {
- public class ExtraTool
- {
- public static float Sphere_Diameter1_ScreenOffsetX = 0.15f;
- public static float Sphere_Diameter1_ScreenOffsetY = 0.25f;
- public static Vector2 Sphere_Diameter1_ScreenLeftBottomOffset = new Vector2(Sphere_Diameter1_ScreenOffsetX, Sphere_Diameter1_ScreenOffsetY);
- public static Vector2 Sphere_Diameter1_ScreenRightTopOffset = new Vector2(1 - Sphere_Diameter1_ScreenOffsetX, 1 - Sphere_Diameter1_ScreenOffsetY);
- public static Vector3 GetWorldPositionWithResolutionPercent(float widthPercent, float heightPercent, float distance, Vector2 widthLimt = default, Vector2 heightLimt = default)
- {
- float x = UnityEngine.Screen.width * widthPercent;
- float y = UnityEngine.Screen.height * heightPercent;
- Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(x, y, distance));
- return new Vector3(Mathf.Clamp(pos.x, widthLimt.x, widthLimt.y), Mathf.Clamp(pos.y, heightLimt.x, heightLimt.y), pos.z);
- }
- public static Vector3 GetScreenOffsetPosition(Vector2 offset, float distance, Vector2 widthLimt = default, Vector2 heightLimt = default)
- {
- return GetWorldPositionWithResolutionPercent(offset.x, offset.y, distance, widthLimt, heightLimt);
- }
- public static bool IsProbabolityInclusive(float probabolity)
- {
- float value = Random.Range(0, 1f);
- return value <= probabolity;
- }
- public static Vector3 RandomPosition(Vector3 min, Vector3 max)
- {
- float x = Random.Range(min.x, max.x);
- float y = Random.Range(min.y, max.y);
- float z = Random.Range(min.z, max.z);
- return new Vector3(x, y, z);
- }
- public static bool TryGetNonOverlapRamdonPos(List<Vector3> existPos, Vector3 minRange, Vector3 maxRange, float diameter, out Vector3 randomPos, bool isInt)
- {
- bool isOverlap;
- int maxAttempts = 50;
- int attempts = 0;
- do
- {
- attempts++;
- isOverlap = false;
- if (attempts <= maxAttempts)
- {
- Vector3 pos = RandomPosition(minRange, maxRange);
- randomPos = isInt ? pos.ToVector3Int() : pos;
- for (int i = 0; i < existPos.Count; i++)
- {
- if (Vector3.Distance(existPos[i], randomPos) < diameter)
- {
- isOverlap = true;
- break;
- }
- }
- }
- else
- {
- randomPos = Vector3.zero;
- break;
- }
- } while (isOverlap);
- return attempts <= maxAttempts;
- }
- }
- }
|