Files
PrinceOfGlory/Assets/Scripts/Scene_02/S2_AnimalGrab.cs
2026-03-06 18:51:18 +08:00

83 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BigSpace.Logic;
using BigSpace.XRCore.Event;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.Interactables;
/// <summary>
/// S2 洹水风光 - 动物抓取交互
/// 挂在 animal_wolf / animal_cattle / animal_elephant 预制体根节点。
/// 同一 GameObject 需要挂 XRGrabInteractable。
///
/// 抓取流程:
/// selectEntered → 更新 AnimalCatchMgr 状态(手翻转手势仍由 AnimalCatchCtr.RightHnadCatchPalmUp 处理)
/// selectExited → 启用物理 + 派发 EventAnimalSend多人同步
/// </summary>
[RequireComponent(typeof(XRGrabInteractable))]
public class S2_AnimalGrab : MonoBehaviour
{
[Header("动物类型")]
[Tooltip("1=狐/狼 2=象 3=牛")]
[SerializeField] private int animalType = 1;
[Header("释放后挂到的父节点")]
[Tooltip("赋值为场景中 AnimalCatchMgr.parent_animals留空则保持原父节点")]
[SerializeField] private Transform releaseParent;
private XRGrabInteractable m_grab;
private Rigidbody m_rb;
private void Awake()
{
m_grab = GetComponent<XRGrabInteractable>();
m_rb = GetComponent<Rigidbody>();
}
private void OnEnable()
{
m_grab.selectEntered.AddListener(OnSelectEntered);
m_grab.selectExited.AddListener(OnSelectExited);
}
private void OnDisable()
{
m_grab.selectEntered.RemoveListener(OnSelectEntered);
m_grab.selectExited.RemoveListener(OnSelectExited);
}
// ── 抓取 ──────────────────────────────────────────────────────
private void OnSelectEntered(SelectEnterEventArgs args)
{
// 让 AnimalCatchCtr.RightHnadCatchPalmUp 仍能读到正确的动物类型
if (AnimalCatchMgr.Instance != null)
{
AnimalCatchMgr.Instance.rightHandIsCatchObj = true;
AnimalCatchMgr.Instance.rightAnimalType = animalType;
}
}
// ── 松手 ──────────────────────────────────────────────────────
private void OnSelectExited(SelectExitEventArgs args)
{
// 脱离手部父节点,挂到场景动物容器
if (releaseParent != null)
transform.SetParent(releaseParent, true);
// 启用物理XRI 在 Velocity/Kinematic 模式下会恢复 isKinematic这里强制打开重力
if (m_rb != null)
{
m_rb.isKinematic = false;
m_rb.useGravity = true;
}
// 重置管理器状态
if (AnimalCatchMgr.Instance != null)
AnimalCatchMgr.Instance.rightHandIsCatchObj = false;
// 多人同步:告知其他玩家在同位置生成该动物
GlobalEventMgr.Dispatch(GameEvent.EventAnimalSend, animalType);
}
}