Files
MinFt/Client/Assets/Scripts/Aquarium/UI/EventFishHuntingAim.cs
2026-04-27 12:07:32 +08:00

147 lines
4.1 KiB
C#

using asap.core;
using cfg;
using DG.Tweening;
using GameCore;
using System;
using System.Collections.Generic;
using UnityEngine;
public class EventFishHuntingAim : MonoBehaviour
{
LayerMask layerMask;
GameObject target;
FishMovement fish;
[NonSerialized]
public EventFishHuntingPanel eventFishHuntingPanel;
public Animator animator;
public bool isFinish;
public float AimTime => crossbow.AimTime;
Vector3 screenPosition;
cfg.Crossbow crossbow;
float shakeTime = 0;
float rotate = 0;
Vector3 currentPos;
Vector3 newPos;
float time;
//CrossbowCameraMove crossbowCameraMove;
private void Awake()
{
crossbow = GContext.container.Resolve<Tables>().TbCrossbow.DataList[0];
layerMask = LayerMask.NameToLayer("fish");
}
private void OnEnable()
{
animator.SetInteger("AimIndex", crossbow.AimIndex);
Time.timeScale = crossbow.TimescaleRate;
transform.localPosition = Vector3.zero;
shakeTime = 0;
rotate = 1;
}
void Shake()
{
float timeRotate = shakeTime / crossbow.AimTime;
shakeTime += Time.deltaTime;
List<float> shakeSpeed = crossbow.ShakeSpeed;
float speed = shakeSpeed[0] - (shakeSpeed[0] - shakeSpeed[1]) * timeRotate;
if (rotate >= 1)
{
rotate = 0;
var ShakeRandRange = crossbow.ShakeRandRange;
float shakeRand1 = ShakeRandRange[0][0] - (ShakeRandRange[0][0] - ShakeRandRange[1][0]) * timeRotate;
float shakeRand2 = ShakeRandRange[0][1] - (ShakeRandRange[0][1] - ShakeRandRange[1][1]) * timeRotate;
//抖动相机
float x = UnityEngine.Random.Range(shakeRand1, shakeRand2);
float y = UnityEngine.Random.Range(shakeRand1, shakeRand2);
currentPos = transform.localPosition;
bool directionX = UnityEngine.Random.Range(0, 2) == 0;
bool directionY = UnityEngine.Random.Range(0, 2) == 0;
if (!directionX)
{
x = -x;
}
if (!directionY)
{
y = -y;
}
newPos = new Vector3(x, y, 0);
float dis = Vector3.Distance(currentPos, newPos);
time = dis / speed;
}
rotate += Time.deltaTime / time;
transform.localPosition = Vector3.Lerp(currentPos, newPos, rotate);
}
private void Update()
{
if (shakeTime < crossbow.AimTime)
{
Shake();
}
}
private void LateUpdate()
{
//if (isGuide)
//{
// OnGuide();
// return;
//}
if (isFinish || eventFishHuntingPanel == null)
{
return;
}
target = SelectTarget();
if (target != null)
{
fish = target.GetComponentInParent<FishMovement>();
}
else
{
fish = null;
}
eventFishHuntingPanel.ChangeTarget(fish);
if (Input.GetMouseButtonUp(0))
{
eventFishHuntingPanel.OnUpAim();
}
}
private GameObject SelectTarget()
{
screenPosition = UIManager.Instance.UICanvas.worldCamera.WorldToScreenPoint(transform.position);
var ray = Camera.main.ScreenPointToRay(screenPosition);
if (Physics.Raycast(ray, out var hit))
{
if (hit.collider.gameObject.layer == layerMask)
{
return hit.collider.gameObject;
}
}
return null;
}
public void Trigger(string value)
{
if (gameObject.activeSelf)
{
animator.SetTrigger(value);
}
}
private void OnDisable()
{
Time.timeScale = 1;
transform.DOKill();
}
//void OnGuide()
//{
// Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
// rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);
// crossbowCameraMove.transform.eulerAngles = new Vector3(rotation.eulerAngles.x, rotation.eulerAngles.y, 0);
//}
}