276 lines
6.9 KiB
C#
276 lines
6.9 KiB
C#
using System;
|
||
using DG.Tweening;
|
||
using DragonLi.Core;
|
||
using System.Collections;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 桃子对象
|
||
/// </summary>
|
||
public class Peach : MonoBehaviour
|
||
{
|
||
[Header("桃子设置")]
|
||
[SerializeField] private bool isGlowing = false; // 是否是发光桃子
|
||
|
||
[Header("动画设置")]
|
||
[SerializeField] private float flyDuration = 0.4f; // 飞向右手时长
|
||
[SerializeField] private float handStayDuration = 2f; // 桃子在手上停留时长
|
||
[SerializeField] private float disappearDuration = 1f; // 非发光桃子消失时长
|
||
[SerializeField] private float transformToCoinDuration = 1f; // 转化为硬币时长
|
||
[SerializeField] private float coinStayDuration = 2f; // 硬币在手上停留时长
|
||
|
||
[Header("特效")]
|
||
[SerializeField] private ParticleSystem glowEffect; // 发光特效
|
||
[SerializeField] private ParticleSystem disappearEffect; // 消失特效
|
||
[SerializeField] private GameObject coinPrefab; // 硬币预制体
|
||
|
||
private bool isCollected = false;
|
||
private RightHand rightHand;
|
||
|
||
private void Start()
|
||
{
|
||
// 初始化发光特效
|
||
if (glowEffect != null)
|
||
{
|
||
if (isGlowing)
|
||
{
|
||
glowEffect.Play();
|
||
}
|
||
else
|
||
{
|
||
glowEffect.Stop();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检测碰撞
|
||
/// </summary>
|
||
private void OnCollisionEnter(Collision collision)
|
||
{
|
||
|
||
}
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
if (isCollected) return;
|
||
// 检测是否是右手碰撞
|
||
RightHand hand = other.gameObject.GetComponent<RightHand>();
|
||
if (hand != null)
|
||
{
|
||
OnTouchedByHand(hand);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 被右手触碰
|
||
/// </summary>
|
||
public void OnTouchedByHand(RightHand hand)
|
||
{
|
||
if (isCollected) return;
|
||
|
||
isCollected = true;
|
||
rightHand = hand;
|
||
|
||
Debug.Log(isGlowing ? "收集发光桃子!" : "收集普通桃子!");
|
||
|
||
// 飞向右手
|
||
FlyToHand();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 飞向右手
|
||
/// </summary>
|
||
private void FlyToHand()
|
||
{
|
||
if (rightHand == null)
|
||
{
|
||
Debug.LogWarning("右手引用为空!");
|
||
return;
|
||
}
|
||
|
||
Vector3 handPosition = rightHand.transform.position;
|
||
|
||
// 飞向右手
|
||
transform.DOMove(handPosition, flyDuration)
|
||
.SetEase(Ease.OutBack)
|
||
.OnComplete(() => {
|
||
OnArrivedAtHand();
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 到达右手
|
||
/// </summary>
|
||
private void OnArrivedAtHand()
|
||
{
|
||
if (isGlowing)
|
||
{
|
||
// 发光桃子转化为硬币
|
||
StartCoroutine(TransformToCoinSequence());
|
||
}
|
||
else
|
||
{
|
||
// 非发光桃子消失
|
||
Disappear();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 转化为硬币序列(发光桃子)
|
||
/// </summary>
|
||
private IEnumerator TransformToCoinSequence()
|
||
{
|
||
// 停止发光特效
|
||
if (glowEffect != null)
|
||
{
|
||
glowEffect.Stop();
|
||
}
|
||
|
||
// 阶段1:桃子在手上停留(跟随右手移动)
|
||
yield return StartCoroutine(StayOnHand());
|
||
|
||
// 阶段2:桃子缩小
|
||
yield return StartCoroutine(ShrinkPeach());
|
||
|
||
// 阶段3:生成硬币
|
||
SpawnCoin();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 桃子在手上停留
|
||
/// </summary>
|
||
private IEnumerator StayOnHand()
|
||
{
|
||
float stayTime = 0f;
|
||
Debug.Log($"桃子在手上停留 {handStayDuration} 秒");
|
||
|
||
while (stayTime < handStayDuration)
|
||
{
|
||
// 持续跟随右手位置
|
||
if (rightHand != null)
|
||
{
|
||
transform.position = rightHand.transform.position;
|
||
}
|
||
stayTime += Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
|
||
Debug.Log("桃子停留结束,开始缩小");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 桃子缩小
|
||
/// </summary>
|
||
private IEnumerator ShrinkPeach()
|
||
{
|
||
// 播放消失特效
|
||
if (disappearEffect != null)
|
||
{
|
||
disappearEffect.Play();
|
||
}
|
||
|
||
// 桃子缩小
|
||
transform.DOScale(Vector3.zero, transformToCoinDuration);
|
||
yield return new WaitForSeconds(transformToCoinDuration);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成硬币
|
||
/// </summary>
|
||
private void SpawnCoin()
|
||
{
|
||
// 获取右手当前位置
|
||
Vector3 handPosition = rightHand.transform.position;
|
||
|
||
if (coinPrefab != null)
|
||
{
|
||
GameObject coinObj = Instantiate(coinPrefab, handPosition, Quaternion.identity);
|
||
Coin coinScript = coinObj.GetComponent<Coin>();
|
||
|
||
// 播放获得硬币音效
|
||
GameManager.Ins.PlaySound2DRPC("1.6");
|
||
|
||
Debug.Log($"在右手位置生成硬币: {handPosition}");
|
||
Debug.Log("发光桃子转化为硬币!");
|
||
|
||
// 销毁桃子
|
||
Destroy(gameObject);
|
||
|
||
// 硬币停留指定时长后飞向硬币台
|
||
StartCoroutine(TriggerCoinAfterDelay(coinScript, coinStayDuration));
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("硬币预制体未设置!");
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 延迟触发硬币收集
|
||
/// </summary>
|
||
private IEnumerator TriggerCoinAfterDelay(Coin coinScript, float delay)
|
||
{
|
||
yield return new WaitForSeconds(delay);
|
||
|
||
if (coinScript != null)
|
||
{
|
||
Debug.Log("硬币开始飞向硬币台");
|
||
coinScript.OnCollected();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 消失(非发光桃子)
|
||
/// </summary>
|
||
private void Disappear()
|
||
{
|
||
// 播放消失特效
|
||
if (disappearEffect != null)
|
||
{
|
||
disappearEffect.Play();
|
||
}
|
||
|
||
// 桃子缩小并消失
|
||
transform.DOScale(Vector3.zero, disappearDuration)
|
||
.OnComplete(() => {
|
||
Debug.Log("普通桃子消失");
|
||
Destroy(gameObject);
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置是否发光
|
||
/// </summary>
|
||
public void SetGlowing(bool glowing)
|
||
{
|
||
isGlowing = glowing;
|
||
|
||
if (glowEffect != null)
|
||
{
|
||
if (isGlowing)
|
||
{
|
||
glowEffect.Play();
|
||
}
|
||
else
|
||
{
|
||
glowEffect.Stop();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置硬币预制体
|
||
/// </summary>
|
||
public void SetCoinPrefab(GameObject prefab)
|
||
{
|
||
coinPrefab = prefab;
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
// 清理DOTween动画
|
||
transform.DOKill();
|
||
}
|
||
} |