using System; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Utils { public static List GenerateSmoothPoints(Vector3 pStartP, Vector3 pEndP, float pDelta) { List tPointList = new List(); Vector3 tDir = (pEndP - pStartP).normalized; Vector3 tNextP = pStartP; while ((pEndP - tNextP).magnitude > pDelta) { tNextP = tNextP + tDir * pDelta; tPointList.Add(tNextP); } return tPointList; } public static long DateToTimestamp(DateTime pDate) { DateTime tDate1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); return (pDate.Ticks - tDate1970.Ticks) / 10000000; } public static DateTime TimestampToDate(long tTimestamp) { DateTime tDate1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); long t = tDate1970.Ticks + tTimestamp * 10000000; return new DateTime(t); } public static float GetClipLength(Animator pAnimator, string pClipName) { if (pAnimator == null || pAnimator.runtimeAnimatorController == null) return 0; AnimationClip[] tClips = pAnimator.runtimeAnimatorController.animationClips; if (tClips == null || tClips.Length <= 0) return 0; AnimationClip tClip; for (int i = 0; i < tClips.Length; i++) { tClip = tClips[i]; if (tClip != null && tClip.name.Equals(pClipName)) return tClip.length; } return 0f; } public static int GetRandomIndex(int pTotalCount, List pMaskList) { List tFilterList = new List(); for (int i = 0; i < pTotalCount; i++) { if (!pMaskList.Contains(i)) tFilterList.Add(i); } int tRandomIndex = tFilterList[UnityEngine.Random.Range(0, tFilterList.Count)]; if (!pMaskList.Contains(tRandomIndex)) pMaskList.Add(tRandomIndex); return tRandomIndex; } public static void CheckDirectory(string tRootPath, string tSubPath) { string tDirectoryPath = tRootPath; string[] tFolderNames = tSubPath.Split('/'); if (tFolderNames.Length > 1) { for (int i = 0; i < tFolderNames.Length - 1; i++) { tDirectoryPath = Path.Combine(tDirectoryPath, tFolderNames[i]); if (!Directory.Exists(tDirectoryPath)) { Directory.CreateDirectory(tDirectoryPath); } } } } public static List GetChildList(Transform pParent) { List tList = new List(); T[] tComs = pParent.GetComponentsInChildren(true); for (int i = 0; i < tComs.Length; i++) { tList.Add(tComs[i]); } return tList; } public static List GetChildListFirstLayer(Transform pParent) { List tList = new List(); for (int i = 0; i < pParent.childCount; i++) { T tCom = pParent.GetChild(i).GetComponent(); tList.Add(tCom); } return tList; } public static void NormalizeGameObject(GameObject pGob) { pGob.transform.localPosition = Vector3.zero; pGob.transform.localRotation = Quaternion.identity; pGob.transform.localScale = Vector3.one; } public static void DelayShowGameObject(GameObject pGob, float pDelay) { pGob.SetActive(false); TimerManager.Instance.Schedule(() => pGob.SetActive(true), pDelay); } public static void DelayHideGameObject(GameObject pGob, float pDelay) { TimerManager.Instance.Schedule(() => pGob.SetActive(false), pDelay); } public static float Clamp360(float pOriginal, float pOffset = 0) { float tResult = pOriginal; while (tResult < 0 + pOffset) { tResult += 360; } while (tResult > 360 + pOffset) { tResult -= 360; } return tResult; } public static void SetGameSpeed(float pSpeed) { Time.timeScale = pSpeed; Time.fixedDeltaTime = 0.02f * Time.timeScale; } public static string GetUnitIDPrefix(BlockType pBType, bool pIsPlayer = true) { string tPrefix = pIsPlayer ? (pBType == BlockType.Monster ? GameConfig.Instance.PlayerNear : GameConfig.Instance.PlayerFar) : (pBType == BlockType.Monster ? GameConfig.Instance.EnemyNear : GameConfig.Instance.EnemyFar); return tPrefix; } public static string GetUnitID(BlockType pBType, int pLevel, bool pIsPlayer = true) { return GetUnitID(GetUnitIDPrefix(pBType, pIsPlayer), pLevel); } public static string GetUnitID(string pIDPrefix, int pLevel) { return pIDPrefix + "_" + pLevel.ToString("D2"); } public static int CountDamage(int pAttackerLv, int pSuffererLv, int pDamage) { int tLevelDelta = Mathf.Max(pSuffererLv - pAttackerLv, 0); float tRatio = Mathf.Max(1 - tLevelDelta * 0.08f, 0.6f); return (int)(tRatio * pDamage); } public static int GetBpByLv(int pLevel) { int tBP = 10; switch (pLevel) { case 1: tBP = 10; break; case 2: tBP = 24; break; case 3: tBP = 56; break; case 4: tBP = 138; break; case 5: tBP = 332; break; case 6: tBP = 796; break; case 7: tBP = 1910; break; case 8: tBP = 4586; break; case 9: tBP = 11008; break; case 10: tBP = 26418; break; } return tBP; } }