using System; using System.Collections; using System.Collections.Generic; using Pathfinding; using UnityEngine; using Random = UnityEngine.Random; namespace BlueWater.Utility { public static class Utils { public static IEnumerator CoolDownCoroutine(float waitTime, Action onCooldownComplete = null) { yield return new WaitForSeconds(waitTime); onCooldownComplete?.Invoke(); } public static void StartUniqueCoroutine(MonoBehaviour owner, ref Coroutine coroutineField, IEnumerator coroutineMethod) { if (coroutineField != null) { owner.StopCoroutine(coroutineField); coroutineField = null; } coroutineField = owner.StartCoroutine(coroutineMethod); } public static void EndUniqueCoroutine(MonoBehaviour owner, ref Coroutine coroutineField) { if (coroutineField != null) { owner.StopCoroutine(coroutineField); coroutineField = null; } } public static void RegisterList(List list, T item) { if (list.Contains(item)) { Debug.Log($"{item}은 이미 {list}안에 등록되어 있습니다."); return; } list.Add(item); } public static void UnregisterList(List list, T item) { if (!list.Contains(item)) { Debug.Log($"{item}은 {list}안에 없습니다."); return; } list.Remove(item); } public static Vector3 RandomPositionOnGraph(int graphIndex = 0) { // 그래프 인덱스가 유효한지 검사 if (graphIndex < 0 || graphIndex >= AstarPath.active.data.graphs.Length) { throw new Exception("유효하지 않은 그래프 인덱스입니다."); } // 특정 그래프를 가져와 모든 walkable 노드를 수집 List walkableNodes = new List(); NavGraph graph = AstarPath.active.data.graphs[graphIndex]; graph.GetNodes(node => { if (node.Walkable) { walkableNodes.Add(node); } return true; // 모든 노드를 반복하도록 true 반환 }); // walkable한 노드가 없을 경우 Vector3.zero 반환 if (walkableNodes.Count == 0) { throw new Exception("이동 가능한 노드가 없습니다."); } // walkable한 노드 중 하나를 무작위로 선택 GraphNode randomNode = walkableNodes[Random.Range(0, walkableNodes.Count)]; // 선택한 노드의 위치 반환 return (Vector3)randomNode.position; } } }