Files
ProjectMD/Assets/Scripts/Player/PlayerInteractionController.cs
2026-01-15 22:46:40 +09:00

102 lines
3.6 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
public class PlayerInteractionController : MonoBehaviour
{
[Header("Detection Settings")]
[SerializeField] private float range = 3f;
[SerializeField] private LayerMask interactableLayer; // TunnelNode 감지용
[SerializeField] private LayerMask constructionLayer; // ConstructionSite 감지용
[Header("Build Settings")]
[SerializeField] private float buildSpeedMultiplier = 2f;
private PlayerInputActions _inputActions;
private bool _isHoldingInteract = false;
private TunnelTraveler _traveler;
void Awake()
{
_inputActions = new PlayerInputActions();
_traveler = GetComponent<TunnelTraveler>();
// [최신화] 탭/홀드 로직을 Input Action 이벤트에 직접 바인딩
// 1. 단일 상호작용 (터널 진입 등)
_inputActions.Player.Interact.performed += OnInteractPerformed;
// 2. 지속 상호작용 (건설 가속)
_inputActions.Player.Interact.started += ctx => _isHoldingInteract = true;
_inputActions.Player.Interact.canceled += ctx => _isHoldingInteract = false;
}
void OnEnable() => _inputActions.Enable();
void OnDisable() => _inputActions.Disable();
void Update()
{
// 홀드 중일 때만 건설 지원 로직 실행
if (_isHoldingInteract && !(_traveler != null && _traveler.IsTraveling))
{
PerformConstructionSupport();
}
}
private void OnInteractPerformed(InputAction.CallbackContext context)
{
// [로그 1] 버튼 입력 자체가 인식되는지 확인
Debug.Log("<color=white>1. E 키 입력 감지됨</color>");
if (_traveler != null && _traveler.IsTraveling)
{
Debug.Log("<color=gray>이동 중이라 상호작용 무시됨</color>");
return;
}
// [로그 2] 탐색 시작 위치와 범위 확인
Debug.Log($"<color=white>2. 탐색 시작 - 위치: {transform.position}, 범위: {range}, 레이어: {interactableLayer.value}</color>");
Collider[] colliders = Physics.OverlapSphere(transform.position, range, interactableLayer);
// [로그 3] 감지된 콜라이더 개수 확인
Debug.Log($"<color=cyan>3. 감지된 콜라이더 개수: {colliders.Length}</color>");
foreach (var col in colliders)
{
// [로그 4] 감지된 오브젝트의 이름 출력
Debug.Log($"<color=yellow>4. 감지된 오브젝트: {col.name}</color>");
IInteractable interactable = col.GetComponentInParent<IInteractable>();
if (interactable != null)
{
Debug.Log("<color=green>5. IInteractable 발견! Interact 실행</color>");
interactable.Interact(gameObject);
break;
}
}
}
private void PerformConstructionSupport()
{
// 주변의 건설 토대 감지
Collider[] targets = Physics.OverlapSphere(transform.position, range, constructionLayer);
foreach (var col in targets)
{
// 토대 스크립트 탐색 (부모 또는 본인)
ConstructionSite site = col.GetComponentInParent<ConstructionSite>();
if (site != null)
{
// 최신화된 AdvanceConstruction 호출
site.AdvanceConstruction(Time.deltaTime * buildSpeedMultiplier);
}
}
}
// 에디터 시각화
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, range);
}
}