mergemilitary/Assets/Scripts/Utils/Utils.cs

329 lines
9.0 KiB
C#

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Utils
{
public static List<Vector3> GenerateSmoothPoints(Vector3 pStartP, Vector3 pEndP, float pDelta)
{
List<Vector3> tPointList = new List<Vector3>();
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<int> pMaskList)
{
List<int> tFilterList = new List<int>();
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<T> GetChildList<T>(Transform pParent)
{
List<T> tList = new List<T>();
T[] tComs = pParent.GetComponentsInChildren<T>(true);
for (int i = 0; i < tComs.Length; i++)
{
tList.Add(tComs[i]);
}
return tList;
}
public static List<T> GetChildListFirstLayer<T>(Transform pParent)
{
List<T> tList = new List<T>();
for (int i = 0; i < pParent.childCount; i++)
{
T tCom = pParent.GetChild(i).GetComponent<T>();
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 WarUnitAttr GetWarUnitAttrFromCard(BlockType pBType, int pLevel)
{
CardData tCard = GameConfig.Instance.GetCardData(GetCardID(pBType, pLevel));
WarUnitAttr tAttr = new WarUnitAttr();
tAttr.MaxHP = tCard.MaxHp / tCard.GroupNum;
tAttr.Damage = tCard.Damage / tCard.GroupNum;
tAttr.MoveSpeed = GameConfig.Instance.UnitSpeed;
tAttr.AtkType = AttackType.Far;// pBType == BlockType.Monster ? AttackType.Near : AttackType.Far;
tAttr.AtkDistance = pBType == BlockType.Monster ? 6 : 12;
return tAttr;
}
public static string GetCardID(BlockType pBType, int pLevel)
{
return pBType + "_" + 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 bool IsHuman(string pCardID)
{
return pCardID.Contains("Human");
}
public static bool IsMonster(string pCardID)
{
return pCardID.Contains("Monster");
}
public static float GetHpHOffset(BlockType pBType, int pLevel)
{
float tHOffset = 4;
if (pBType == BlockType.Monster)
{
switch (pLevel)
{
case 1: tHOffset = 2f; break;
case 2: tHOffset = 2f; break;
case 3: tHOffset = 3f; break;
case 4: tHOffset = 3f; break;
case 5: tHOffset = 3.5f; break;
case 6: tHOffset = 4f; break;
case 7: tHOffset = 4.5f; break;
case 8: tHOffset = 3.5f; break;
case 9: tHOffset = 4f; break;
case 10: tHOffset = 5f; break;
}
}
else if (pBType == BlockType.Human)
{
switch (pLevel)
{
case 1: tHOffset = 2f; break;
case 2: tHOffset = 2f; break;
case 3: tHOffset = 3f; break;
case 4: tHOffset = 3f; break;
case 5: tHOffset = 3.5f; break;
case 6: tHOffset = 4f; break;
case 7: tHOffset = 4.5f; break;
case 8: tHOffset = 5f; break;
case 9: tHOffset = 5.2f; break;
case 10: tHOffset = 6f; break;
}
}
return tHOffset;
}
public static float GetHpScale(BlockType pBType, int pLevel)
{
float tScale = 1;
if (pBType == BlockType.Monster)
{
switch (pLevel)
{
case 1:
case 2:
case 3:
case 4: tScale = 0.5f; break;
case 5:
case 6:
case 7: tScale = 0.8f; break;
case 8: tScale = 1f; break;
case 9:
case 10: tScale = 1.2f; break;
}
}
else if (pBType == BlockType.Human)
{
switch (pLevel)
{
case 1:
case 2:
case 3:
case 4:
case 5: tScale = 0.5f; break;
case 6: tScale = 0.5f; break;
case 7: tScale = 0.6f; break;
case 8: tScale = 0.7f; break;
case 9: tScale = 0.8f; break;
case 10: tScale = 1f; break;
}
}
return tScale;
}
public static float GetAtkAniLength(BlockType pBType, int pLevel)
{
float tLength = 1;
if (pBType == BlockType.Monster)
{
switch (pLevel)
{
case 1:
case 2:
case 3: tLength = 1.167f; break;
case 4:
case 5:
case 6:
case 7: tLength = 1f; break;
}
}
else if (pBType == BlockType.Human)
{
switch (pLevel)
{
case 1:
case 2: tLength = 1.033f; break;
case 3:
case 4: tLength = 1.333f; break;
case 5:
case 6:
case 7: tLength = 1f; break;
}
}
return tLength;
}
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;
}
}