using UnityEngine;
namespace BaseGames.World
{
///
/// 幻象板(Phantom Plate):玩家可从下方穿过,从上方站立的单向平台。
/// 使用 PlatformEffector2D + Collider2D 实现;支持下蹲跌落(按下 + 跳跃)。
///
/// 挂载要求:同一 GameObject 上需有 Collider2D 和 PlatformEffector2D。
///
[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(PlatformEffector2D))]
public class PhantomPlate : MonoBehaviour
{
[Header("下蹲跌落")]
[Tooltip("按住下方向 + 跳跃时临时禁用碰撞器,允许玩家向下穿过平台")]
[SerializeField] private bool _allowDropThrough = true;
[Tooltip("禁用碰撞器的持续时间(秒)")]
[SerializeField] private float _dropDisableDuration = 0.3f;
private Collider2D _col;
private PlatformEffector2D _effector;
private float _reEnableTimer;
private bool _isDisabled;
private void Awake()
{
_col = GetComponent();
_effector = GetComponent();
// 确保 PlatformEffector2D 配置为单向平台
_effector.useOneWay = true;
_effector.surfaceArc = 170f;
_col.usedByEffector = true;
}
private void Update()
{
if (!_isDisabled) return;
_reEnableTimer -= Time.deltaTime;
if (_reEnableTimer <= 0f)
{
_col.enabled = true;
_isDisabled = false;
}
}
///
/// 由玩家状态机调用:触发下蹲跌落,临时禁用碰撞器。
///
public void TriggerDropThrough()
{
if (!_allowDropThrough || _isDisabled) return;
_col.enabled = false;
_isDisabled = true;
_reEnableTimer = _dropDisableDuration;
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
// 绘制蓝色轮廓以便与实体地面区分
if (TryGetComponent(out var col))
{
Gizmos.color = new Color(0.3f, 0.6f, 1f, 0.4f);
Gizmos.DrawWireCube(col.bounds.center, col.bounds.size);
}
}
#endif
}
}