summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XCommon.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-25 14:28:30 +0800
committerchai <chaifix@163.com>2021-01-25 14:28:30 +0800
commit6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch)
tree7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/XCommon.cs
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XCommon.cs')
-rw-r--r--Client/Assets/Scripts/XUtliPoolLib/XCommon.cs695
1 files changed, 695 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XCommon.cs b/Client/Assets/Scripts/XUtliPoolLib/XCommon.cs
new file mode 100644
index 00000000..d08dbcbd
--- /dev/null
+++ b/Client/Assets/Scripts/XUtliPoolLib/XCommon.cs
@@ -0,0 +1,695 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using UnityEngine;
+
+namespace XUtliPoolLib
+{
+ public class XCommon : XSingleton<XCommon>
+ {
+ public static float XEps
+ {
+ get
+ {
+ return XCommon._eps;
+ }
+ }
+
+ public int New_id
+ {
+ get
+ {
+ int num = this._new_id + 1;
+ this._new_id = num;
+ return num;
+ }
+ }
+
+ public long UniqueToken
+ {
+ get
+ {
+ return DateTime.Now.Ticks + (long)this.New_id;
+ }
+ }
+
+ public readonly float FrameStep = 0.0333333351f;
+
+ private static readonly float _eps = 0.0001f;
+
+ private System.Random _random = new System.Random(DateTime.Now.Millisecond);
+
+ private int _idx = 0;
+
+ private uint[] _seeds = new uint[]
+ {
+ 17u,
+ 33u,
+ 65u,
+ 129u
+ };
+
+ private int _new_id = 0;
+
+ public StringBuilder shareSB = new StringBuilder();
+
+ public static List<Renderer> tmpRender = new List<Renderer>();
+
+ public static List<ParticleSystem> tmpParticle = new List<ParticleSystem>();
+
+ public static List<SkinnedMeshRenderer> tmpSkinRender = new List<SkinnedMeshRenderer>();
+
+ public static List<MeshRenderer> tmpMeshRender = new List<MeshRenderer>();
+
+ public XCommon()
+ {
+ this._idx = 5;
+ }
+
+ public uint XHash(string str)
+ {
+ bool flag = str == null;
+ uint result;
+ if (flag)
+ {
+ result = 0u;
+ }
+ else
+ {
+ uint num = 0u;
+ for (int i = 0; i < str.Length; i++)
+ {
+ num = (num << this._idx) + num + (uint)str[i];
+ }
+ result = num;
+ }
+ return result;
+ }
+
+ public uint XHashLowerRelpaceDot(uint hash, string str)
+ {
+ bool flag = str == null;
+ uint result;
+ if (flag)
+ {
+ result = hash;
+ }
+ else
+ {
+ for (int i = 0; i < str.Length; i++)
+ {
+ char c = char.ToLower(str[i]);
+ bool flag2 = c == '/' || c == '\\';
+ if (flag2)
+ {
+ c = '.';
+ }
+ hash = (hash << this._idx) + hash + (uint)c;
+ }
+ result = hash;
+ }
+ return result;
+ }
+
+ public uint XHash(uint hash, string str)
+ {
+ bool flag = str == null;
+ uint result;
+ if (flag)
+ {
+ result = hash;
+ }
+ else
+ {
+ foreach (char c in str)
+ {
+ hash = (hash << this._idx) + hash + (uint)c;
+ }
+ result = hash;
+ }
+ return result;
+ }
+
+ public uint XHash(StringBuilder str)
+ {
+ bool flag = str == null;
+ uint result;
+ if (flag)
+ {
+ result = 0u;
+ }
+ else
+ {
+ uint num = 0u;
+ for (int i = 0; i < str.Length; i++)
+ {
+ num = (num << this._idx) + num + (uint)str[i];
+ }
+ result = num;
+ }
+ return result;
+ }
+
+ public bool IsEqual(float a, float b)
+ {
+ return a == b;
+ }
+
+ public bool IsLess(float a, float b)
+ {
+ return a < b;
+ }
+
+ public int Range(int value, int min, int max)
+ {
+ value = Math.Max(value, min);
+ return Math.Min(value, max);
+ }
+
+ public bool IsGreater(float a, float b)
+ {
+ return a > b;
+ }
+
+ public bool IsEqualLess(float a, float b)
+ {
+ return a <= b;
+ }
+
+ public bool IsEqualGreater(float a, float b)
+ {
+ return a >= b;
+ }
+
+ public uint GetToken()
+ {
+ return (uint)DateTime.Now.Millisecond;
+ }
+
+ public void ProcessValueDamp(ref float values, float target, ref float factor, float deltaT)
+ {
+ bool flag = XSingleton<XCommon>.singleton.IsEqual(values, target);
+ if (flag)
+ {
+ values = target;
+ factor = 0f;
+ }
+ else
+ {
+ values += (target - values) * Mathf.Min(1f, deltaT * factor);
+ }
+ }
+
+ public void ProcessValueEvenPace(ref float value, float target, float speed, float deltaT)
+ {
+ float num = target - value;
+ float num2 = target - (num - speed * deltaT);
+ bool flag = XSingleton<XCommon>.singleton.IsGreater((target - num2) * num, 0f);
+ if (flag)
+ {
+ value = num2;
+ }
+ else
+ {
+ value = target;
+ }
+ }
+
+ public bool IsRectCycleCross(float rectw, float recth, Vector3 c, float r)
+ {
+ Vector3 vector;
+ vector=new Vector3(Mathf.Max(Mathf.Abs(c.x) - rectw, 0f), 0f, Mathf.Max(Mathf.Abs(c.z) - recth, 0f));
+ return vector.sqrMagnitude < r * r;
+ }
+
+ public bool Intersection(Vector2 begin, Vector2 end, Vector2 center, float radius, out float t)
+ {
+ t = 0f;
+ float num = radius * radius;
+ Vector2 vector = center - begin;
+ float sqrMagnitude = vector.sqrMagnitude;
+ bool flag = sqrMagnitude < num;
+ bool result;
+ if (flag)
+ {
+ result = true;
+ }
+ else
+ {
+ Vector2 vector2 = end - begin;
+ bool flag2 = vector2.sqrMagnitude > 0f;
+ if (flag2)
+ {
+ float num2 = Mathf.Sqrt(sqrMagnitude) * Mathf.Cos(Vector2.Angle(vector, vector2));
+ bool flag3 = num2 >= 0f;
+ if (flag3)
+ {
+ float num3 = sqrMagnitude - num2 * num2;
+ bool flag4 = num3 < num;
+ if (flag4)
+ {
+ float num4 = num - num3;
+ t = num2 - Mathf.Sqrt(num4);
+ return true;
+ }
+ }
+ }
+ result = false;
+ }
+ return result;
+ }
+
+ private float CrossProduct(float x1, float z1, float x2, float z2)
+ {
+ return x1 * z2 - x2 * z1;
+ }
+
+ public bool IsLineSegmentCross(Vector3 p1, Vector3 p2, Vector3 q1, Vector3 q2)
+ {
+ bool flag = Mathf.Min(p1.x, p2.x) <= Mathf.Max(q1.x, q2.x) && Mathf.Min(q1.x, q2.x) <= Mathf.Max(p1.x, p2.x) && Mathf.Min(p1.z, p2.z) <= Mathf.Max(q1.z, q2.z) && Mathf.Min(q1.z, q2.z) <= Mathf.Max(p1.z, p2.z);
+ bool result;
+ if (flag)
+ {
+ float num = this.CrossProduct(p1.x - q1.x, p1.z - q1.z, q2.x - q1.x, q2.z - q1.z);
+ float num2 = this.CrossProduct(p2.x - q1.x, p2.z - q1.z, q2.x - q1.x, q2.z - q1.z);
+ float num3 = this.CrossProduct(q1.x - p1.x, q1.z - p1.z, p2.x - p1.x, p2.z - p1.z);
+ float num4 = this.CrossProduct(q2.x - p1.x, q2.z - p1.z, p2.x - p1.x, p2.z - p1.z);
+ result = (num * num2 <= 0f && num3 * num4 <= 0f);
+ }
+ else
+ {
+ Vector3 vector = Vector3.Project(p1, Vector3.up);
+ result = false;
+ }
+ return result;
+ }
+
+ public Vector3 Horizontal(Vector3 v)
+ {
+ v.y = 0f;
+ return v.normalized;
+ }
+
+ public void Horizontal(ref Vector3 v)
+ {
+ v.y = 0f;
+ v.Normalize();
+ }
+
+ public float AngleNormalize(float basic, float degree)
+ {
+ Vector3 vector = this.FloatToAngle(basic);
+ Vector3 vector2 = this.FloatToAngle(degree);
+ float num = Vector3.Angle(vector, vector2);
+ return this.Clockwise(vector, vector2) ? (basic + num) : (basic - num);
+ }
+
+ public Vector2 HorizontalRotateVetor2(Vector2 v, float degree, bool normalized = true)
+ {
+ degree = -degree;
+ float num = degree * 0.0174532924f;
+ float num2 = Mathf.Sin(num);
+ float num3 = Mathf.Cos(num);
+ float x = v.x * num3 - v.y * num2;
+ float y = v.x * num2 + v.y * num3;
+ v.x = x;
+ v.y = y;
+ return normalized ? v.normalized : v;
+ }
+
+ public Vector3 HorizontalRotateVetor3(Vector3 v, float degree, bool normalized = true)
+ {
+ degree = -degree;
+ float num = degree * 0.0174532924f;
+ float num2 = Mathf.Sin(num);
+ float num3 = Mathf.Cos(num);
+ float x = v.x * num3 - v.z * num2;
+ float z = v.x * num2 + v.z * num3;
+ v.x = x;
+ v.z = z;
+ return normalized ? v.normalized : v;
+ }
+
+ public float TicksToSeconds(long tick)
+ {
+ long num = tick / 10000L;
+ return (float)num / 1000f;
+ }
+
+ public long SecondsToTicks(float time)
+ {
+ long num = (long)(time * 1000f);
+ return num * 10000L;
+ }
+
+ public float AngleToFloat(Vector3 dir)
+ {
+ float num = Vector3.Angle(Vector3.forward, dir);
+ return XSingleton<XCommon>.singleton.Clockwise(Vector3.forward, dir) ? num : (-num);
+ }
+
+ public float AngleWithSign(Vector3 from, Vector3 to)
+ {
+ float num = Vector3.Angle(from, to);
+ return this.Clockwise(from, to) ? num : (-num);
+ }
+
+ public Vector3 FloatToAngle(float angle)
+ {
+ return Quaternion.Euler(0f, angle, 0f) * Vector3.forward;
+ }
+
+ public Quaternion VectorToQuaternion(Vector3 v)
+ {
+ return XSingleton<XCommon>.singleton.FloatToQuaternion(XSingleton<XCommon>.singleton.AngleWithSign(Vector3.forward, v));
+ }
+
+ public Quaternion FloatToQuaternion(float angle)
+ {
+ return Quaternion.Euler(0f, angle, 0f);
+ }
+
+ public Quaternion RotateToGround(Vector3 pos, Vector3 forward)
+ {
+ RaycastHit raycastHit;
+ bool flag = Physics.Raycast(new Ray(pos + Vector3.up, Vector3.down), out raycastHit, 5f, 513);
+ Quaternion result;
+ if (flag)
+ {
+ Vector3 normal = raycastHit.normal;
+ Vector3 vector = forward;
+ Vector3.OrthoNormalize(ref normal, ref vector);
+ result = Quaternion.LookRotation(vector, normal);
+ }
+ else
+ {
+ result = Quaternion.identity;
+ }
+ return result;
+ }
+
+ public bool Clockwise(Vector3 fiduciary, Vector3 relativity)
+ {
+ float num = fiduciary.z * relativity.x - fiduciary.x * relativity.z;
+ return num > 0f;
+ }
+
+ public bool Clockwise(Vector2 fiduciary, Vector2 relativity)
+ {
+ float num = fiduciary.y * relativity.x - fiduciary.x * relativity.y;
+ return num > 0f;
+ }
+
+ public bool IsInRect(Vector3 point, Rect rect, Vector3 center, Quaternion rotation)
+ {
+ float num = -(rotation.eulerAngles.y % 360f) / 180f * 3.14159274f;
+ Quaternion identity = Quaternion.identity;
+ identity.w = Mathf.Cos(num / 2f);
+ identity.x = 0f;
+ identity.y = Mathf.Sin(num / 2f);
+ identity.z = 0f;
+ point = identity * (point - center);
+ return point.x > rect.xMin && point.x < rect.xMax && point.z > rect.yMin && point.z < rect.yMax;
+ }
+
+ public float RandomPercentage()
+ {
+ return (float)this._random.NextDouble();
+ }
+
+ public float RandomPercentage(float min)
+ {
+ bool flag = this.IsEqualGreater(min, 1f);
+ float result;
+ if (flag)
+ {
+ result = 1f;
+ }
+ else
+ {
+ float num = (float)this._random.NextDouble();
+ bool flag2 = this.IsGreater(num, min);
+ if (flag2)
+ {
+ result = num;
+ }
+ else
+ {
+ result = num / min * (1f - min) + min;
+ }
+ }
+ return result;
+ }
+
+ public float RandomFloat(float max)
+ {
+ return this.RandomPercentage() * max;
+ }
+
+ public float RandomFloat(float min, float max)
+ {
+ return min + this.RandomFloat(max - min);
+ }
+
+ public int RandomInt(int min, int max)
+ {
+ return this._random.Next(min, max);
+ }
+
+ public int RandomInt(int max)
+ {
+ return this._random.Next(max);
+ }
+
+ public int RandomInt()
+ {
+ return this._random.Next();
+ }
+
+ public bool IsInteger(float n)
+ {
+ return Mathf.Abs(n - (float)((int)n)) < XCommon._eps || Mathf.Abs(n - (float)((int)n)) > 1f - XCommon._eps;
+ }
+
+ public float GetFloatingValue(float value, float floating)
+ {
+ bool flag = this.IsEqualLess(floating, 0f) || this.IsEqualGreater(floating, 1f);
+ float result;
+ if (flag)
+ {
+ result = value;
+ }
+ else
+ {
+ float num = this.IsLess(this.RandomPercentage(), 0.5f) ? (1f - floating) : (1f + floating);
+ result = value * num;
+ }
+ return result;
+ }
+
+ public float GetSmoothFactor(float distance, float timespan, float nearenough)
+ {
+ float result = 0f;
+ distance = Mathf.Abs(distance);
+ bool flag = distance > XCommon.XEps;
+ if (flag)
+ {
+ float smoothDeltaTime = Time.smoothDeltaTime;
+ float num = nearenough / distance;
+ float num2 = timespan / smoothDeltaTime;
+ bool flag2 = num2 > 1f;
+ if (flag2)
+ {
+ float num3 = Mathf.Pow(num, 1f / num2);
+ result = (1f - num3) / smoothDeltaTime;
+ }
+ else
+ {
+ result = float.PositiveInfinity;
+ }
+ }
+ return result;
+ }
+
+ public float GetJumpForce(float airTime, float g)
+ {
+ return 0f;
+ }
+
+ public string SecondsToString(int time)
+ {
+ int num = time / 60;
+ int num2 = time % 60;
+ return string.Format("{0:D2}:{1}", num, num2);
+ }
+
+ public double Clamp(double value, double min, double max)
+ {
+ return Math.Min(Math.Max(min, value), max);
+ }
+
+ public Transform FindChildRecursively(Transform t, string name)
+ {
+ bool flag = t.name == name;
+ Transform result;
+ if (flag)
+ {
+ result = t;
+ }
+ else
+ {
+ for (int i = 0; i < t.childCount; i++)
+ {
+ Transform transform = this.FindChildRecursively(t.GetChild(i), name);
+ bool flag2 = transform != null;
+ if (flag2)
+ {
+ return transform;
+ }
+ }
+ result = null;
+ }
+ return result;
+ }
+
+ public void CleanStringCombine()
+ {
+ this.shareSB.Length = 0;
+ }
+
+ public StringBuilder GetSharedStringBuilder()
+ {
+ return this.shareSB;
+ }
+
+ public string GetString()
+ {
+ return this.shareSB.ToString();
+ }
+
+ public XCommon AppendString(string s)
+ {
+ this.shareSB.Append(s);
+ return this;
+ }
+
+ public XCommon AppendString(string s0, string s1)
+ {
+ this.shareSB.Append(s0);
+ this.shareSB.Append(s1);
+ return this;
+ }
+
+ public XCommon AppendString(string s0, string s1, string s2)
+ {
+ this.shareSB.Append(s0);
+ this.shareSB.Append(s1);
+ this.shareSB.Append(s2);
+ return this;
+ }
+
+ public string StringCombine(string s0, string s1)
+ {
+ this.shareSB.Length = 0;
+ this.shareSB.Append(s0);
+ this.shareSB.Append(s1);
+ return this.shareSB.ToString();
+ }
+
+ public string StringCombine(string s0, string s1, string s2)
+ {
+ this.shareSB.Length = 0;
+ this.shareSB.Append(s0);
+ this.shareSB.Append(s1);
+ this.shareSB.Append(s2);
+ return this.shareSB.ToString();
+ }
+
+ public string StringCombine(string s0, string s1, string s2, string s3)
+ {
+ this.shareSB.Length = 0;
+ this.shareSB.Append(s0);
+ this.shareSB.Append(s1);
+ this.shareSB.Append(s2);
+ this.shareSB.Append(s3);
+ return this.shareSB.ToString();
+ }
+
+ public uint CombineAdd(uint value, int heigh, int low)
+ {
+ int num = (int)((value >> 16) + (uint)heigh);
+ int num2 = (int)((value & 65535u) + (uint)low);
+ return (uint)(num << 16 | num2);
+ }
+
+ public void CombineSetHeigh(ref uint value, uint heigh)
+ {
+ value = (heigh << 16 | (value & 65535u));
+ }
+
+ public ushort CombineGetHeigh(uint value)
+ {
+ return (ushort)(value >> 16);
+ }
+
+ public void CombineSetLow(ref uint value, uint low)
+ {
+ value = ((value & 4294901760u) | (low & 65535u));
+ }
+
+ public ushort CombineGetLow(uint value)
+ {
+ return (ushort)(value & 65535u);
+ }
+
+ public void EnableParticleRenderer(GameObject go, bool enable)
+ {
+ Animator componentInChildren = go.GetComponentInChildren<Animator>();
+ bool flag = componentInChildren != null;
+ if (flag)
+ {
+ componentInChildren.enabled = enable;
+ }
+ }
+
+ public void EnableParticle(GameObject go, bool enable)
+ {
+ go.GetComponentsInChildren<ParticleSystem>(XCommon.tmpParticle);
+ int i = 0;
+ int count = XCommon.tmpParticle.Count;
+ while (i < count)
+ {
+ ParticleSystem particleSystem = XCommon.tmpParticle[i];
+ if (enable)
+ {
+ particleSystem.Play();
+ }
+ else
+ {
+ particleSystem.Stop();
+ }
+ i++;
+ }
+ XCommon.tmpParticle.Clear();
+ }
+
+ public static UnityEngine.Object Instantiate(UnityEngine.Object prefab)
+ {
+ return UnityEngine.Object.Instantiate(prefab, null);
+ }
+
+ public static T Instantiate<T>(T original) where T : UnityEngine.Object
+ {
+ return UnityEngine.Object.Instantiate<T>(original, null);
+ }
+
+ public override bool Init()
+ {
+ return true;
+ }
+
+ public override void Uninit()
+ {
+ }
+ }
+}