Files
PrinceOfGlory/Assets/YOMOV Access/Scripts/QuadCatchCtr.cs
2026-03-06 18:51:18 +08:00

75 lines
2.3 KiB
C#

using System.Collections;
using UnityEngine;
using BigSpace.XRCore.Event;
using BigSpace.Logic;
public class QuadCatchCtr : MonoBehaviour
{
[Header("检测设置")]
[SerializeField] private float rayLength = 50f;
[SerializeField] private LayerMask detectionLayer;
[Tooltip("张开手丢失后保持就绪的宽限时间(秒),避免手势过渡时漏判")]
[SerializeField] private float readyGracePeriod = 0.3f;
private bool m_isReady = false;
private bool m_isCatching = false;
private Coroutine m_graceCoroutine;
// ── 张开手 StaticHandGesture ────────────────────────────────
public void OnOpenHandDetected()
{
// 取消正在倒计时的宽限期
if (m_graceCoroutine != null)
{
StopCoroutine(m_graceCoroutine);
m_graceCoroutine = null;
}
m_isReady = true;
}
public void OnOpenHandLost()
{
// 不立即清除,给握拳手势留过渡窗口
m_graceCoroutine = StartCoroutine(ClearReadyAfterDelay());
}
private IEnumerator ClearReadyAfterDelay()
{
yield return new WaitForSeconds(readyGracePeriod);
if (!m_isCatching)
m_isReady = false;
m_graceCoroutine = null;
}
// ── 握拳 StaticHandGesture ──────────────────────────────────
public void OnFistDetected()
{
if (!m_isReady || m_isCatching) return;
// 相机视线射线验证
Camera cam = Camera.main;
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
if (!Physics.Raycast(ray, out RaycastHit hit, rayLength, detectionLayer))
return;
string hitName = hit.collider.gameObject.name;
if (hitName.Length < 5 || hitName.Substring(0, 4) != "Quad") return;
if (!int.TryParse(hitName.Substring(4), out int fontIndex)) return;
m_isCatching = true;
GlobalEventMgr.Dispatch(GameEvent.EventRayQuadOk, true, 2, fontIndex);
}
public void OnFistLost()
{
if (!m_isCatching) return;
m_isCatching = false;
m_isReady = false;
GlobalEventMgr.Dispatch(GameEvent.EventHandRelease, 2);
}
}