CapersProject/Assets/02.Scripts/Utility/Utils.cs

94 lines
3.0 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-11-07 12:20:24 +00:00
using Pathfinding;
2024-06-03 18:26:03 +00:00
using UnityEngine;
2024-11-07 12:20:24 +00:00
using Random = UnityEngine.Random;
2024-06-03 18:26:03 +00:00
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<T>(List<T> list, T item)
{
if (list.Contains(item))
{
2024-11-07 09:13:54 +00:00
Debug.Log($"{item}은 이미 {list}안에 등록되어 있습니다.");
return;
}
list.Add(item);
}
public static void UnregisterList<T>(List<T> list, T item)
{
if (!list.Contains(item))
{
2024-11-07 09:13:54 +00:00
Debug.Log($"{item}은 {list}안에 없습니다.");
return;
}
list.Remove(item);
}
2024-11-07 12:20:24 +00:00
public static Vector3 RandomPositionOnGraph(int graphIndex = 0)
{
// 그래프 인덱스가 유효한지 검사
if (graphIndex < 0 || graphIndex >= AstarPath.active.data.graphs.Length)
{
throw new Exception("유효하지 않은 그래프 인덱스입니다.");
}
// 특정 그래프를 가져와 모든 walkable 노드를 수집
List<GraphNode> walkableNodes = new List<GraphNode>();
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;
}
2024-06-03 18:26:03 +00:00
}
}