324 lines
7.7 KiB
C#
324 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UFS2.Gameplay;
|
|
using UnityEngine;
|
|
|
|
public class FWaterDisplacement : MonoBehaviour
|
|
{
|
|
public enum MoveCharacteristic
|
|
{
|
|
Custom = 0,
|
|
MoveUp_StopDown = 1,
|
|
MoveDown_100cm = 2,
|
|
MoveDown_150cm = 3,
|
|
TopWater = 4,
|
|
MoveDown = 5,
|
|
MoveUp_StopDown_Twisters = 6
|
|
}
|
|
|
|
public MoveCharacteristic moveCharacteristic;
|
|
|
|
public float objectDisplacement = 1f;
|
|
|
|
public float waterDrag = 3f;
|
|
|
|
public float waterMass = 3f;
|
|
|
|
public float outwaterMass = 6f;
|
|
|
|
public Action<bool> OnIsInWaterChanged;
|
|
|
|
private bool _IsInWater;
|
|
|
|
public bool resetVelocityEnterToWater = true;
|
|
|
|
private float normalDrag;
|
|
|
|
public bool isFreeze;
|
|
|
|
public bool useSplashes = true;
|
|
|
|
public bool useSplashesOnlyInThrow;
|
|
|
|
public bool useWaterCurrent;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody rigidbody;
|
|
|
|
[HideInInspector]
|
|
public Collider collider;
|
|
|
|
[HideInInspector]
|
|
public float waterHeightPosition;
|
|
|
|
[HideInInspector]
|
|
public float depth;
|
|
|
|
private FFishSystem fFishSystem;
|
|
|
|
public List<FFish> fishListCreated;
|
|
|
|
private bool isEnabled = true;
|
|
|
|
public bool IsInWater
|
|
{
|
|
get
|
|
{
|
|
return _IsInWater;
|
|
}
|
|
set
|
|
{
|
|
_IsInWater = value;
|
|
}
|
|
}
|
|
|
|
public bool IsEnabled
|
|
{
|
|
get
|
|
{
|
|
return isEnabled;
|
|
}
|
|
set
|
|
{
|
|
isEnabled = value;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
fFishSystem = UnityEngine.Object.FindObjectOfType<FFishSystem>();
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
collider = GetComponent<Collider>();
|
|
normalDrag = 1f;
|
|
fishListCreated = new List<FFish>();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!FishEntity.CurrentFishInFight && IsEnabled)
|
|
{
|
|
InWaterUpdateHandler();
|
|
GameWaterSystem.FindWaterLevelAtLocation(base.transform.position, out waterHeightPosition);
|
|
depth = GameWaterSystem.GetDepth(base.transform.position);
|
|
AffectCharacteristicNew();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
}
|
|
|
|
private void AffectCharacteristic()
|
|
{
|
|
if (IsInWater && objectDisplacement > 0f)
|
|
{
|
|
base.transform.position = Vector3.MoveTowards(base.transform.position, new Vector3(base.transform.position.x, waterHeightPosition, base.transform.position.z), Time.deltaTime * objectDisplacement);
|
|
}
|
|
if (rigidbody.velocity.magnitude > 0.01f)
|
|
{
|
|
Quaternion b = Quaternion.LookRotation(rigidbody.velocity, Vector3.up);
|
|
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b, Time.deltaTime);
|
|
rigidbody.freezeRotation = false;
|
|
}
|
|
}
|
|
|
|
private void AffectCharacteristicNew()
|
|
{
|
|
if (isFreeze || !IsInWater)
|
|
{
|
|
return;
|
|
}
|
|
float num = Vector3.Dot(rigidbody.velocity, base.transform.forward);
|
|
float magnitude = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z).magnitude;
|
|
switch (moveCharacteristic)
|
|
{
|
|
case MoveCharacteristic.MoveDown:
|
|
if (num > 0.2f)
|
|
{
|
|
rigidbody.AddForce(-Vector3.up * Time.deltaTime * Mathf.Clamp01(num) * 2f, ForceMode.VelocityChange);
|
|
}
|
|
else
|
|
{
|
|
rigidbody.AddForce(Vector3.up * Time.deltaTime * 1f, ForceMode.VelocityChange);
|
|
}
|
|
Rotation();
|
|
break;
|
|
case MoveCharacteristic.MoveUp_StopDown:
|
|
case MoveCharacteristic.MoveUp_StopDown_Twisters:
|
|
{
|
|
float num2 = 0.2f;
|
|
float b = 1f;
|
|
if (magnitude > num2)
|
|
{
|
|
float t = Mathf.InverseLerp(num2, b, magnitude);
|
|
AddForce(Mathf.Lerp(0f, 5f, t));
|
|
}
|
|
else
|
|
{
|
|
AddForce(-3f);
|
|
}
|
|
Rotation();
|
|
break;
|
|
}
|
|
case MoveCharacteristic.TopWater:
|
|
{
|
|
float verticalAmount = Mathf.Clamp((((num > 0.2f) ? (-0.05f) : 0.05f) + waterHeightPosition - base.transform.position.y) * 100f, -10f, 10f);
|
|
AddForce(verticalAmount);
|
|
rigidbody.freezeRotation = true;
|
|
Rotation();
|
|
StabilizeRotation();
|
|
break;
|
|
}
|
|
case MoveCharacteristic.MoveDown_150cm:
|
|
if (magnitude > 0.2f && depth > 1.5f)
|
|
{
|
|
AddForce(Mathf.Clamp(magnitude * 5f, 5f, 10f));
|
|
}
|
|
else
|
|
{
|
|
AddForce(-2.5f);
|
|
}
|
|
Rotation();
|
|
break;
|
|
case MoveCharacteristic.MoveDown_100cm:
|
|
if (magnitude > 0.2f && depth > 1f)
|
|
{
|
|
AddForce(Mathf.Clamp(magnitude * 5f, 5f, 10f));
|
|
}
|
|
else
|
|
{
|
|
AddForce(-2.5f);
|
|
}
|
|
Rotation();
|
|
break;
|
|
case MoveCharacteristic.Custom:
|
|
rigidbody.AddForce(Vector3.up * objectDisplacement, ForceMode.Acceleration);
|
|
Rotation();
|
|
break;
|
|
}
|
|
void AddForce(float num3)
|
|
{
|
|
Vector3 vector = Vector3.zero;
|
|
Vector3 vector2 = Vector3.zero;
|
|
if (rigidbody.velocity.y > 0f)
|
|
{
|
|
vector = new Vector3(0f, 0f - rigidbody.velocity.y, 0f);
|
|
}
|
|
else
|
|
{
|
|
vector2 = new Vector3(0f, 0f - rigidbody.velocity.y, 0f);
|
|
}
|
|
if (num3 > 0f)
|
|
{
|
|
rigidbody.AddForce(vector2 + Vector3.up * Time.fixedDeltaTime * num3, ForceMode.VelocityChange);
|
|
}
|
|
else
|
|
{
|
|
rigidbody.AddForce(vector + Vector3.up * Time.fixedDeltaTime * num3, ForceMode.VelocityChange);
|
|
}
|
|
}
|
|
void Rotation()
|
|
{
|
|
if (rigidbody.velocity.magnitude > 0.1f)
|
|
{
|
|
Quaternion b2 = Quaternion.LookRotation(rigidbody.velocity, Vector3.up);
|
|
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b2, Time.fixedDeltaTime);
|
|
}
|
|
}
|
|
void StabilizeRotation()
|
|
{
|
|
Quaternion rotation = Quaternion.Euler(0f, rigidbody.transform.localEulerAngles.y, 0f);
|
|
base.transform.SetPositionAndRotation(base.transform.position, rotation);
|
|
}
|
|
}
|
|
|
|
private void InWaterUpdateHandler()
|
|
{
|
|
bool flag = GameWaterSystem.IsPositionUnderWater(base.transform.position + Vector3.down * 0.15f);
|
|
if (!IsInWater && flag)
|
|
{
|
|
IsInWater = true;
|
|
GameWaterSystem.FindWaterLevelAtLocation(base.transform.position, out var waterLevel);
|
|
BuildSplash(new Vector3(base.transform.position.x, waterLevel, base.transform.position.z));
|
|
rigidbody.mass = waterMass;
|
|
if (rigidbody.useGravity)
|
|
{
|
|
EnableDisableGravity(value: false);
|
|
}
|
|
if (rigidbody.drag != waterDrag)
|
|
{
|
|
SetDragRigidbody(waterDrag);
|
|
}
|
|
if ((bool)fFishSystem)
|
|
{
|
|
fFishSystem.AddInwaterObject(this);
|
|
}
|
|
}
|
|
if (IsInWater && !flag)
|
|
{
|
|
IsInWater = false;
|
|
GameWaterSystem.FindWaterLevelAtLocation(base.transform.position, out var waterLevel2);
|
|
BuildSplash(new Vector3(base.transform.position.x, waterLevel2, base.transform.position.z));
|
|
rigidbody.mass = outwaterMass;
|
|
EnableDisableGravity(value: true);
|
|
SetDragRigidbody(normalDrag);
|
|
if ((bool)fFishSystem)
|
|
{
|
|
fFishSystem.DeleteInwaterObject(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void EnableDisableGravity(bool value)
|
|
{
|
|
if (resetVelocityEnterToWater)
|
|
{
|
|
rigidbody.velocity = Vector3.zero;
|
|
}
|
|
Rigidbody[] componentsInChildren = base.gameObject.GetComponentsInChildren<Rigidbody>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].useGravity = value;
|
|
}
|
|
}
|
|
|
|
private void SetDragRigidbody(float value)
|
|
{
|
|
Rigidbody[] componentsInChildren = base.gameObject.GetComponentsInChildren<Rigidbody>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].drag = value;
|
|
}
|
|
}
|
|
|
|
public void ForceByWaterCurrent(Vector3 direction, float powerForce)
|
|
{
|
|
if (useWaterCurrent && IsInWater)
|
|
{
|
|
rigidbody.AddForce(direction * powerForce, ForceMode.Acceleration);
|
|
}
|
|
}
|
|
|
|
private void BuildSplash(Vector3 contactPosition)
|
|
{
|
|
if (useSplashes && !(rigidbody.velocity.magnitude < 1f) && (!FScriptsHandler.Instance.m_PlayerMain.currentRod || !useSplashesOnlyInThrow || FScriptsHandler.Instance.m_PlayerMain.currentRod.currentReel.isBailOpen) && !(rigidbody.velocity.y > 0f))
|
|
{
|
|
Vector3 vector = Vector3.one * (0.02f * Mathf.Abs(rigidbody.velocity.y)) * base.transform.localScale.x;
|
|
int num = 0;
|
|
GameObject obj = UnityEngine.Object.Instantiate(FScriptsHandler.Instance.waterFishSplash[num]);
|
|
obj.transform.position = new Vector3(contactPosition.x, contactPosition.y + 0.05f, contactPosition.z);
|
|
obj.GetComponent<AudioSource>().volume = 0.5f * vector.x;
|
|
vector = Vector3.ClampMagnitude(vector, 1f);
|
|
obj.transform.localScale = vector * 1.5f;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if ((bool)fFishSystem)
|
|
{
|
|
fFishSystem.DeleteInwaterObject(this);
|
|
}
|
|
}
|
|
}
|