using System; using System.Collections; using System.Collections.Generic; using Core; using UnityEngine; using UnityEngine.Events; using Script.Gameplay.Interface; using Script.Gameplay.Input; using UI; namespace Script.Gameplay.Player { public class PlayerDialogueController : MonoBehaviour { [SerializeField] private bool isEnablePlayerDialogue = true; // 是否启用玩家对话功能 [SerializeField] private FirstPersonRaycaster firstPersonRaycaster; // 第一人称射线检测器 private bool isReadingDialogue = false; public event Action OnPlayerBeginDialogue; public event Action OnPlayerEndDialogue; public event Action OnGazeEnterDialogue; public event Action OnGazeExitDialogue; <<<<<<< HEAD ======= private InputManager inputManager; >>>>>>> 2f1ad467aab3adc0a2a8675567ff130fa4202138 private Queue dialogueQueue = new Queue(); private PlayerDialogueViewer playerDialogueViewer; private IDialogue previousDialogue; private IDialogue currentDialogueTarget; public IDialogue CurrentDialogueTarget { get => currentDialogueTarget; set { previousDialogue = currentDialogueTarget; currentDialogueTarget = value; if (previousDialogue != currentDialogueTarget) { if (currentDialogueTarget != null) { OnGazeEnterDialogue?.Invoke((currentDialogueTarget as MonoBehaviour)?.gameObject); } if (previousDialogue != null) { OnGazeExitDialogue?.Invoke((previousDialogue as MonoBehaviour)?.gameObject); } } } } void Start() { if (firstPersonRaycaster == null) firstPersonRaycaster = GetComponent() ?? GetComponentInChildren(); if (firstPersonRaycaster == null) firstPersonRaycaster = FindObjectOfType(); if (firstPersonRaycaster == null) Debug.LogWarning("FirstPersonRaycaster not found! Please assign or add it to the player."); <<<<<<< HEAD var input = InputManager.Instance.Input; ======= inputManager = InputManager.Instance; var input = inputManager.Input; >>>>>>> 2f1ad467aab3adc0a2a8675567ff130fa4202138 input.Player.Read.performed += ctx => { if (!isEnablePlayerDialogue) return; if (CurrentDialogueTarget == null) return; if (isReadingDialogue) return; BeginDialogue(); }; input.Player.ShowNextDialogue.performed += ctx => { if (!isEnablePlayerDialogue) return; if (CurrentDialogueTarget == null) return; if (!isReadingDialogue) return; PassNextDialogue(); }; ControllerLocator.Instance.Register(this); } void Update() { DetectDialogue(); } private void DetectDialogue() { if (!isEnablePlayerDialogue || firstPersonRaycaster == null) return; GameObject lookAtObj = firstPersonRaycaster.CurrentLookAtObject; IDialogue hitDialogue = lookAtObj != null ? lookAtObj.GetComponent() : null; CurrentDialogueTarget = hitDialogue; } private void BeginDialogue() { if (CurrentDialogueTarget != null) { isReadingDialogue = true; OnPlayerBeginDialogue?.Invoke(); CurrentDialogueTarget.OnBeginDialogue?.Invoke(true); dialogueQueue = SplitDialogue(CurrentDialogueTarget.DialogueContent); <<<<<<< HEAD ======= inputManager.SetInputForLook(false); inputManager.SetInputForMove(false); >>>>>>> 2f1ad467aab3adc0a2a8675567ff130fa4202138 PassNextDialogue(); } } private void PassNextDialogue() { if (CurrentDialogueTarget != null) { if (dialogueQueue.Count == 0) { EndDialogue(); return; } string nextDialogue = dialogueQueue.Dequeue(); if (playerDialogueViewer != null) { playerDialogueViewer.ReceiveDialogue(nextDialogue); } } } private void EndDialogue() { if (CurrentDialogueTarget != null) { isReadingDialogue = false; OnPlayerEndDialogue?.Invoke(); CurrentDialogueTarget.OnEndDialogue?.Invoke(true); <<<<<<< HEAD ======= inputManager.SetInputForLook(true); inputManager.SetInputForMove(true); >>>>>>> 2f1ad467aab3adc0a2a8675567ff130fa4202138 if (playerDialogueViewer != null) { playerDialogueViewer.ClosePanel(); } // 可扩展:隐藏UI、恢复输入等 } } private Queue SplitDialogue(string content) { // 按换行分段 return new(content.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)); } public void RegisterDialogueViewer(PlayerDialogueViewer viewer) { playerDialogueViewer = viewer; } public void SetPlayerDialogueEnabled(bool isEnabled) { isEnablePlayerDialogue = isEnabled; if (!isEnabled) { CurrentDialogueTarget = null; } } } }