290 lines
6.6 KiB
C#
290 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Sirenix.Utilities;
|
|
using UFS2.Gameplay;
|
|
using UnityEngine;
|
|
|
|
public class FHook : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public int gameID = -1;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody rigidbody;
|
|
|
|
[HideInInspector]
|
|
public FWaterDisplacement waterDisplacement;
|
|
|
|
[HideInInspector]
|
|
public FRod currentRod;
|
|
|
|
[Tooltip("Punkt połaczenia z rybą")]
|
|
public Rigidbody fishJoiner;
|
|
|
|
public Transform baitContainer;
|
|
|
|
public Vector3 rotationInFishJaw = Vector3.zero;
|
|
|
|
public bool isLookingDisable;
|
|
|
|
private bool isInWater;
|
|
|
|
private float hookTimer;
|
|
|
|
public int hookNotAcceptFishCounter;
|
|
|
|
private float originalObjectDisplacement;
|
|
|
|
private float currentDistanceToBottom;
|
|
|
|
private float feederLiftDistanceToBottom = 1f;
|
|
|
|
private ConfigurableJoint _CfgJoint;
|
|
|
|
private Rigidbody _Rigidbody;
|
|
|
|
private List<Renderer> hidenRenderers;
|
|
|
|
public ConfigurableJoint CfgJoint
|
|
{
|
|
get
|
|
{
|
|
return _CfgJoint;
|
|
}
|
|
private set
|
|
{
|
|
_CfgJoint = value;
|
|
}
|
|
}
|
|
|
|
public Rigidbody Rigidbody
|
|
{
|
|
get
|
|
{
|
|
return _Rigidbody;
|
|
}
|
|
private set
|
|
{
|
|
_Rigidbody = value;
|
|
}
|
|
}
|
|
|
|
public bool IsFeederOnGround { get; private set; }
|
|
|
|
public static event Action OnHookWaterEnter;
|
|
|
|
public static event Action OnHookWaterExit;
|
|
|
|
public static event Action OnFeederWaterEnter;
|
|
|
|
public static event Action OnFeederWaterExit;
|
|
|
|
public static event Action OnFeederGroundEnter;
|
|
|
|
public static event Action OnFeederGroundExit;
|
|
|
|
private void Start()
|
|
{
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
waterDisplacement = GetComponent<FWaterDisplacement>();
|
|
fishJoiner.mass = 0.01f;
|
|
CfgJoint = GetComponent<ConfigurableJoint>();
|
|
Rigidbody = GetComponent<Rigidbody>();
|
|
originalObjectDisplacement = waterDisplacement.objectDisplacement;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
FishEntityManager.AddTransformCull(base.transform);
|
|
FishCatchState.OnExit += ShowRenderer;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
FishEntityManager.RemoveTransformCull(base.transform);
|
|
FishCatchState.OnExit -= ShowRenderer;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!currentRod)
|
|
{
|
|
return;
|
|
}
|
|
waterDisplacement.useSplashes = !currentRod.isThrowing;
|
|
if ((bool)currentRod.currentFish)
|
|
{
|
|
waterDisplacement.isFreeze = true;
|
|
AddHookNotAcceptedCount(reset: true);
|
|
}
|
|
else
|
|
{
|
|
waterDisplacement.isFreeze = false;
|
|
if (waterDisplacement.IsInWater)
|
|
{
|
|
Quaternion b = Quaternion.Euler(0f, rigidbody.transform.localEulerAngles.y, rigidbody.velocity.x - 1f);
|
|
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b, Time.deltaTime * 1f);
|
|
if ((bool)currentRod.takeFish)
|
|
{
|
|
AddHookNotAcceptedCount(reset: true);
|
|
}
|
|
else
|
|
{
|
|
AddHookNotAcceptedCount(reset: false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AddHookNotAcceptedCount(reset: true);
|
|
}
|
|
}
|
|
if (!currentRod.takeFish && !currentRod.currentFish && isLookingDisable)
|
|
{
|
|
isLookingDisable = false;
|
|
}
|
|
}
|
|
|
|
public void HideRenderer()
|
|
{
|
|
FindEnabledRenderers();
|
|
hidenRenderers.Where((Renderer r) => r != null).ForEach(delegate(Renderer r)
|
|
{
|
|
r.enabled = false;
|
|
});
|
|
}
|
|
|
|
public void ShowRenderer()
|
|
{
|
|
FindEnabledRenderers();
|
|
hidenRenderers.Where((Renderer r) => r != null).ForEach(delegate(Renderer r)
|
|
{
|
|
r.enabled = true;
|
|
});
|
|
}
|
|
|
|
private void FindEnabledRenderers()
|
|
{
|
|
if (hidenRenderers != null)
|
|
{
|
|
return;
|
|
}
|
|
hidenRenderers = new List<Renderer>();
|
|
baitContainer = ((baitContainer == null) ? base.transform : baitContainer);
|
|
Renderer[] componentsInChildren = baitContainer.GetComponentsInChildren<Renderer>();
|
|
foreach (Renderer renderer in componentsInChildren)
|
|
{
|
|
if (renderer.enabled)
|
|
{
|
|
hidenRenderers.Add(renderer);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FeederInWaterUpdateHandler()
|
|
{
|
|
if (waterDisplacement.IsInWater)
|
|
{
|
|
if (!isInWater)
|
|
{
|
|
isInWater = true;
|
|
FHook.OnHookWaterEnter?.Invoke();
|
|
if ((bool)currentRod && (bool)currentRod.currentFeeder)
|
|
{
|
|
FHook.OnFeederWaterEnter?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
else if (isInWater)
|
|
{
|
|
isInWater = false;
|
|
FHook.OnHookWaterExit?.Invoke();
|
|
if ((bool)currentRod && (bool)currentRod.currentFeeder)
|
|
{
|
|
FHook.OnFeederWaterExit?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FeederOnGroundUpdateHandler()
|
|
{
|
|
if (!waterDisplacement.IsInWater)
|
|
{
|
|
return;
|
|
}
|
|
if (IsFeederOnGround)
|
|
{
|
|
if (currentDistanceToBottom > feederLiftDistanceToBottom)
|
|
{
|
|
IsFeederOnGround = false;
|
|
FHook.OnFeederGroundExit?.Invoke();
|
|
}
|
|
}
|
|
else if (currentDistanceToBottom <= feederLiftDistanceToBottom)
|
|
{
|
|
IsFeederOnGround = true;
|
|
FHook.OnFeederGroundEnter?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
DisplcementController();
|
|
FeederInWaterUpdateHandler();
|
|
FeederOnGroundUpdateHandler();
|
|
}
|
|
|
|
private void AddHookNotAcceptedCount(bool reset)
|
|
{
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().GameMode == GameManager.PlayerData.CPlayer.GameMode.Realistic || FScriptsHandler.Instance.m_PlayerMain.currentRod != currentRod)
|
|
{
|
|
return;
|
|
}
|
|
if (reset)
|
|
{
|
|
hookTimer = 0f;
|
|
hookNotAcceptFishCounter = 0;
|
|
return;
|
|
}
|
|
hookTimer += Time.deltaTime;
|
|
if (hookTimer >= 300f && hookNotAcceptFishCounter > 5)
|
|
{
|
|
hookTimer = 0f;
|
|
hookNotAcceptFishCounter = 0;
|
|
GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_NOT_ACCEPT_FISH_INFO"), FScriptsHandler.Instance.m_Canvas.transform);
|
|
}
|
|
}
|
|
|
|
private void DisplcementController()
|
|
{
|
|
if ((bool)currentRod && !currentRod.currentFishEntity && waterDisplacement.IsInWater)
|
|
{
|
|
float num = 10f;
|
|
float num2 = 1f;
|
|
Vector3 origin = base.transform.position + Vector3.up * num2;
|
|
currentDistanceToBottom = num;
|
|
if (Physics.Raycast(origin, Vector3.down, out var hitInfo, num + num2, LayerMask.GetMask("Terrain", "UnderwaterObject")))
|
|
{
|
|
currentDistanceToBottom = hitInfo.distance - num2;
|
|
}
|
|
if ((bool)currentRod.currentFeeder)
|
|
{
|
|
float objectDisplacement = Mathf.Lerp(10f, originalObjectDisplacement, currentDistanceToBottom / feederLiftDistanceToBottom);
|
|
waterDisplacement.objectDisplacement = objectDisplacement;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
waterDisplacement.objectDisplacement = originalObjectDisplacement;
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
if (!waterDisplacement.IsInWater && (bool)currentRod && (bool)currentRod.fishingLine && (bool)currentRod.fishingLine.currentLineHandler && !currentRod.currentFish && currentRod.fishingLine.currentLineHandler.PhisicsLineOut > 5f && Vector3.Distance(base.transform.position, currentRod.transform.position) > 5f)
|
|
{
|
|
GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_ON_THE_GROUND"), FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
|
|
}
|
|
}
|
|
}
|