using System; using System.Collections.Generic; using BitStrap; using UnityEngine; public class FishingReel : MonoBehaviour { public enum ReelType { STANDARD = 0, LINE_COUNTER = 1, GROUND = 2, FLY = 3 } public enum DragType { FRONT = 0, BACK = 1, STAR = 2, CENTRIFUGAL = 3 } public ReelType reelType; public DragType dragType; [ReadOnly] public float currentDrag = 0.5f; [ReadOnly] public float maxDrag = 1f; public float maxDragWeight; public static int maxDragSteps = 12; public int dragSteps = 20; public float dragStepValue = 0.1f; public float durability = 0.5f; public bool isDragActive = true; [ReadOnly] public float currentRotationSpeed; public float maxLineLength = 150f; public int reelSize = 1000; public float ratio = 5.2f; public int weight = 250; public int ballBearings = 4; public string rotationSound = "Reel_01"; public string spoolSound = "Spool_01"; public string dragSound = "Drag_01"; public float rotationSoundPitch; public float spoolAdditionalPitch; [HideInInspector] public AudioObject rotationAudioObject; [HideInInspector] public AudioObject dragAudioObject; [HideInInspector] public AudioObject spoolAudioObject; [Header("Reel Parts")] public Transform mountPosition; public Transform handMountPosition; public Transform handle; public Transform handleGrab; public Vector3 handleRotationVector = Vector3.zero; public Vector3 handleGrabVector = Vector3.zero; private Vector3 handleGrabStartRot = Vector3.zero; public Vector2 handleRotateSpeed = new Vector2(400f, 800f); [ReadOnly] public Vector3 handleStartRotation = Vector3.zero; [Space(10f)] public Transform lineGuard; public Vector3 lineGuardRotationVector = Vector3.zero; public float lineGuardRotateSpeed = 2f; [Space(10f)] public Transform spoolParent; public Vector2 spoolParentMoveSpeed = new Vector2(0.07f, 0.22f); public Vector3 spoolParentMaxPos = Vector3.zero; private LTDescr leanSpoolParentMove; [Space(10f)] public Transform spool; public Vector3 spoolRotationVector = Vector3.zero; public Vector2 spoolRotateSpeed = new Vector2(0f, 700f); [ReadOnly] [Space(10f)] public bool isArchOpened; public Transform arch; public Vector3 archClosedRotation = Vector3.zero; public Vector3 archOpenedRotation = Vector3.zero; [Space(10f)] public Transform drag; [Space(10f)] public float lineSpeed = 2f; public GameObject line; public float lineFullScale; public float lineEmptyScale; public Transform reelLineFinish; public MeshRenderer shortLine; [Space(10f)] public float baitDragSpeed = 10f; [HideInInspector] public LTDescr flyHandTween; [HideInInspector] public bool isReelingIn; [HideInInspector] public bool isReelingOut; [HideInInspector] public FishingHands fishingHands; [ReadOnly] public bool vrHandle_IsHolding; private Transform vrHandle_HelpPoint; private Transform vrHandle_HelpPointer; private float vrHandle_PrevAngle; public float vrHandle_DiffAngle; public List vrHandle_History = new List(); public bool useDiffRotation = true; public float vrHandle_ModelRotateOffset; private void Start() { if (base.transform.parent == null) { base.gameObject.SetActive(false); } currentDrag = 0f; if (dragSteps > 10) { dragSteps = 10; } dragStepValue = 1f / (float)dragSteps; handleRotateSpeed = new Vector2(200f, 750f); handleStartRotation = handle.localEulerAngles; if ((bool)handleGrab) { handleGrabStartRot = handleGrab.localEulerAngles; } FreezeTransform componentInChildren = handle.GetComponentInChildren(); if ((bool)componentInChildren) { componentInChildren.dontUpdatePosition = true; } rotationAudioObject = AudioController.Play(rotationSound, base.transform); rotationAudioObject.Pause(); spoolAudioObject = AudioController.Play(spoolSound, base.transform); spoolAudioObject.Pause(); if ((bool)spoolParent) { leanSpoolParentMove = LeanTween.moveLocal(spoolParent.gameObject, spoolParent.localPosition + spoolParentMaxPos, spoolParentMoveSpeed.x).setLoopPingPong().setRepeat(-1) .pause(); } if (!GameController.Instance.iceLevel) { lineFullScale = line.transform.localScale.x; } Debug.Log("line.transform.localScale.x: " + line.transform.localScale.x); CalculateDragWeight(); } private void Update() { if (Time.timeScale == 0f) { return; } if (!isReelingIn) { if (leanSpoolParentMove != null && leanSpoolParentMove.direction != 0f) { leanSpoolParentMove.pause(); } if (rotationAudioObject != null) { if (!rotationAudioObject.IsPaused()) { rotationAudioObject.Pause(); } } else { Debug.LogError("Reel rotationAudioObject == null"); } fishingHands.ShakeHandsWhileReeling(false); } if (!isReelingOut) { if (spoolAudioObject != null) { if (!spoolAudioObject.IsPaused()) { spoolAudioObject.Pause(); } } else { Debug.LogError("Reel spoolAudioObject == null"); } } if (!isReelingIn && !isReelingOut) { currentRotationSpeed = 0f; } if ((flyHandTween != null && flyHandTween.direction < 0f) || (VRManager.Instance.IsVRReeling() && fishingHands.vrRopeIsReelingIn)) { currentRotationSpeed = 1f; fishingHands.fishingLine.ReelLine(currentRotationSpeed); } isReelingIn = false; isReelingOut = false; } public void UpdateInput() { if (VRManager.Instance.IsVRReeling()) { UpdateInputVRControllers(); if (UtilitiesInput.isReelingIn || UtilitiesInput.isReelingOut) { if (reelType == ReelType.FLY && !fishingHands.fishingPlayer.fish) { fishingHands.currentUserReelSpeed = 0.2f; } else { fishingHands.currentUserReelSpeed = (0f - vrHandle_DiffAngle) * ((!fishingHands.bait.fish) ? VRControllersManager.Instance.handReelNoFishSpeedMultiplier : VRControllersManager.Instance.handReelSpeedMultiplier); } fishingHands.currentUserReelSpeed = Mathf.Clamp(fishingHands.currentUserReelSpeed, 0.1f, 0.9f); } else { fishingHands.currentUserReelSpeed = 0f; } if ((fishingHands.fishingRod.rodType != FishingRod.RodType.ICE_FISHING && reelType != ReelType.FLY) || (bool)fishingHands.bait.fish) { if (fishingHands.currentUserReelSpeed != 0f) { UtilitiesInput.SetVibration(0, false, Mathf.Lerp(0.08f, 0.17f, fishingHands.currentUserReelSpeed) * ((!fishingHands.fishingPlayer.fish) ? 0.5f : 1.25f)); } else { UtilitiesInput.StopVibration(false); } } } else if (UtilitiesInput.isReelingIn && VRManager.Instance.IsControllersInput()) { fishingHands.currentUserReelSpeed = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, VRControllersManager.Instance.GetSecondaryController()); fishingHands.currentUserReelSpeed *= ((!fishingHands.bait.fish) ? VRControllersManager.Instance.handReelNoFishSpeedMultiplier : VRControllersManager.Instance.handReelSpeedMultiplier); fishingHands.currentUserReelSpeed = Mathf.Clamp(fishingHands.currentUserReelSpeed, 0.1f, 0.9f); } else if (UtilitiesInput.GetButtonDown("REEL_INC") || UtilitiesInput.GetButtonDown("REEL_DEC")) { if ((UtilitiesInput.GetButtonDown("REEL_INC") && fishingHands.currentUserReelSpeed < 0.9f) || (UtilitiesInput.GetButtonDown("REEL_DEC") && fishingHands.currentUserReelSpeed > 0.1f)) { AudioController.Play("Reel_SpeedChange_01"); } fishingHands.currentUserReelSpeed += 0.2f * ((!UtilitiesInput.GetButtonDown("REEL_INC")) ? (-1f) : 1f); fishingHands.currentUserReelSpeed = Mathf.Clamp(fishingHands.currentUserReelSpeed, 0.1f, 0.9f); } float num = 0f; if (UtilitiesInput.GetButtonDown("DRAG_INC")) { num = 1f; } else if (UtilitiesInput.GetButtonDown("DRAG_DEC")) { num = -1f; } else if (Mathf.Abs(UtilitiesInput.GetAxis("DRAG_CHANGE")) >= 1f) { num = UtilitiesInput.GetAxis("DRAG_CHANGE"); } if (num != 0f) { if ((currentDrag != 1f || !(num > 0f)) && (currentDrag != 0f || !(num < 0f))) { AudioController.Play(dragSound); } currentDrag += ((!(num > 0f)) ? (0f - dragStepValue) : dragStepValue); if (currentDrag < 0.01f) { currentDrag = 0f; } currentDrag = Mathf.Clamp(currentDrag, 0f, 1f); } } public void UpdateInputVRControllers() { if (VRControllersManager.Instance.GetVRController(VRControllersManager.Instance.GetSecondaryController()) == null || (reelType == ReelType.FLY && !fishingHands.fishingPlayer.fish)) { return; } if (vrHandle_HelpPoint == null) { vrHandle_HelpPoint = new GameObject("VRHelpPoint").transform; vrHandle_HelpPoint.parent = handle; } if (vrHandle_HelpPointer == null) { vrHandle_HelpPointer = new GameObject("VRHelpPointer").transform; vrHandle_HelpPointer.parent = handle.parent; vrHandle_HelpPointer.position = handle.position; } Transform reelCatchTransform = VRControllersManager.Instance.GetVRController(VRControllersManager.Instance.GetSecondaryController()).reelCatchTransform; float num = Vector3.Distance(handMountPosition.position, reelCatchTransform.position); if (vrHandle_IsHolding && (num > VRControllersManager.Instance.handToReelDistanceRelease || (VRManager.Instance.IsVRReelingGrip() && !UtilitiesInput.GetButton("VR_REEL_GRIP")))) { vrHandle_IsHolding = false; UtilitiesInput.SetVibration(0, false, 0.2f, 0.1f); } else if (!vrHandle_IsHolding && num < VRControllersManager.Instance.handToReelDistanceCatch && (!VRManager.Instance.IsVRReelingGrip() || UtilitiesInput.GetButton("VR_REEL_GRIP"))) { vrHandle_IsHolding = true; UtilitiesInput.SetVibration(0, false, 0.2f, 0.1f); } if (!vrHandle_IsHolding) { vrHandle_DiffAngle = 0f; vrHandle_History.Clear(); if ((bool)fishingHands) { fishingHands.leftArm.SetActive(false); } return; } if ((bool)fishingHands) { fishingHands.leftArm.SetActive(true); } vrHandle_HelpPoint.position = reelCatchTransform.position; if (handleRotationVector.x != 0f) { vrHandle_HelpPoint.localPosition = new Vector3(0f, vrHandle_HelpPoint.localPosition.y, vrHandle_HelpPoint.localPosition.z); } else if (handleRotationVector.y != 0f) { vrHandle_HelpPoint.localPosition = new Vector3(vrHandle_HelpPoint.localPosition.x, 0f, vrHandle_HelpPoint.localPosition.z); } else if (handleRotationVector.z != 0f) { vrHandle_HelpPoint.localPosition = new Vector3(vrHandle_HelpPoint.localPosition.x, vrHandle_HelpPoint.localPosition.y, 0f); } vrHandle_HelpPointer.position = handle.position; vrHandle_HelpPointer.LookAt(vrHandle_HelpPoint); Debug.DrawLine(vrHandle_HelpPointer.position, vrHandle_HelpPoint.position, Color.yellow); float num2 = vrHandle_HelpPointer.localEulerAngles.x; float num3 = vrHandle_HelpPointer.localEulerAngles.y; if (handleRotationVector.x != 0f) { num3 -= 90f; if (num3 > 180f) { num3 -= 360f; } } else if (handleRotationVector.y != 0f) { num2 = vrHandle_HelpPointer.localEulerAngles.y; num3 = vrHandle_HelpPointer.localEulerAngles.z; } else if (handleRotationVector.z != 0f && num3 > 180f) { num3 -= 360f; } num2 = ((num3 < 0f) ? (num2 + 180f) : ((!(num2 > 0f)) ? (0f - num2) : (360f - num2))); if (num2 > 360f && vrHandle_PrevAngle < 360f) { num2 -= 360f; } else if (num2 < 360f && vrHandle_PrevAngle > 360f) { vrHandle_PrevAngle -= 360f; } vrHandle_DiffAngle = num2 - vrHandle_PrevAngle; vrHandle_PrevAngle = num2; if (VRControllersManager.Instance.IsLeftHanded()) { num2 += 180f; } if (handleRotationVector.x != 0f) { handle.localEulerAngles = new Vector3(num2 + vrHandle_ModelRotateOffset, 0f, 0f); } else if (handleRotationVector.y != 0f) { handle.localEulerAngles = new Vector3(0f, 0f - num2 + vrHandle_ModelRotateOffset, 0f); } else if (handleRotationVector.z != 0f) { handle.localEulerAngles = new Vector3(0f, 0f, num2 + vrHandle_ModelRotateOffset); } if (vrHandle_DiffAngle > 180f || vrHandle_DiffAngle < -180f) { if (vrHandle_History.Count > 0) { vrHandle_DiffAngle = vrHandle_History[vrHandle_History.Count - 1]; } else { vrHandle_DiffAngle = 0f; } } else { vrHandle_DiffAngle *= Time.deltaTime; if (handleRotationVector.x > 0f || handleRotationVector.y < 0f || handleRotationVector.z > 0f) { vrHandle_DiffAngle = 0f - vrHandle_DiffAngle; } } if (vrHandle_DiffAngle >= -0.03f && vrHandle_History.Count > 0) { vrHandle_History.Clear(); return; } float num4 = 0f; if (vrHandle_History.Count < 20) { vrHandle_History.Add(vrHandle_DiffAngle); for (int i = 0; i < vrHandle_History.Count; i++) { num4 += vrHandle_History[i]; } } else { for (int j = 0; j < vrHandle_History.Count - 1; j++) { vrHandle_History[j] = vrHandle_History[j + 1]; num4 += vrHandle_History[j]; } vrHandle_History[vrHandle_History.Count - 1] = vrHandle_DiffAngle; num4 += vrHandle_DiffAngle; } num4 /= (float)vrHandle_History.Count; vrHandle_DiffAngle = num4; } public void RotateLineGuard(float speed, bool fakeRotation = false) { if (speed < 0f && fishingHands.fishingLine.isMaxLength()) { Debug.Log("fishingHands.fishingLine.isMaxLength()"); return; } if (speed < 0f && !fishingHands.fishingLine.CanReelOut()) { Debug.Log("!fishingHands.fishingLine.CanReelOut()"); return; } speed = Mathf.Clamp(speed, -1f, 1f); if (!VRManager.Instance.IsVRReeling() && fishingHands.fishingLine.isLoose && speed > 0f) { speed *= 2f; } if (VRManager.Instance.IsVRReeling()) { currentRotationSpeed = speed; } else if (currentRotationSpeed == 0f) { currentRotationSpeed = speed; } else if ((bool)fishingHands.fishingPlayer.fish) { currentRotationSpeed = Mathf.MoveTowards(currentRotationSpeed, speed, 3f * Time.deltaTime); } else { currentRotationSpeed = Mathf.MoveTowards(currentRotationSpeed, speed, 3f * Time.deltaTime); } if (currentRotationSpeed == 0f) { Debug.Log("currentRotationSpeed: " + currentRotationSpeed + " speed: " + speed); } float num = 1f; float num2 = Mathf.Lerp(handleRotateSpeed.x * num, handleRotateSpeed.y, (!(currentRotationSpeed < 0f)) ? fishingHands.currentReelSpeed : (0f - currentRotationSpeed)); if (num2 == 0f) { Debug.Log("currentHandleSpeed: " + num2); } if (!VRManager.Instance.IsVRReeling()) { handle.Rotate(handleRotationVector * num2 * Time.deltaTime * currentRotationSpeed, Space.Self); } if ((bool)lineGuard) { lineGuard.Rotate(lineGuardRotationVector * currentRotationSpeed * (num2 * lineGuardRotateSpeed * Time.deltaTime), Space.Self); } UpdateHandleGrab(); if (leanSpoolParentMove != null) { leanSpoolParentMove.setTime(Mathf.Lerp(spoolParentMoveSpeed.x, spoolParentMoveSpeed.y * (1f + (1f - num)), 1f - ((!(currentRotationSpeed < 0f)) ? fishingHands.currentReelSpeed : (0f - currentRotationSpeed)))); if (leanSpoolParentMove.direction == 0f) { leanSpoolParentMove.resume(); } } if (!fakeRotation) { if (rotationAudioObject != null) { rotationAudioObject.pitch = Mathf.Lerp(0.9f * (1f + (1f - num)), 1.03f, fishingHands.currentReelSpeed); rotationAudioObject.volume = rotationAudioObject.audioItem.Volume * rotationAudioObject.subItem.Volume * rotationAudioObject.pitch * 0.5f; rotationAudioObject.Unpause(); } fishingHands.fishingLine.ReelLine(currentRotationSpeed); fishingHands.ShakeHandsWhileReeling(true); isReelingIn = true; } else { Debug.Log("fakeRotation"); } } public void PullFlyLine() { if (VRManager.Instance.IsVRReeling()) { return; } if (flyHandTween == null) { Debug.Log("PullFlyLine"); flyHandTween = LeanTween.moveLocal(fishingHands.leftArmReelParent.gameObject, fishingHands.flyArmFinishPos.localPosition, 0.5f).setLoopPingPong().setEaseInOutQuad() .setOnCompleteOnRepeat(true) .setOnCompleteOnStart(true) .setDestroyOnComplete(false); flyHandTween.setOnComplete((Action)delegate { if (!UtilitiesInput.isReelingIn && flyHandTween.direction > 0f) { flyHandTween.pause(); } if (flyHandTween.direction > 0f) { OpenFlyThumb(true); } else if (flyHandTween.direction < 0f) { OpenFlyThumb(false); AudioController.Play("PullFlyLine"); } RefreshFlyHandSpeed(); }); RefreshFlyHandSpeed(); } else if (flyHandTween.direction == 0f) { RefreshFlyHandSpeed(); flyHandTween.resume(); OpenFlyThumb(true); } } private void RefreshFlyHandSpeed() { if (flyHandTween != null) { flyHandTween.setTime(Mathf.Lerp(0.35f, 0.7f, 1f - fishingHands.currentUserReelSpeed)); } } private void OpenFlyThumb(bool open) { Vector3 localEulerAngles = fishingHands.flyArmThumb.localEulerAngles; if (open) { localEulerAngles.z = 10f; } else { localEulerAngles.z = 23f; } LeanTween.rotateLocal(fishingHands.flyArmThumb.gameObject, localEulerAngles, (!open) ? 0.1f : 0.1f); } public void RotateSpool(float speed) { if ((fishingHands.fishingLine.lineState == FishingLine.LineState.REEL_OUT || fishingHands.fishingLine.lineState == FishingLine.LineState.NEAR_COAST || fishingHands.fishingLine.lineState == FishingLine.LineState.FLY) && (!fishingHands.fishingPlayer.fish || !fishingHands.fishingPlayer.fish.isTryingBait)) { float num = Mathf.Lerp(spoolRotateSpeed.x, spoolRotateSpeed.y, speed); if ((bool)spool) { spool.Rotate(spoolRotationVector * num * Time.deltaTime, Space.Self); } spoolAudioObject.pitch = Mathf.Lerp(0.8f, 1.2f, speed); spoolAudioObject.volume = spoolAudioObject.audioItem.Volume * spoolAudioObject.subItem.Volume * Mathf.Lerp(0.6f, 1f, speed); spoolAudioObject.Unpause(); isReelingOut = true; } } public void UpdateLineAmount(float amount) { amount = Mathf.Lerp(lineEmptyScale, lineFullScale, amount); line.transform.localScale = new Vector3(amount, line.transform.localScale.y, amount); } public void OpenArch(bool open) { if (!(arch == null) && isArchOpened != open) { isArchOpened = open; AudioController.Play((!open) ? "ReelArchClose_01" : "ReelArchOpen_01", base.transform); LeanTween.rotateLocal(arch.gameObject, (!open) ? archClosedRotation : archOpenedRotation, 0.3f); } } public void ResetReel() { handle.localEulerAngles = handleStartRotation; vrHandle_PrevAngle = 0f; if ((bool)lineGuard) { lineGuard.localRotation = Quaternion.identity; } if ((bool)spoolParent) { } if ((bool)rotationAudioObject) { rotationAudioObject.Pause(); } if ((bool)spoolAudioObject) { spoolAudioObject.Pause(); } fishingHands.ShakeHandsWhileReeling(false); OpenArch(false); SetDragActive(true); UpdateLineAmount(1f); if ((bool)handleGrab) { handleGrab.localEulerAngles = handleGrabStartRot; } } public float GetDragForce() { return (!isDragActive) ? 0f : currentDrag; } public void SetDragActive(bool active) { isDragActive = active; if ((bool)arch) { OpenArch(!active); } } public void ToggleDragActive() { SetDragActive(!isDragActive); } [Button] public void CalculateDragWeight() { if (durability <= 1f) { maxDragWeight = Mathf.Lerp(0f, 55f, Mathf.Pow(durability, 1.5f)); } else { maxDragWeight = Mathf.Lerp(55f, 90f, Mathf.Pow(durability - 1f, 1.5f)); } maxDragWeight = (float)Mathf.RoundToInt(maxDragWeight * 10f) * 0.1f; } [Button] public void OpenArch() { OpenArch(true); } [Button] public void CloseArch() { OpenArch(false); } public void UpdateHandleGrab() { } }