feat(Edit): 现在玩家按E可以直接对可编辑物体进行编辑

This commit is contained in:
2025-10-21 13:41:51 +08:00
parent d3eed0945c
commit f71ef8cae9
4 changed files with 70 additions and 100 deletions

View File

@@ -2,21 +2,30 @@ using Core;
using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Input;
using System;
using Script.Gameplay.Global;
namespace Script.Gameplay.Player
{
public class PlayerEditController:MonoBehaviour
public class PlayerEditController : MonoBehaviour
{
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
public bool IsEnableEditing = true; // 是否启用编辑功能
private bool isEditing = false; // 当前是否处于编辑状态
public event Action<IEditable> OnBeginEditTarget;
public event Action<IEditable> OnEndEditTarget;
private IEditable currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
private IEditable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
private InputManager inputManager;
private TimePauseManager timePauseManager;
void Start()
{
inputManager = InputManager.Instance;
inputManager.Input.Player.Edit.performed += context => EditTarget();
timePauseManager = TimePauseManager.Instance;
if (raycaster == null)
raycaster = GetComponent<FirstPersonRaycaster>() ?? GetComponentInChildren<FirstPersonRaycaster>();
if (raycaster == null)
@@ -24,7 +33,6 @@ namespace Script.Gameplay.Player
if (raycaster == null)
Debug.LogWarning("FirstPersonRaycaster not found! Please assign or add it to the player.");
ControllerLocator.Instance.Register(this);
}
void Update()
@@ -62,6 +70,36 @@ namespace Script.Gameplay.Player
return currentTarget;
}
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);
}
}
}
void OnDrawGizmos()
{
// 交由 FirstPersonRaycaster 绘制射线

View File

@@ -9,7 +9,6 @@ namespace Script.Gameplay.Player
public enum WatchMode
{
Normal,
Inside,
Outside
}
@@ -21,7 +20,7 @@ namespace Script.Gameplay.Player
private InputManager inputManager;
private TimePauseManager timePauseManager;
private PlayerInteractorController playerInteractorController;
private PlayerConnectController _playerConnectController;
private PlayerConnectController playerConnectController;
private WatchMode previousMode = WatchMode.Normal;
private WatchMode currentMode = WatchMode.Normal;
@@ -45,12 +44,12 @@ namespace Script.Gameplay.Player
inputManager = InputManager.Instance;
timePauseManager = TimePauseManager.Instance;
playerInteractorController = GetComponent<PlayerInteractorController>();
_playerConnectController = GetComponent<PlayerConnectController>();
playerConnectController = GetComponent<PlayerConnectController>();
var input = inputManager.Input;
input.Player.SwitchWatchMode.performed += ctx =>
{
var modeTemp = (WatchMode)(((int)CurrentWatchMode + 1) % 3);
var modeTemp = (WatchMode)(((int)CurrentWatchMode + 1) % Enum.GetNames(typeof(WatchMode)).Length);
CurrentWatchMode = modeTemp;
};
;
@@ -69,61 +68,31 @@ namespace Script.Gameplay.Player
inputManager.SetCursorState(false, CursorLockMode.Locked);
inputManager.SetInputForLook(true);
inputManager.SetInputForMove(true);
if (timePauseManager != null)
{
timePauseManager.SetPaused(false);
}
if (playerInteractorController != null)
{
playerInteractorController.SetPlayerInteractionEnabled(true);
}
if (_playerConnectController != null)
if (playerConnectController != null)
{
_playerConnectController.IsEnableConnecting = false;
playerConnectController.IsEnableConnecting = false;
}
break;
case WatchMode.Inside:
SetSeeLines(false);
inputManager.SetCursorState(true, CursorLockMode.Confined);
inputManager.SetInputForLook(false);
inputManager.SetInputForMove(false);
if (timePauseManager != null)
{
timePauseManager.SetPaused(true);
}
if (playerInteractorController != null)
{
playerInteractorController.SetPlayerInteractionEnabled(false);
}
if (_playerConnectController != null)
{
_playerConnectController.IsEnableConnecting = false;
}
break;
case WatchMode.Outside:
SetSeeLines(true);
inputManager.SetCursorState(false, CursorLockMode.Locked);
inputManager.SetInputForLook(true);
inputManager.SetInputForMove(true);
if (timePauseManager != null)
{
timePauseManager.SetPaused(false);
}
if (playerInteractorController != null)
{
playerInteractorController.SetPlayerInteractionEnabled(true);
}
if (_playerConnectController != null)
if (playerConnectController != null)
{
_playerConnectController.IsEnableConnecting = true;
playerConnectController.IsEnableConnecting = true;
}
break;