Files
AliceBall/Assets/_Alice/Scripts/Item/CarrotGem.cs
2025-07-25 14:30:55 +08:00

212 lines
6.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using DG.Tweening;
using EPOOutline;
using UnityEngine;
public class CarrotGem : MonoBehaviour
{
public GameObject carrot;
public GameObject realGem;
public GameObject fakeGem;
public GameObject punishGem;
public GameObject[] yeZi;
[NonSerialized]
public int Id;
private float _high;
private bool _isFly;
private bool _isTrigger;
private bool _isHand;
private bool _isMove;
private bool _isGet;
private Vector3 _startPos;
public SeekGemType gemType;
public void Init(int curid)
{
realGem.SetActive(false);
fakeGem.SetActive(false);
punishGem.SetActive(false);
_isHand = false;
fakeGem.GetComponent<Animator>().enabled = false;
punishGem.GetComponent<Animator>().enabled = false;
Id = curid;
_startPos=transform.position;
}
public void GetGem(int gemTypeIndex)
{
gemType = (SeekGemType)gemTypeIndex;
if(gemTypeIndex==3)
GameManager.Ins.taskManager.isGetGem = true;
StartCoroutine(ShowGem());
}
IEnumerator ShowGem()
{
carrot.SetActive(true);
MasterAudio.StopAllSoundsOfTransform(transform);
GameManager.Ins.PlaySound3DRPC("1.48",transform);
yield return new WaitForSeconds(0.5f);
carrot.SetActive(false);
switch (gemType)
{
case SeekGemType.Fake:
fakeGem.SetActive(true);
fakeGem.GetComponent<Animator>().enabled = true;
GameManager.Ins.PlaySound3DRPC("1.31",transform);
break;
case SeekGemType.Punish:
punishGem.SetActive(true);
punishGem.GetComponent<Animator>().enabled = true;
GameManager.Ins.PlaySound3DRPC("1.32",transform);
break;
case SeekGemType.Real:
realGem.SetActive(true);
GameManager.Ins.PlaySound3DRPC("1.30",transform);
realGem.transform.DORotate(new Vector3(0, 360, 0), 2f, RotateMode.FastBeyond360)
.SetEase(Ease.Linear)
.SetLoops(-1); // -1 表示无限循环
break;
}
_isGet = false;
if (gemType == SeekGemType.Real)
{
LaunchTowards(GameManager.Ins.playerRightHand.transform);
yield return !_isMove;
GameManager.Ins.PlaySound2DRPC("1.51");
_isGet = true;
yield return new WaitForSeconds(4f);
GameManager.Ins.taskManager.CompleteTask();
}
yield return new WaitForSeconds(1f);
Destroy(gameObject);
}
private void Update()
{
_isTrigger = MRInput.Ins.pressRightTrigger;
if(_isMove)
return;
if (_isTrigger && !_isFly && _isHand)
{
MRInput.Ins.VibrateRightController(0.3f,1);
if (GameManager.Ins.playerRightHand.triggerDis > 0 &&
_startPos.y + GameManager.Ins.playerRightHand.triggerDis * 100 > transform.position.y)
{
transform.position = Vector3.Lerp(transform.position,
new Vector3(_startPos.x, _startPos.y + GameManager.Ins.playerRightHand.triggerDis * 100,
_startPos.z), Time.deltaTime);
foreach (var item in yeZi)
{
item.transform.localScale=new Vector3(1,1+GameManager.Ins.playerRightHand.triggerDis * 100,1);
}
}
if ( transform.position.y > 0.2f)
{
_isFly = true;
transform.position = new Vector3(transform.position.x, 0.2f, transform.position.z);
foreach (var item in yeZi)
{
item.transform.localScale=new Vector3(1,1,1);
}
GameManager.Ins.taskManager.GetCarrotGem(gameObject);
}
}
if (_isGet)
{
transform.position=GameManager.Ins.playerRightHand.transform.position;
transform.rotation = GameManager.Ins.playerRightHand.transform.rotation;
}
}
[Tooltip("水平飞行速度 (m/s)")]
float flightSpeed = 10f;
[Tooltip("重力加速度 (正值),默认 9.81")]
public float gravity = 9.81f;
// 调用此方法开始飞向 movingHand
public void LaunchTowards(Transform movingHand)
{
StartCoroutine(FlyCoroutine(movingHand));
}
private IEnumerator FlyCoroutine(Transform hand)
{
_isMove = true;
Vector3 pos =transform.position; // 当前模拟位置
Vector3 startPos = pos;
Vector3 targetPos0 = hand.position;
// 1. 计算初始水平距离和方向
Vector3 toTarget0 = targetPos0 - startPos;
toTarget0.y = 0;
float distXZ0 = toTarget0.magnitude;
// 2. 飞行大致时间
float flightTime = distXZ0 / flightSpeed;
if (flightTime < 0.01f) yield break;
// 3. 初始垂直速度 vY
float dy = targetPos0.y - startPos.y;
float vY = dy / flightTime + 0.5f * gravity * flightTime;
// 4. 初始速度向量
Vector3 velocity = toTarget0.normalized * flightSpeed
+ Vector3.up * vY;
float elapsed = 0f;
while (elapsed < flightTime)
{
float dt = Time.deltaTime;
elapsed += dt;
// 重定向水平速度到「当前」手位置
Vector3 toHand = hand.position - pos;
toHand.y = 0;
if (toHand.sqrMagnitude > 0.001f)
{
Vector3 vxz = toHand.normalized * flightSpeed;
velocity.x = vxz.x;
velocity.z = vxz.z;
}
// 叠加重力
velocity.y -= gravity * dt;
// 更新位置
pos += velocity * dt;
transform.position = pos;
yield return null;
}
// 结束时钉在手上当前位置
transform.position = hand.position;
_isMove = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "RightHand")
{
_isHand = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "RightHand")
{
_isHand = false;
}
}
}