Files
UltimateFishing2020/Assets/Scripts/Assembly-CSharp/Rod.cs
2026-03-04 10:03:45 +08:00

253 lines
7.1 KiB
C#

using System;
using RootMotion.FinalIK;
using UnityEngine;
public class Rod : MonoBehaviour
{
[HideInInspector]
public int indexOfslot = -1;
public Transform rHandPoser;
public float throwRange = 60f;
public Transform reelContainer;
[HideInInspector]
public bool isTargetValid = true;
[HideInInspector]
public float targetThrowPower;
private CCDIK _ccdik;
[HideInInspector]
public FishingLine fishingLine;
public float maxBendWeight = 0.5f;
public float maxRodStrength = 10f;
public Vector2 cw;
public float currentRodStrength;
public bool isDamage;
public AudioClip woohhSound;
[HideInInspector]
public bool isTrowing;
[HideInInspector]
public GameObject throwTarget;
private Rigidbody _rigibody;
private AudioSource _audioSource;
[HideInInspector]
public int rodIndex = -1;
[HideInInspector]
public bool isCast;
private float StartThrowDst;
private void Start()
{
_rigibody = GetComponent<Rigidbody>();
_ccdik = GetComponent<CCDIK>();
_audioSource = GetComponent<AudioSource>();
fishingLine = GetComponent<FishingLine>();
IKSolverCCD solver = _ccdik.solver;
solver.OnPostUpdate = (IKSolver.UpdateDelegate)Delegate.Combine(solver.OnPostUpdate, new IKSolver.UpdateDelegate(OnPostUpdate));
}
private void Update()
{
if (!isDamage)
{
CalculateRodTension();
}
}
private void FixedUpdate()
{
if (!isDamage)
{
Throwing();
BendControll();
if (fishingLine.ropeOut == 0f)
{
isCast = false;
}
}
}
public void HideThrowTarget()
{
throwTarget.GetComponentInChildren<Transform>().gameObject.SetActive(value: false);
}
public bool SetThrowTarget(float range, float camRotationX, bool visable = true)
{
if (throwTarget == null)
{
throwTarget = UnityEngine.Object.Instantiate(ScriptsHandler.Instance.fishingThrowTargetPrefab, ScriptsHandler.Instance.transform);
}
throwTarget.GetComponentInChildren<Transform>().gameObject.SetActive(visable);
Vector3 vector = base.transform.position + ScriptsHandler.Instance.m_PlayerMain.transform.forward * range * camRotationX;
Vector3 vector2 = vector;
int mask = LayerMask.GetMask("RiverCurrent");
mask |= LayerMask.GetMask("Water");
mask |= LayerMask.GetMask("Bridges");
if (Physics.Raycast(vector + Vector3.up * 50f, -Vector3.up, out var hitInfo, float.PositiveInfinity, ~mask))
{
if (hitInfo.transform.name == "WaterObject")
{
vector2 = hitInfo.point;
throwTarget.transform.position = new Vector3(vector2.x, vector2.y, vector2.z);
return true;
}
throwTarget.GetComponentInChildren<Transform>().gameObject.SetActive(value: false);
}
return false;
}
public void Throw()
{
if ((bool)fishingLine && !(fishingLine.currentLineHandler.EndLineRigidbody_1 == null) && (bool)_audioSource && (bool)throwTarget)
{
isTrowing = true;
fishingLine.currentLineHandler.EndLineRigidbody_1.isKinematic = true;
if (!isTargetValid)
{
throwTarget.transform.position += new Vector3(UnityEngine.Random.insideUnitCircle.x, 0f, UnityEngine.Random.insideUnitCircle.y) * 2f;
Debug.Log("Zle wycelowane");
}
StartThrowDst = Vector3.Distance(fishingLine.currentLineHandler.EndLineRigidbody_1.position, throwTarget.transform.position);
_audioSource.pitch = Mathf.Clamp(_audioSource.pitch, 2f, StartThrowDst * 0.1f);
_audioSource.volume = Mathf.Clamp(_audioSource.volume, 0f, StartThrowDst * 0.05f);
_audioSource.PlayOneShot(woohhSound);
}
}
private void Throwing()
{
if (!fishingLine || fishingLine.currentLineHandler.EndLineRigidbody_1 == null || (bool)fishingLine.fishObject || !throwTarget)
{
return;
}
if (isTrowing)
{
float num = Vector3.Distance(fishingLine.currentLineHandler.EndLineRigidbody_1.position, throwTarget.transform.position);
Vector3 target = throwTarget.transform.position + new Vector3(0f, StartThrowDst / 2f, 0f);
if (num >= StartThrowDst / 2f && StartThrowDst >= 3f)
{
fishingLine.currentLineHandler.EndLineRigidbody_1.position = Vector3.MoveTowards(fishingLine.currentLineHandler.EndLineRigidbody_1.position, target, Time.deltaTime * (5f + num));
}
else
{
fishingLine.currentLineHandler.EndLineRigidbody_1.position = Vector3.MoveTowards(fishingLine.currentLineHandler.EndLineRigidbody_1.position, throwTarget.transform.position, Time.deltaTime * (2f + num));
}
fishingLine.ropeOut = fishingLine.ropeToHookDistance;
if (fishingLine.currentLineHandler.EndLineRigidbody_1.position == throwTarget.transform.position)
{
isTrowing = false;
ScriptsHandler.Instance.m_PlayerMain.isThrowStartTargeting = false;
ScriptsHandler.Instance.m_PlayerMain.isThrowStartFar = false;
ScriptsHandler.Instance.m_PlayerMain.isThrowStarted = false;
}
}
else if (fishingLine.currentLineHandler.EndLineRigidbody_1.isKinematic)
{
ScriptsHandler.Instance.m_PlayerMain.isThrowStartTargeting = false;
ScriptsHandler.Instance.m_PlayerMain.isThrowStartFar = false;
ScriptsHandler.Instance.m_PlayerMain.isThrowStarted = false;
fishingLine.currentLineHandler.EndLineRigidbody_1.isKinematic = false;
DestroyThrowtarget();
ScriptsHandler.Instance.m_PlayerMain.interactiveObjectsAnimator.SetTrigger("StopThrowing");
Invoke("ReelHandStart", 0.4f);
isCast = true;
}
}
private void ReelHandStart()
{
if (!ScriptsHandler.Instance.m_InteractionControl.LeftHandInteractionWithReel)
{
ScriptsHandler.Instance.m_InteractionControl.StartReelInteractionWithLeftHand();
}
}
public void DestroyThrowtarget()
{
if (throwTarget != null)
{
UnityEngine.Object.Destroy(throwTarget);
}
}
private void OnPostUpdate()
{
fishingLine.RenderLine();
}
public InteractionObject GetReelInteraction()
{
if (fishingLine.reel == null)
{
return null;
}
return fishingLine.reel.interactionObject;
}
public InteractionObject GetReelUnlockInteraction()
{
if (fishingLine.reel == null)
{
return null;
}
return fishingLine.reel.interactionUnlockObject;
}
private void CalculateRodTension()
{
currentRodStrength = fishingLine.ropeTension * fishingLine.lineMaxStrength / maxRodStrength;
currentRodStrength = Mathf.Clamp(currentRodStrength, 0f, 1f);
}
private void BendControll()
{
if (fishingLine.currentLineHandler == null)
{
_ccdik.solver.target = null;
_ccdik.solver.SetIKPositionWeight(Mathf.MoveTowards(_ccdik.solver.IKPositionWeight, 0.0003f, Time.deltaTime * 0.5f));
}
else if (!_ccdik.solver.target)
{
_ccdik.solver.target = fishingLine.currentLineHandler.LineConnector_1.transform;
}
else
{
float value = Mathf.Clamp01(fishingLine.ropeRoznicaTension) * 0.05f;
value = Mathf.Clamp(value, 0.0001f, maxBendWeight);
_ccdik.solver.SetIKPositionWeight(Mathf.Lerp(_ccdik.solver.IKPositionWeight, value, Time.deltaTime));
}
}
public void Damage()
{
Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerSlotsEquip[indexOfslot].SetWearRodReel(GameManager.ItemType.Rod, 0);
ScriptsHandler.Instance.m_PlayerMain.RodDamage();
}
private void OnDestroy()
{
Debug.Log("Rod destroy");
Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerRods[rodIndex].status = GameManager.PlayerData.CRods.Status.InEquip;
UnityEngine.Object.Destroy(fishingLine.currentLineHandler.gameObject);
}
}