2025-10-18 08:55:38 +08:00
|
|
|
using Core;
|
2025-10-17 15:10:19 +08:00
|
|
|
using UnityEngine;
|
2025-10-20 09:02:15 +08:00
|
|
|
using Script.Gameplay.Interface;
|
2025-10-17 15:10:19 +08:00
|
|
|
using Script.Gameplay.Input;
|
|
|
|
|
2025-10-20 10:12:07 +08:00
|
|
|
namespace Script.Gameplay.Player
|
2025-10-17 15:10:19 +08:00
|
|
|
{
|
|
|
|
public class PlayerEditController:MonoBehaviour
|
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
|
2025-10-17 15:10:19 +08:00
|
|
|
|
|
|
|
private IEditable currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
|
|
|
|
private IEditable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
|
|
|
|
|
|
|
private InputManager inputManager;
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
inputManager = InputManager.Instance;
|
2025-10-18 17:14:09 +08:00
|
|
|
if (raycaster == null)
|
|
|
|
raycaster = GetComponent<FirstPersonRaycaster>() ?? GetComponentInChildren<FirstPersonRaycaster>();
|
|
|
|
if (raycaster == null)
|
|
|
|
raycaster = FindObjectOfType<FirstPersonRaycaster>();
|
|
|
|
if (raycaster == null)
|
|
|
|
Debug.LogWarning("FirstPersonRaycaster not found! Please assign or add it to the player.");
|
2025-10-18 08:55:38 +08:00
|
|
|
ControllerLocator.Instance.Register(this);
|
2025-10-18 17:14:09 +08:00
|
|
|
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
DetectInteractable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetectInteractable()
|
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
if (raycaster == null) return;
|
|
|
|
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
|
|
|
IEditable hitEditable = lookAtObj != null ? lookAtObj.GetComponent<IEditable>() : null;
|
2025-10-17 15:10:19 +08:00
|
|
|
|
2025-10-18 17:14:09 +08:00
|
|
|
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
|
|
|
if (hitEditable != previousGazedTarget)
|
2025-10-17 15:10:19 +08:00
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
if (previousGazedTarget != null)
|
2025-10-17 15:10:19 +08:00
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
previousGazedTarget.OnGazeExit(this);
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
|
|
|
|
2025-10-18 17:14:09 +08:00
|
|
|
if (hitEditable != null)
|
2025-10-17 15:10:19 +08:00
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
hitEditable.OnGazeEnter(this);
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
|
|
|
|
2025-10-18 17:14:09 +08:00
|
|
|
previousGazedTarget = hitEditable;
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
2025-10-18 17:14:09 +08:00
|
|
|
|
|
|
|
currentTarget = hitEditable;
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
|
|
|
|
2025-10-18 08:55:38 +08:00
|
|
|
public IEditable GetCurrentTarget()
|
|
|
|
{
|
|
|
|
return currentTarget;
|
|
|
|
}
|
|
|
|
|
2025-10-17 15:10:19 +08:00
|
|
|
void OnDrawGizmos()
|
|
|
|
{
|
2025-10-18 17:14:09 +08:00
|
|
|
// 交由 FirstPersonRaycaster 绘制射线
|
2025-10-17 15:10:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|