220 lines
5.5 KiB
C#
220 lines
5.5 KiB
C#
using DG.Tweening;
|
|
using DragonLi.Core;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 碎片对象
|
|
/// </summary>
|
|
public class Fragment : MonoBehaviour
|
|
{
|
|
[Header("收集设置")]
|
|
[SerializeField] private float flyDuration = 0.5f; // 飞向拼图区域时长
|
|
[SerializeField] private float snapDistance = 0.1f; // 自动拼入距离
|
|
[SerializeField] private float disappearDuration = 0.5f; // 消失时长
|
|
|
|
[Header("特效")]
|
|
[SerializeField] private ParticleSystem collectEffect; // 收集特效
|
|
[SerializeField] private ParticleSystem snapEffect; // 拼入特效
|
|
|
|
[Header("音效")]
|
|
[SerializeField] private string collectSound = "1.25"; // 收集音效
|
|
[SerializeField] private string snapSound = "1.26"; // 拼入音效
|
|
|
|
private bool isCollected = false;
|
|
private Vector3 targetPosition; // 目标位置(玉环拼图区域)
|
|
private JadeRing jadeRing; // 玉环拼图管理器
|
|
|
|
private void Start()
|
|
{
|
|
// 初始化特效
|
|
if (collectEffect != null)
|
|
{
|
|
collectEffect.Stop();
|
|
}
|
|
if (snapEffect != null)
|
|
{
|
|
snapEffect.Stop();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测碰撞
|
|
/// </summary>
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (isCollected) return;
|
|
|
|
// 检测是否是玩家碰撞
|
|
if (other.CompareTag("Player"))
|
|
{
|
|
OnCollectedByPlayer();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被玩家收集
|
|
/// </summary>
|
|
public void OnCollectedByPlayer()
|
|
{
|
|
if (isCollected) return;
|
|
|
|
isCollected = true;
|
|
|
|
Debug.Log("碎片被玩家收集!");
|
|
|
|
// 播放收集音效
|
|
if (!string.IsNullOrEmpty(collectSound))
|
|
{
|
|
GameManager.Ins.PlaySound2DRPC(collectSound);
|
|
}
|
|
|
|
// 播放收集特效
|
|
if (collectEffect != null)
|
|
{
|
|
collectEffect.Play();
|
|
}
|
|
|
|
// 飞向玉环拼图区域
|
|
StartCoroutine(FlyToJadeRing());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 飞向玉环拼图区域
|
|
/// </summary>
|
|
private IEnumerator FlyToJadeRing()
|
|
{
|
|
// 获取玉环拼图管理器
|
|
JadeRing jadeRingObj = GameObject.FindObjectOfType<JadeRing>();
|
|
if (jadeRingObj != null)
|
|
{
|
|
jadeRing = jadeRingObj.GetComponent<JadeRing>();
|
|
}
|
|
|
|
if (jadeRing != null)
|
|
{
|
|
// 获取目标位置
|
|
targetPosition = jadeRing.GetNextFragmentSlot();
|
|
|
|
if (targetPosition != Vector3.zero)
|
|
{
|
|
// 飞向目标位置
|
|
transform.DOMove(targetPosition, flyDuration)
|
|
.SetEase(Ease.OutBack)
|
|
.OnComplete(() => {
|
|
OnArrivedAtTarget();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("没有可用的碎片插槽");
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("未找到玉环拼图管理器");
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 到达目标位置
|
|
/// </summary>
|
|
private void OnArrivedAtTarget()
|
|
{
|
|
// 检查是否接近目标位置
|
|
if (Vector3.Distance(transform.position, targetPosition) <= snapDistance)
|
|
{
|
|
SnapToPosition();
|
|
}
|
|
else
|
|
{
|
|
// 如果距离较远,继续靠近
|
|
StartCoroutine(ApproachTarget());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 靠近目标位置
|
|
/// </summary>
|
|
private IEnumerator ApproachTarget()
|
|
{
|
|
while (Vector3.Distance(transform.position, targetPosition) > snapDistance)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, targetPosition, 0.1f);
|
|
yield return null;
|
|
}
|
|
|
|
SnapToPosition();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拼入位置
|
|
/// </summary>
|
|
private void SnapToPosition()
|
|
{
|
|
// 设置到目标位置
|
|
transform.position = targetPosition;
|
|
transform.rotation = Quaternion.identity;
|
|
|
|
// 播放拼入特效
|
|
if (snapEffect != null)
|
|
{
|
|
snapEffect.Play();
|
|
}
|
|
|
|
// 播放拼入音效
|
|
if (!string.IsNullOrEmpty(snapSound))
|
|
{
|
|
GameManager.Ins.PlaySound2DRPC(snapSound);
|
|
}
|
|
|
|
Debug.Log("碎片拼入位置!");
|
|
|
|
// 通知玉环拼图管理器
|
|
if (jadeRing != null)
|
|
{
|
|
jadeRing.OnFragmentSnapped(this);
|
|
}
|
|
|
|
// 销毁碎片
|
|
StartCoroutine(Disappear());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消失
|
|
/// </summary>
|
|
private IEnumerator Disappear()
|
|
{
|
|
// 逐渐缩小
|
|
transform.DOScale(Vector3.zero, disappearDuration);
|
|
yield return new WaitForSeconds(disappearDuration);
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置目标位置
|
|
/// </summary>
|
|
public void SetTargetPosition(Vector3 position)
|
|
{
|
|
targetPosition = position;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置玉环拼图管理器
|
|
/// </summary>
|
|
public void SetJadeRing(JadeRing ring)
|
|
{
|
|
jadeRing = ring;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// 清理DOTween动画
|
|
transform.DOKill();
|
|
}
|
|
} |