OldBlueWater/BlueWater/Assets/02.Scripts/Utility/Utils.cs

134 lines
4.9 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using UnityEditor;
2023-08-30 05:57:45 +00:00
using UnityEditor.Animations;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Assertions;
using Debug = UnityEngine.Debug;
using Random = UnityEngine.Random;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class Utils
{
public enum DebugType
{
NONE,
LOG,
WARNING,
ERROR,
DEBUG_ASSERT,
ASSERT //
}
/// <summary>
/// 각도를 통해 방향값을 반환하는 함수
/// </summary>
public static Vector3 AngleToDir(float angle)
{
var radian = angle * Mathf.Deg2Rad;
return new Vector3(Mathf.Sin(radian), 0f, Mathf.Cos(radian)).normalized;
}
// public static T FindTaskAndAssert<T>(BehaviorTree behaviorTree) where T : Task
// {
// var newBehaviorTask = behaviorTree.FindTask<T>();
// Assert.IsNotNull(newBehaviorTask, typeof(T) + "타입의 Task가 존재하지 않습니다.");
//
// return newBehaviorTask;
// }
public static T GetComponentAndAssert<T>(Transform componentLocation, DebugType debugType = DebugType.DEBUG_ASSERT) where T : class
{
var newComponent = componentLocation.GetComponent<T>();
switch (debugType)
{
case DebugType.NONE:
Debug.Log("DebugType is None.");
break;
case DebugType.LOG:
Debug.Log(typeof(T) + "타입의 Component가 존재하지 않습니다.");
break;
case DebugType.WARNING:
Debug.LogWarning(typeof(T) + "타입의 Component가 존재하지 않습니다.");
break;
case DebugType.ERROR:
Debug.LogError(typeof(T) + "타입의 Component가 존재하지 않습니다.");
break;
case DebugType.DEBUG_ASSERT:
Debug.Assert(newComponent != null, typeof(T) + "타입의 Component가 존재하지 않습니다.");
break;
case DebugType.ASSERT:
Assert.IsNotNull(newComponent, typeof(T) + "타입의 Component가 존재하지 않습니다.");
break;
default:
throw new ArgumentOutOfRangeException(nameof(debugType), debugType, null);
}
return newComponent;
}
public static bool ArrivedAgentDestination(NavMeshAgent agent) =>
!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance;
/// <summary>
/// 메서드를 누가 호출하는지 알려주는 메서드입니다
/// 메서드 마지막에 이 함수를 호출하면 됩니다
/// </summary>
public static void WhereCallThisFunction()
{
var stackTrace = new StackTrace();
var stackFrame = stackTrace.GetFrame(1);
var method = stackFrame.GetMethod();
var className = method.DeclaringType?.Name;
var methodName = method.Name;
UnityEngine.Debug.Log($"Call {className}.{methodName}");
}
public static float CalcDamage(AiStat attacker, AiStat defender)
{
var finalDamage = 0f;
if (defender.UsingShield)
{
var penetrationChance = attacker.ShieldPenetrationRate -
(attacker.ShieldPenetrationRate * defender.PenetrationResistivity * 0.01f);
// 방패를 관통했다면,
if (Random.Range(0, 100) < penetrationChance)
{
finalDamage = attacker.Atk - defender.Def;
finalDamage = Mathf.Max(finalDamage, 0);
return finalDamage;
}
// 방패를 관통하지 못 함
return 0;
}
finalDamage = attacker.Atk - defender.Def;
finalDamage = Mathf.Max(finalDamage, 0);
return finalDamage;
}
#if UNITY_EDITOR
public static GameObject LoadPrefabFromFolder(string folderPath, string prefabName)
{
var fullPath = System.IO.Path.Combine(folderPath, prefabName + ".prefab");
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(fullPath);
return prefab;
}
2023-08-30 05:57:45 +00:00
public static AnimatorController LoadAnimatorControllerFromFolder(string folderPath, string name)
{
var fullPath = System.IO.Path.Combine(folderPath, name + ".controller");
var prefab = AssetDatabase.LoadAssetAtPath<AnimatorController>(fullPath);
return prefab;
}
#endif
}
}