Files
zeling_v2/Assets/_Game/Scripts/World/PhantomPlate.cs
2026-05-21 11:44:01 +08:00

76 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BaseGames.Core;
using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 幻象板Phantom Plate玩家可从下方穿过从上方站立的单向平台。
/// 使用 PlatformEffector2D + Collider2D 实现;支持下蹲跌落(按下 + 跳跃)。
///
/// 挂载要求:同一 GameObject 上需有 Collider2D 和 PlatformEffector2D。
/// </summary>
[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(PlatformEffector2D))]
public class PhantomPlate : MonoBehaviour, IDropThrough
{
[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<Collider2D>();
_effector = GetComponent<PlatformEffector2D>();
// 确保 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;
}
}
/// <summary>
/// 由玩家状态机调用:触发下蹲跌落,临时禁用碰撞器。
/// </summary>
public void TriggerDropThrough()
{
if (!_allowDropThrough || _isDisabled) return;
_col.enabled = false;
_isDisabled = true;
_reEnableTimer = _dropDisableDuration;
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
// 绘制蓝色轮廓以便与实体地面区分
if (TryGetComponent<Collider2D>(out var col))
{
Gizmos.color = new Color(0.3f, 0.6f, 1f, 0.4f);
Gizmos.DrawWireCube(col.bounds.center, col.bounds.size);
}
}
#endif
}
}