133 lines
4.5 KiB
C#
133 lines
4.5 KiB
C#
using Core;
|
|
using UnityEngine;
|
|
using Script.Gameplay.Interface;
|
|
using Script.Gameplay.Input;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Script.Gameplay.Global;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Script.Gameplay.Player
|
|
{
|
|
public class PlayerEditController : MonoBehaviour
|
|
{
|
|
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
|
|
public bool IsEnableEditing = true; // 是否启用编辑功能
|
|
private bool isEditing = false; // 当前是否处于编辑状态
|
|
public event Action<GameObject> OnGazeEnterEditableComponent;
|
|
public event Action<GameObject> OnGazeExitEditableComponent;
|
|
public event Action<GameObject> OnBeginEditTarget;
|
|
public event Action<GameObject> OnEndEditTarget;
|
|
|
|
private GameObject previousTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
|
private GameObject currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
|
|
|
|
public GameObject CurrentTarget
|
|
{
|
|
get => currentTarget;
|
|
set
|
|
{
|
|
previousTarget = currentTarget;
|
|
currentTarget = value;
|
|
if (previousTarget != currentTarget)
|
|
{
|
|
if (currentTarget != null)
|
|
{
|
|
OnGazeEnterEditableComponent?.Invoke((currentTarget)?.gameObject);
|
|
}
|
|
if (previousTarget != null)
|
|
{
|
|
OnGazeExitEditableComponent?.Invoke((previousTarget)?.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private InputManager inputManager;
|
|
|
|
private TimePauseManager timePauseManager;
|
|
void Start()
|
|
{
|
|
inputManager = InputManager.Instance;
|
|
inputManager.Input.Player.Edit.performed += OnEditOnperformed;
|
|
timePauseManager = TimePauseManager.Instance;
|
|
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.");
|
|
ControllerLocator.Instance.Register(this);
|
|
}
|
|
|
|
private void OnEditOnperformed(InputAction.CallbackContext context)
|
|
{
|
|
EditTarget();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
DetectInteractable();
|
|
}
|
|
|
|
void DetectInteractable()
|
|
{
|
|
if (raycaster == null) return;
|
|
|
|
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
|
GameObject hitEditable = null;
|
|
if (lookAtObj != null)
|
|
{
|
|
if (lookAtObj.GetComponent<IEditableComponent>() != null)
|
|
{
|
|
hitEditable = lookAtObj;
|
|
}
|
|
}
|
|
CurrentTarget = hitEditable;
|
|
}
|
|
|
|
private void EditTarget()
|
|
{
|
|
if (isEditing)
|
|
{
|
|
isEditing = false;
|
|
OnEndEditTarget?.Invoke(CurrentTarget);
|
|
inputManager.SetCursorState(false, CursorLockMode.Locked);
|
|
inputManager.SetInputForLook(true);
|
|
inputManager.SetInputForMove(true);
|
|
if (timePauseManager != null)
|
|
{
|
|
timePauseManager.SetPaused(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (CurrentTarget == null) return;
|
|
if (!IsEnableEditing) return;
|
|
isEditing = true;
|
|
OnBeginEditTarget?.Invoke(CurrentTarget);
|
|
inputManager.SetCursorState(true, CursorLockMode.Confined);
|
|
inputManager.SetInputForLook(false);
|
|
inputManager.SetInputForMove(false);
|
|
if (timePauseManager != null)
|
|
{
|
|
timePauseManager.SetPaused(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (inputManager != null)
|
|
{
|
|
inputManager.Input.Player.Edit.performed -= OnEditOnperformed;
|
|
}
|
|
ControllerLocator.Instance.Unregister(this);
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
// 交由 FirstPersonRaycaster 绘制射线
|
|
}
|
|
}
|
|
} |