using UnityEngine;
namespace BaseGames.Enemies.Perception
{
///
/// 挂载在 PhysicsPerceptionSystem 子节点上的触发器代理组件。
/// 与 的 TriggerZone 槽位配合使用:
/// 当物理触发器产生 Enter / Exit 事件时,通知父系统的感知字典,
/// 同时触发 /
/// 委托。
///
/// 使用步骤:
/// 1. 在 PhysicsPerceptionSystem 的子 GameObject 上添加本组件。
/// 2. 确保同一 GameObject 或子节点上有 Collider2D,且 isTrigger = true。
/// 3. 填写 (与父系统中同名 TriggerZone 槽位对应)。
/// 4. 设置 (只有命中该层的碰撞体才会触发通知)。
/// 5. 由父系统 Awake() 自动赋值,无需手动操作。
///
[AddComponentMenu("BaseGames/Enemies/Perception Trigger Proxy")]
[RequireComponent(typeof(Collider2D))]
public sealed class PerceptionTriggerProxy : MonoBehaviour
{
[Tooltip("对应 PhysicsPerceptionSystem 中 TriggerZone 槽位的 slotName")]
public string slotName;
[Tooltip("目标检测层,只有命中此层的碰撞体才通知父系统")]
public LayerMask detectLayer;
/// 由父 PhysicsPerceptionSystem 在 Awake() 中自动注入,无需手动赋值。
internal PhysicsPerceptionSystem ParentSystem { get; set; }
// ── Unity 生命周期 ────────────────────────────────────────────────────
private void OnValidate()
{
var col = GetComponent();
if (col != null && !col.isTrigger)
Debug.LogWarning(
$"[PerceptionTriggerProxy] '{name}' 上的 Collider2D.isTrigger 未勾选," +
"TriggerZone 槽位将无法工作。", this);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (ParentSystem == null || string.IsNullOrEmpty(slotName)) return;
if (detectLayer != 0 && ((1 << other.gameObject.layer) & (int)detectLayer) == 0) return;
ParentSystem.OnTriggerZoneEnter(slotName, other.gameObject);
}
private void OnTriggerExit2D(Collider2D other)
{
if (ParentSystem == null || string.IsNullOrEmpty(slotName)) return;
if (detectLayer != 0 && ((1 << other.gameObject.layer) & (int)detectLayer) == 0) return;
ParentSystem.OnTriggerZoneExit(slotName, other.gameObject);
}
}
}