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

202 lines
5.1 KiB
C#

using System;
using RootMotion.FinalIK;
using UFS2.Gameplay;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class AnglerViewFish : Singleton<AnglerViewFish>
{
public Transform content;
public Vector3 waterPosition;
public FishEntity currentFish;
public CCDIK leftHandIK;
public CCDIK rightHandIK;
public Transform fishContainer;
public Transform cameraPoint;
public GameObject flashlight;
[SerializeField]
private float targetDistanceFromPlayer = 3f;
[SerializeField]
private float rotationX;
[SerializeField]
private float rotationY;
[SerializeField]
private float smoothTime = 3f;
[SerializeField]
private float cameraSensitivity = 3f;
[SerializeField]
private float scrollSensitivity = 9f;
[SerializeField]
private Transform FishOnLinePosition;
[SerializeField]
private Transform FishInArmsPosition;
[SerializeField]
private Transform FishOnGroundPosition;
[SerializeField]
private GameObject fishSheet;
private float currentDistanceFromPlayer = 1f;
private Transform boatAnglerTransform;
private Vector3 cameraPosStart;
private Vector3 cameraPosEnd;
private Vector3 smoothDampVelocity;
private FullBodyAvatar fba;
public static event Action OnAnglerViewStart;
public static event Action OnAnglerViewEnd;
private void Start()
{
fba = FullBodyAvatar.GetAvatar();
SetInitialPosition();
SetFishOnGroundPosition();
CheckAndHandleBoatPresence();
PlaceFish();
AnglerViewFish.OnAnglerViewStart?.Invoke();
}
private void OnDestroy()
{
if (currentFish.Weight >= 6f)
{
fba.PlayTPPAnimation(FullBodyAvatar.TPPAnimation.Idle);
fba.LeftArmIK();
fba.RightArmIK();
}
AnglerViewFish.OnAnglerViewEnd?.Invoke();
}
private void SetInitialPosition()
{
if (Physics.Raycast(new Ray(base.transform.position, Vector3.down), out var hitInfo))
{
base.transform.position = new Vector3(base.transform.position.x, base.transform.position.y - hitInfo.distance, base.transform.position.z);
}
}
private void SetFishOnGroundPosition()
{
Vector3 origin = FishOnGroundPosition.position + Vector3.up;
float radius = 0.25f;
if (Physics.SphereCast(origin, radius, Vector3.down, out var hitInfo, 1f))
{
FishOnGroundPosition.position = hitInfo.point + Vector3.up * 0.1f;
}
}
private void CheckAndHandleBoatPresence()
{
if (UnityEngine.Object.FindObjectOfType<Boat>()?.currentPlayerCharacter != null)
{
try
{
boatAnglerTransform = GameObject.Find("AnglerViewTransform").transform;
AttachToBoat(boatAnglerTransform);
}
catch (Exception ex)
{
Debug.LogError("Failed to find the boat's angler view transform: " + ex.Message);
boatAnglerTransform = null;
}
}
}
private void AttachToBoat(Transform boatTransform)
{
if ((bool)boatTransform)
{
base.transform.parent = boatTransform;
base.transform.position = new Vector3(boatTransform.position.x, boatTransform.position.y - 0.5f, boatTransform.position.z);
}
}
private void Update()
{
HandleFlashlight();
HandleCameraRotationAndZoom();
}
private void HandleFlashlight()
{
flashlight.SetActive(InputManager.isHeadFlashLight);
}
private void HandleCameraRotationAndZoom()
{
if (Time.timeScale != 0f)
{
float num = Input.GetAxis("Mouse ScrollWheel") * 10f;
if (num == 0f)
{
num = CrossPlatformInputManager.GetAxis("Mouse Y");
}
num *= -1f;
currentDistanceFromPlayer += num * scrollSensitivity;
currentDistanceFromPlayer = Mathf.Clamp(currentDistanceFromPlayer, 0.2f, 1f);
Vector3 target = Vector3.Lerp(cameraPosEnd, cameraPosStart, currentDistanceFromPlayer);
cameraPoint.localPosition = Vector3.SmoothDamp(cameraPoint.localPosition, target, ref smoothDampVelocity, smoothTime);
Vector3 forward = base.transform.TransformPoint(cameraPosEnd) - cameraPoint.position;
cameraPoint.rotation = Quaternion.LookRotation(forward, Vector3.up);
}
}
private void PlaceFish()
{
cameraPosStart = cameraPoint.transform.localPosition;
fishSheet.SetActive(value: false);
if (currentFish.Weight < 6f)
{
cameraPosEnd = FishOnLinePosition.localPosition;
}
else if (currentFish.Weight < 35f || (bool)boatAnglerTransform)
{
cameraPosEnd = FishInArmsPosition.localPosition;
currentFish.Physics.Rbody.isKinematic = true;
if ((bool)currentFish.Physics.Collider)
{
currentFish.Physics.Collider.enabled = false;
}
currentFish.Physics.Rbody.position = fba.FishInArms.position;
currentFish.Physics.Rbody.rotation = fba.FishInArms.rotation;
fba.PlayTPPAnimation(FullBodyAvatar.TPPAnimation.MiddleFishView);
fba.LeftArmIK(currentFish.transform.GetChild(0).Find("LeftHandHandler"));
fba.RightArmIK(currentFish.transform.GetChild(0).Find("RightHandHandler"));
}
else
{
cameraPosEnd = FishOnGroundPosition.localPosition;
currentFish.Physics.Rbody.isKinematic = true;
if ((bool)currentFish.Physics.Collider)
{
currentFish.Physics.Collider.enabled = false;
}
currentFish.Physics.Rbody.position = FishOnGroundPosition.position;
currentFish.Physics.Rbody.rotation = FishOnGroundPosition.rotation;
fba.PlayTPPAnimation(FullBodyAvatar.TPPAnimation.BigFishView);
}
}
}