112 lines
3.1 KiB
C#
112 lines
3.1 KiB
C#
using KWS;
|
|
using UnityEngine;
|
|
|
|
public static class GameWaterSystem
|
|
{
|
|
public static bool ContainsXZ(this Bounds bounds, Vector3 point)
|
|
{
|
|
Vector3 min = bounds.min;
|
|
Vector3 max = bounds.max;
|
|
if (point.x >= min.x && point.x <= max.x && point.z >= min.z)
|
|
{
|
|
return point.z <= max.z;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool RaycastRiverColliderDownward(WaterSystem water, Vector3 position, out float collisionY)
|
|
{
|
|
bool flag = false;
|
|
collisionY = 0f;
|
|
MeshCollider meshCollider = water.GetComponentInChildren<MeshCollider>();
|
|
if (!meshCollider && (bool)GameWaterSystemHandler.Instance)
|
|
{
|
|
meshCollider = GameWaterSystemHandler.Instance.GetMeshCollider(water);
|
|
}
|
|
if ((bool)meshCollider)
|
|
{
|
|
Vector3 origin = position + meshCollider.transform.localPosition;
|
|
Vector3 down = Vector3.down;
|
|
flag = meshCollider.Raycast(new Ray(origin, down), out var hitInfo, 100000f);
|
|
if (flag)
|
|
{
|
|
collisionY = hitInfo.point.y - meshCollider.transform.localPosition.y;
|
|
}
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
public static bool IsPositionUnderWater(Vector3 position)
|
|
{
|
|
bool waterDetected;
|
|
float waterLevel;
|
|
WaterSystem waterSystem;
|
|
return IsPositionUnderWater(position, out waterDetected, out waterLevel, out waterSystem);
|
|
}
|
|
|
|
public static bool IsPositionUnderWater(Vector3 position, out bool waterDetected, out float waterLevel, out WaterSystem waterSystem)
|
|
{
|
|
bool result = false;
|
|
waterDetected = false;
|
|
waterLevel = 0f;
|
|
waterSystem = null;
|
|
float y = 300f;
|
|
Vector3 vector = position;
|
|
vector.y = y;
|
|
float num = float.MinValue;
|
|
WaterSystem waterSystem2 = null;
|
|
foreach (WaterSystem waterInstance in WaterSharedResources.WaterInstances)
|
|
{
|
|
if (!waterInstance.WorldSpaceBounds.ContainsXZ(vector) || waterInstance.CompareTag("Aquarium"))
|
|
{
|
|
continue;
|
|
}
|
|
WaterSurfaceData currentWaterSurfaceData = waterInstance.GetCurrentWaterSurfaceData(vector);
|
|
if (waterInstance.Settings.WaterMeshType == WaterSystemScriptableData.WaterMeshTypeEnum.River)
|
|
{
|
|
if (RaycastRiverColliderDownward(waterInstance, vector, out var collisionY) && collisionY > num)
|
|
{
|
|
num = collisionY;
|
|
waterSystem2 = waterInstance;
|
|
}
|
|
}
|
|
else if (currentWaterSurfaceData.Position.y > num)
|
|
{
|
|
num = currentWaterSurfaceData.Position.y;
|
|
waterSystem2 = waterInstance;
|
|
}
|
|
}
|
|
if (num > float.MinValue)
|
|
{
|
|
result = position.y < num;
|
|
waterDetected = true;
|
|
waterLevel = num;
|
|
waterSystem = waterSystem2;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static float GetDepth(Vector3 position)
|
|
{
|
|
float result = 0f;
|
|
if (FindWaterLevelAtLocation(position, out var waterLevel) && waterLevel > position.y)
|
|
{
|
|
result = Mathf.Abs(waterLevel - position.y);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static bool FindWaterLevelAtLocation(Vector3 position, out float waterLevel)
|
|
{
|
|
IsPositionUnderWater(position, out var waterDetected, out waterLevel, out var _);
|
|
return waterDetected;
|
|
}
|
|
|
|
public static bool FindWaterSystemAtLocation(Vector3 position, out WaterSystem waterSystem)
|
|
{
|
|
IsPositionUnderWater(position, out var waterDetected, out var _, out var waterSystem2);
|
|
waterSystem = waterSystem2;
|
|
return waterDetected;
|
|
}
|
|
}
|