Files
StoryGame_Unity/Assets/02_Scripts/Communication/Dialog/DialogTouchZone.cs
2026-07-24 21:11:32 +09:00

47 lines
2.0 KiB
C#

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
// 캐릭터의 특정 부위(손·머리·어깨 등)에 붙이는 클릭 판정 영역.
// 대화 노드가 히든 분기로 무장한 동안 이 영역을 마우스로 누르면 그 분기가 열린다.
//
// 어느 부위인지는 Key로 구분된다:
// - 노드에 Key="head" 분기가 있으면 머리 존(Key="head")이 그 분기를 연다.
// - 어디에도 안 걸리면 노드의 catch-all 분기(Key 빈 항목)로 가고, 그것도 없으면 아무 일도 안 일어난다.
//
// 무장 중이 아니면 클릭 판정 자체를 하지 않는다 → 평소엔 존재하지 않는 것처럼 동작(=히든).
//
// 배치: Collider2D가 있는 오브젝트에 붙일 것 (보통 캐릭터의 자식).
// isTrigger 여부는 상관없다 — 충돌이 아니라 점 판정만 쓴다.
[RequireComponent(typeof(Collider2D))]
public class DialogTouchZone : MonoBehaviour
{
[Tooltip("이 부위의 키. 대화 노드의 히든 분기 Key와 일치할 때 그 분기를 연다 (예: head, hand, cheek)")]
[SerializeField] private string _key;
[Tooltip("실제로 분기가 열렸을 때 호출 (연출·사운드용). 발동하지 않은 클릭에는 불리지 않는다")]
[SerializeField] private UnityEvent _onTouched;
private Collider2D _collider;
private void Awake() => _collider = GetComponent<Collider2D>();
private void Update()
{
// 무장 안 된 순간에는 클릭을 아예 보지 않는다
if (!HiddenBranchResolver.Armed) return;
if (Mouse.current == null || !Mouse.current.leftButton.wasPressedThisFrame) return;
var cam = Camera.main;
if (cam == null || _collider == null) return;
Vector2 world = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
if (!_collider.OverlapPoint(world)) return;
// 이 노드에 맞는 분기가 있을 때만 true (무장 구간당 1회)
if (HiddenBranchResolver.Fire(_key))
_onTouched?.Invoke();
}
}