408 lines
10 KiB
C#
408 lines
10 KiB
C#
using System;
|
|
using UFS2.Gameplay;
|
|
using UnityEngine;
|
|
|
|
public class FLure : HookBaitLureBase
|
|
{
|
|
public enum MoveType
|
|
{
|
|
None = 0,
|
|
PowolnyWleczony = 1,
|
|
Wleczony = 2,
|
|
Opadający = 3,
|
|
PowolnyOpadający = 4
|
|
}
|
|
|
|
public static FLure CurrentLure;
|
|
|
|
[HideInInspector]
|
|
public int gameID = -1;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody rigidbody;
|
|
|
|
[HideInInspector]
|
|
public FWaterDisplacement waterDisplacement;
|
|
|
|
public Vector3 rotationInFishJaw = Vector3.zero;
|
|
|
|
[Tooltip("maksymalny kąt odchylenia przynetu przy sciaganiu, rotacja w osi Y")]
|
|
public float rotateVeloMaxAngle = 10f;
|
|
|
|
[Tooltip("predkosc odchylenia przynetu przy sciaganiu, rotacja w osi Y")]
|
|
public float rotateVeloMaxSpeed = 3f;
|
|
|
|
[HideInInspector]
|
|
public FRod currentRod;
|
|
|
|
[Tooltip("Punkt połaczenia z rybą")]
|
|
public Rigidbody fishJoiner;
|
|
|
|
public Transform hook;
|
|
|
|
private Vector3 hookPositionStart;
|
|
|
|
public bool isLookingDisable;
|
|
|
|
private bool isInWater;
|
|
|
|
private Animator animator;
|
|
|
|
private float takeRange = 3f;
|
|
|
|
[HideInInspector]
|
|
public float percentEfficacy;
|
|
|
|
public MoveType LureMoveType = MoveType.Wleczony;
|
|
|
|
[HideInInspector]
|
|
public MoveType currentMoveType;
|
|
|
|
private MoveType newMoveType;
|
|
|
|
private float delayMoveTypeTimer;
|
|
|
|
private float moveTowardsFactor;
|
|
|
|
private float moveUpperFactor;
|
|
|
|
private ConfigurableJoint cfgJoint;
|
|
|
|
private Transform currentWaterDrop;
|
|
|
|
private Vector3 lastPos;
|
|
|
|
private Rigidbody lastConnectedBody;
|
|
|
|
private float rotateVelo;
|
|
|
|
private bool destroyDropFx;
|
|
|
|
public ConfigurableJoint CfgJoint => cfgJoint;
|
|
|
|
public Rigidbody LineConnector => cfgJoint.connectedBody;
|
|
|
|
public static event Action OnLureEnterWater;
|
|
|
|
public static event Action OnLureExitWater;
|
|
|
|
private void Start()
|
|
{
|
|
if ((bool)hook)
|
|
{
|
|
hookPositionStart = hook.localPosition;
|
|
}
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
waterDisplacement = GetComponent<FWaterDisplacement>();
|
|
animator = GetComponent<Animator>();
|
|
cfgJoint = GetComponent<ConfigurableJoint>();
|
|
if ((bool)fishJoiner)
|
|
{
|
|
FixedJoint component = fishJoiner.GetComponent<FixedJoint>();
|
|
if ((bool)component && !component.connectedBody)
|
|
{
|
|
if ((bool)component.transform.parent.GetComponent<Rigidbody>())
|
|
{
|
|
component.connectedBody = component.transform.parent.GetComponent<Rigidbody>();
|
|
}
|
|
else
|
|
{
|
|
component.connectedBody = base.transform.GetComponent<Rigidbody>();
|
|
}
|
|
}
|
|
}
|
|
SetupSphereCollider();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
FishFightState.OnEnter += DisableColliders;
|
|
FishFightState.OnExit += EnableColliders;
|
|
FishEntity.OnPostSpitOutBait += EnablePhysics;
|
|
FishEntityManager.AddTransformCull(base.transform);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
FishFightState.OnEnter -= DisableColliders;
|
|
FishFightState.OnExit -= EnableColliders;
|
|
FishEntity.OnPostSpitOutBait -= EnablePhysics;
|
|
FishEntityManager.RemoveTransformCull(base.transform);
|
|
FLure.OnLureExitWater?.Invoke();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
RotationToVelocity();
|
|
ShowWaterFX();
|
|
MoveTypeController();
|
|
if ((bool)FishEntity.CurrentFishInFight)
|
|
{
|
|
waterDisplacement.isFreeze = true;
|
|
fishJoiner.gameObject.SetActive(value: true);
|
|
}
|
|
else
|
|
{
|
|
waterDisplacement.isFreeze = false;
|
|
fishJoiner.gameObject.SetActive(value: false);
|
|
}
|
|
if ((bool)hook)
|
|
{
|
|
hook.localPosition = hookPositionStart;
|
|
}
|
|
if ((bool)FishEntity.CurrentFishInFight && isLookingDisable)
|
|
{
|
|
isLookingDisable = false;
|
|
}
|
|
if ((bool)animator && base.transform.position.y < 0.2f && animator.runtimeAnimatorController != null)
|
|
{
|
|
animator.SetFloat("Speed", rigidbody.velocity.magnitude);
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (waterDisplacement.IsInWater)
|
|
{
|
|
if (!isInWater)
|
|
{
|
|
isInWater = true;
|
|
FLure.OnLureEnterWater?.Invoke();
|
|
}
|
|
}
|
|
else if (isInWater)
|
|
{
|
|
isInWater = false;
|
|
FLure.OnLureExitWater?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void EnablePhysics(FishEntity fish)
|
|
{
|
|
rigidbody.constraints = RigidbodyConstraints.None;
|
|
rigidbody.detectCollisions = true;
|
|
EnableColliders();
|
|
}
|
|
|
|
private void DisableColliders()
|
|
{
|
|
waterDisplacement.IsEnabled = false;
|
|
Collider[] componentsInChildren = base.gameObject.GetComponentsInChildren<Collider>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = false;
|
|
}
|
|
}
|
|
|
|
private void EnableColliders()
|
|
{
|
|
waterDisplacement.IsEnabled = true;
|
|
Collider[] componentsInChildren = base.gameObject.GetComponentsInChildren<Collider>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = true;
|
|
}
|
|
}
|
|
|
|
private void RotationToVelocity()
|
|
{
|
|
if (!waterDisplacement)
|
|
{
|
|
return;
|
|
}
|
|
if (base.transform.position.y > 0.02f)
|
|
{
|
|
if (base.transform.position.y > 0.2f && (bool)animator)
|
|
{
|
|
animator.SetFloat("Speed", 0f);
|
|
}
|
|
rigidbody.freezeRotation = false;
|
|
}
|
|
else
|
|
{
|
|
if (currentRod.fishingLine.linelenghtDiferent <= 0f)
|
|
{
|
|
return;
|
|
}
|
|
Vector3 vector = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
|
|
if (vector != Vector3.zero)
|
|
{
|
|
Quaternion b = Quaternion.LookRotation(vector, Vector3.up);
|
|
base.transform.rotation = Quaternion.Slerp(base.transform.rotation, b, rigidbody.velocity.magnitude * 5f * Time.deltaTime);
|
|
}
|
|
if (rotateVeloMaxAngle > 0f)
|
|
{
|
|
rotateVelo = rotateVeloMaxAngle * (1f - Mathf.PingPong(Time.time * rigidbody.velocity.normalized.magnitude * rotateVeloMaxSpeed, 2f));
|
|
}
|
|
else if (rotateVeloMaxAngle == 0f)
|
|
{
|
|
rotateVelo = Time.time * rigidbody.velocity.normalized.magnitude * rotateVeloMaxSpeed;
|
|
}
|
|
if (rigidbody.velocity.magnitude > 0f)
|
|
{
|
|
if (rotateVeloMaxAngle == 0f)
|
|
{
|
|
base.transform.Rotate(Vector3.forward, rotateVelo);
|
|
return;
|
|
}
|
|
base.transform.Rotate(Vector3.up, rotateVelo);
|
|
rigidbody.AddForce(base.transform.right * rotateVelo, ForceMode.Acceleration);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MoveTypeController()
|
|
{
|
|
GameWaterSystem.FindWaterLevelAtLocation(base.transform.position, out var waterLevel);
|
|
if (base.transform.position.y > waterLevel + 0.1f || Vector3.Distance(currentRod.transform.position, base.transform.position) < 5f)
|
|
{
|
|
moveTowardsFactor = 0f;
|
|
moveUpperFactor = 0f;
|
|
percentEfficacy = 0f;
|
|
currentMoveType = MoveType.None;
|
|
newMoveType = MoveType.None;
|
|
return;
|
|
}
|
|
float num = (Mathf.Abs(rigidbody.velocity.x) + Mathf.Abs(rigidbody.velocity.z)) * 0.5f;
|
|
moveTowardsFactor = Mathf.MoveTowards(moveTowardsFactor, Mathf.Clamp01(num * 1.5f), Time.deltaTime * 0.5f);
|
|
moveUpperFactor = Mathf.MoveTowards(moveUpperFactor, Mathf.Clamp01((0f - rigidbody.velocity.y) * 1.3f), Time.deltaTime * 0.5f);
|
|
if (moveTowardsFactor > 0.01f && moveTowardsFactor < 0.5f)
|
|
{
|
|
newMoveType = MoveType.PowolnyWleczony;
|
|
}
|
|
else if (moveTowardsFactor >= 0.5f && moveTowardsFactor < 1f)
|
|
{
|
|
newMoveType = MoveType.Wleczony;
|
|
}
|
|
else if (moveTowardsFactor <= 0.1f)
|
|
{
|
|
if (moveUpperFactor > 0f && moveUpperFactor < 0.4f)
|
|
{
|
|
newMoveType = MoveType.PowolnyOpadający;
|
|
}
|
|
else if (moveUpperFactor >= 0.4f && moveUpperFactor < 0.8f)
|
|
{
|
|
newMoveType = MoveType.Opadający;
|
|
}
|
|
else
|
|
{
|
|
newMoveType = MoveType.None;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
newMoveType = MoveType.None;
|
|
}
|
|
if (currentMoveType == LureMoveType)
|
|
{
|
|
percentEfficacy = Mathf.MoveTowards(percentEfficacy, 1f, Time.deltaTime * 0.4f);
|
|
}
|
|
else
|
|
{
|
|
percentEfficacy = Mathf.MoveTowards(percentEfficacy, 0f, Time.deltaTime * 0.2f);
|
|
}
|
|
if (newMoveType != currentMoveType)
|
|
{
|
|
delayMoveTypeTimer += Time.deltaTime;
|
|
if (delayMoveTypeTimer > 1f)
|
|
{
|
|
currentMoveType = newMoveType;
|
|
delayMoveTypeTimer = 0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool CheckBaitEfficacy(float distanceToFish)
|
|
{
|
|
if ((bool)TutorialManager.Instance)
|
|
{
|
|
return true;
|
|
}
|
|
float num = 0f;
|
|
GameManager.Instance._playerData.GetGameItemIndexByItemId(GameManager.ItemType.Bait, gameID);
|
|
int num2 = 100;
|
|
if (Singleton<SaveDataManager>.Instance.HasCurrentPlayerSkill(GameManager.Skills.attractive_player_10p))
|
|
{
|
|
num = 0.1f;
|
|
}
|
|
else if (Singleton<SaveDataManager>.Instance.HasCurrentPlayerSkill(GameManager.Skills.attractive_player_5p))
|
|
{
|
|
num = 0.05f;
|
|
}
|
|
float num3 = Vector3.Distance(currentRod.transform.position, base.transform.position);
|
|
if (num3 < 5f)
|
|
{
|
|
return false;
|
|
}
|
|
Mathf.Clamp((12f - num3) * 10f, 0f, 50f);
|
|
float num4 = num2;
|
|
num4 = num2;
|
|
takeRange = 1f + num4 * 0.5f;
|
|
int num5 = UnityEngine.Random.Range(0, 1);
|
|
Debug.Log("Bait efficacy range: " + (takeRange + takeRange * num) + " Bait current efficacy > rand: " + num4 + "/" + num5);
|
|
if (takeRange + takeRange * num < distanceToFish)
|
|
{
|
|
Debug.Log("Lure distans is too high");
|
|
}
|
|
if ((float)num5 < num4)
|
|
{
|
|
Debug.Log("Lure efficacy is too low");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void DestroyDelayFxDrop()
|
|
{
|
|
if ((bool)currentWaterDrop)
|
|
{
|
|
UnityEngine.Object.Destroy(currentWaterDrop.gameObject);
|
|
destroyDropFx = false;
|
|
}
|
|
}
|
|
|
|
private void ShowWaterFX()
|
|
{
|
|
GameWaterSystem.FindWaterLevelAtLocation(base.transform.position, out var waterLevel);
|
|
float num = waterLevel - base.transform.position.y;
|
|
if (num < 0.1f && num > -0.1f)
|
|
{
|
|
if ((bool)currentWaterDrop)
|
|
{
|
|
currentWaterDrop.position = new Vector3(base.transform.position.x, 0f, base.transform.position.z);
|
|
}
|
|
}
|
|
else if ((bool)currentWaterDrop)
|
|
{
|
|
if (!destroyDropFx)
|
|
{
|
|
Invoke("DestroyDelayFxDrop", 4f);
|
|
}
|
|
destroyDropFx = true;
|
|
}
|
|
}
|
|
|
|
private void SetupSphereCollider()
|
|
{
|
|
SphereCollider sphereCollider = base.gameObject.AddComponent<SphereCollider>();
|
|
sphereCollider.enabled = true;
|
|
sphereCollider.radius = takeRange;
|
|
sphereCollider.isTrigger = true;
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
if (!waterDisplacement.IsInWater && (bool)currentRod && (bool)currentRod.fishingLine && (bool)currentRod.fishingLine.currentLineHandler && !currentRod.currentFish && currentRod.fishingLine.currentLineHandler.PhisicsLineOut > 5f)
|
|
{
|
|
GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_ON_THE_GROUND"), FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if ((bool)currentWaterDrop)
|
|
{
|
|
UnityEngine.Object.Destroy(currentWaterDrop.gameObject);
|
|
}
|
|
}
|
|
}
|