diff options
author | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
commit | 6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch) | |
tree | 7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/XGrid.cs |
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XGrid.cs')
-rw-r--r-- | Client/Assets/Scripts/XUtliPoolLib/XGrid.cs | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XGrid.cs b/Client/Assets/Scripts/XUtliPoolLib/XGrid.cs new file mode 100644 index 00000000..651e6d3c --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/XGrid.cs @@ -0,0 +1,167 @@ +using System;
+using System.IO;
+using UnityEngine;
+
+namespace XUtliPoolLib
+{
+ public sealed class XGrid
+ {
+ public int Width;
+
+ public int Height;
+
+ public float XMin;
+
+ public float ZMin;
+
+ public float XMax;
+
+ public float ZMax;
+
+ public float Side;
+
+ public int[] IdxData = null;
+
+ public float[] ValueData = null;
+
+ public bool LoadFile(string FileName)
+ {
+ XBinaryReader xbinaryReader = XSingleton<XResourceLoaderMgr>.singleton.ReadBinary(FileName, ".bytes", false, true);
+ try
+ {
+ this.Height = xbinaryReader.ReadInt32();
+ this.Width = xbinaryReader.ReadInt32();
+ this.XMin = xbinaryReader.ReadSingle();
+ this.ZMin = xbinaryReader.ReadSingle();
+ this.XMax = xbinaryReader.ReadSingle();
+ this.ZMax = xbinaryReader.ReadSingle();
+ this.Side = xbinaryReader.ReadSingle();
+ int num = xbinaryReader.ReadInt32();
+ this.IdxData = new int[num];
+ for (int i = 0; i < num; i++)
+ {
+ this.IdxData[i] = xbinaryReader.ReadInt32();
+ }
+ this.ValueData = new float[num];
+ for (int j = 0; j < num; j++)
+ {
+ short num2 = xbinaryReader.ReadInt16();
+ bool flag = num2 < 0;
+ if (flag)
+ {
+ this.ValueData[j] = -100f;
+ bool flag2 = j + 1 < num;
+ if (flag2)
+ {
+ bool flag3 = num2 == short.MinValue;
+ if (flag3)
+ {
+ num2 = 0;
+ }
+ else
+ {
+ num2 = (short) -num2;
+ }
+ this.ValueData[j + 1] = (float)num2;
+ j++;
+ }
+ }
+ else
+ {
+ this.ValueData[j] = (float)num2;
+ }
+ }
+ }
+ catch (EndOfStreamException ex)
+ {
+ XSingleton<XDebug>.singleton.AddErrorLog("read file ", FileName, " not complete :", ex.Message, null, null);
+ return false;
+ }
+ finally
+ {
+ XSingleton<XResourceLoaderMgr>.singleton.ClearBinary(xbinaryReader, false);
+ }
+ return true;
+ }
+
+ public bool TryGetHeight(Vector3 pos, out float y)
+ {
+ y = 0f;
+ int num = (int)((pos.z - this.ZMin) / this.Side);
+ int num2 = (int)((pos.x - this.XMin) / this.Side);
+ int key = num * this.Width + num2;
+ int index = this.GetIndex(key);
+ bool flag = index >= 0 && index < this.ValueData.Length;
+ bool result;
+ if (flag)
+ {
+ y = this.ValueData[index] / 100f;
+ result = true;
+ }
+ else
+ {
+ result = false;
+ }
+ return result;
+ }
+
+ public float GetHeight(Vector3 pos)
+ {
+ int num = (int)((pos.z - this.ZMin) / this.Side);
+ int num2 = (int)((pos.x - this.XMin) / this.Side);
+ int key = num * this.Width + num2;
+ int index = this.GetIndex(key);
+ bool flag = index < 0 || index >= this.ValueData.Length;
+ float result;
+ if (flag)
+ {
+ XSingleton<XDebug>.singleton.AddErrorLog2("error grid index:{0} pos:{1}", new object[]
+ {
+ index,
+ pos
+ });
+ result = -1f;
+ }
+ else
+ {
+ result = this.ValueData[index] / 100f;
+ }
+ return result;
+ }
+
+ private int GetIndex(int key)
+ {
+ int num = this.IdxData.Length;
+ int i = 0;
+ int num2 = num - 1;
+ while (i <= num2)
+ {
+ int num3 = i + num2 >> 1;
+ int num4 = this.IdxData[num3];
+ bool flag = key == num4;
+ if (flag)
+ {
+ return num3;
+ }
+ bool flag2 = key < num4;
+ if (flag2)
+ {
+ num2 = num3 - 1;
+ }
+ else
+ {
+ bool flag3 = key > num4;
+ if (flag3)
+ {
+ i = num3 + 1;
+ }
+ }
+ }
+ return num2;
+ }
+
+ public void Update()
+ {
+ }
+ }
+}
|