234 lines
5.9 KiB
C#
234 lines
5.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using DragonLi.Core;
|
|
|
|
/// <summary>
|
|
/// 半个玉环 - 玩家手柄触碰后可抓取,跟随手移动
|
|
/// </summary>
|
|
public class JadeRingHalf : MonoBehaviour
|
|
{
|
|
[Header("玉环设置")]
|
|
[SerializeField] private bool isLeftHalf = true; // 是否是左半玉环
|
|
|
|
[Header("抓取设置")]
|
|
[SerializeField] private float grabDistance = 0.3f; // 抓取距离
|
|
[SerializeField] private Vector3 grabOffset = Vector3.zero; // 抓取偏移
|
|
|
|
[Header("吸附设置")]
|
|
[SerializeField] private float snapDistance = 0.1f; // 吸附距离
|
|
[SerializeField] private Transform snapPoint; // 缺口标记点(用于检测缺口对准)
|
|
[SerializeField] private float correctMergeAngle = 180f; // 正确合成时的相对角度(左玉环和右玉环面对面)
|
|
|
|
[Header("特效")]
|
|
[SerializeField] private ParticleSystem glowEffect; // 发光特效
|
|
|
|
[Header("音效")]
|
|
[SerializeField] private string grabSound = "1.40"; // 抓取音效
|
|
[SerializeField] private string releaseSound = "1.41"; // 释放音效
|
|
|
|
private bool isGrabbed = false;
|
|
private Transform currentHand = null;
|
|
private JadeRingPuzzle puzzleManager = null;
|
|
private Vector3 originalPosition;
|
|
private Quaternion originalRotation;
|
|
|
|
private void Start()
|
|
{
|
|
// 记录原始位置
|
|
originalPosition = transform.position;
|
|
originalRotation = transform.rotation;
|
|
|
|
// 查找玉环拼图管理器
|
|
puzzleManager = FindObjectOfType<JadeRingPuzzle>();
|
|
|
|
// 播放发光特效
|
|
if (glowEffect != null)
|
|
{
|
|
glowEffect.Play();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 如果被抓取,跟随手移动
|
|
if (isGrabbed && currentHand != null)
|
|
{
|
|
transform.position = currentHand.position + grabOffset;
|
|
transform.rotation = currentHand.rotation;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测碰撞
|
|
/// </summary>
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (isGrabbed) return;
|
|
|
|
// 检测是否是右手碰撞
|
|
RightHand rightHand = other.gameObject.GetComponent<RightHand>();
|
|
|
|
if (rightHand != null)
|
|
{
|
|
Grab(rightHand.transform);
|
|
}
|
|
|
|
// 也检测标签
|
|
if (other.CompareTag("LeftHand") || other.CompareTag("RightHand"))
|
|
{
|
|
Grab(other.transform);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 抓取玉环
|
|
/// </summary>
|
|
public void Grab(Transform hand)
|
|
{
|
|
if (isGrabbed) return;
|
|
|
|
isGrabbed = true;
|
|
currentHand = hand;
|
|
|
|
// 播放抓取音效
|
|
if (!string.IsNullOrEmpty(grabSound))
|
|
{
|
|
GameManager.Ins.PlaySound2DRPC(grabSound);
|
|
}
|
|
|
|
Debug.Log($"{(isLeftHalf ? "左" : "右")}半玉环被抓取");
|
|
|
|
// 通知拼图管理器
|
|
if (puzzleManager != null)
|
|
{
|
|
puzzleManager.OnHalfRingGrabbed(this);
|
|
}
|
|
|
|
// 开始检测吸附
|
|
StartCoroutine(CheckSnap());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 释放玉环
|
|
/// </summary>
|
|
public void Release()
|
|
{
|
|
if (!isGrabbed) return;
|
|
|
|
isGrabbed = false;
|
|
currentHand = null;
|
|
|
|
// 播放释放音效
|
|
if (!string.IsNullOrEmpty(releaseSound))
|
|
{
|
|
GameManager.Ins.PlaySound2DRPC(releaseSound);
|
|
}
|
|
|
|
Debug.Log($"{(isLeftHalf ? "左" : "右")}半玉环被释放");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测吸附
|
|
/// </summary>
|
|
private IEnumerator CheckSnap()
|
|
{
|
|
while (isGrabbed)
|
|
{
|
|
if (puzzleManager != null)
|
|
{
|
|
// 检查另一个玉环是否靠近
|
|
if (puzzleManager.CheckCanSnap(this))
|
|
{
|
|
// 触发合成
|
|
puzzleManager.SnapRings();
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移动到目标位置
|
|
/// </summary>
|
|
public void MoveToPosition(Vector3 targetPos, Quaternion targetRot, float duration)
|
|
{
|
|
isGrabbed = false;
|
|
currentHand = null;
|
|
|
|
transform.DOMove(targetPos, duration).SetEase(Ease.OutQuad);
|
|
transform.DORotateQuaternion(targetRot, duration).SetEase(Ease.OutQuad);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置发光效果
|
|
/// </summary>
|
|
public void SetGlow(bool enabled)
|
|
{
|
|
if (glowEffect != null)
|
|
{
|
|
if (enabled)
|
|
{
|
|
glowEffect.Play();
|
|
}
|
|
else
|
|
{
|
|
glowEffect.Stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取是否是左半玉环
|
|
/// </summary>
|
|
public bool IsLeftHalf()
|
|
{
|
|
return isLeftHalf;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否被抓取
|
|
/// </summary>
|
|
public bool IsGrabbed()
|
|
{
|
|
return isGrabbed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取缺口标记点位置
|
|
/// </summary>
|
|
public Vector3 GetSnapPointPosition()
|
|
{
|
|
return snapPoint != null ? snapPoint.position : transform.position;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取正确合成时的相对角度
|
|
/// </summary>
|
|
public float GetCorrectMergeAngle()
|
|
{
|
|
return correctMergeAngle;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置玉环(用于测试)
|
|
/// </summary>
|
|
public void ResetRing()
|
|
{
|
|
isGrabbed = false;
|
|
currentHand = null;
|
|
|
|
transform.position = originalPosition;
|
|
transform.rotation = originalRotation;
|
|
|
|
Debug.Log($"{(isLeftHalf ? "左" : "右")}半玉环已重置");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// 清理DOTween动画
|
|
transform.DOKill();
|
|
}
|
|
} |