using System.Collections;
using MoreMountains.Feedbacks;
using UnityEngine;
namespace BaseGames.World
{
///
/// 碎裂平台。玩家踩上后经历 Warning → Crumbling → Gone → (Recovering) 四态。
/// _isOneShot = true 时碎裂后永久消失;_respawnDelay > 0 则在延迟后恢复。
///
[RequireComponent(typeof(BoxCollider2D))]
public class CrumblePlatform : MonoBehaviour
{
[SerializeField] private float _warningDuration = 0.6f;
[SerializeField] private float _crumbleDuration = 0.3f;
[SerializeField] private float _respawnDelay = 3.0f; // 0 = 永久消失
[SerializeField] private bool _isOneShot = false;
[SerializeField] private MMF_Player _crumbleFeedback; // 预警震动 + 碎裂粒子 + 音效
[SerializeField] private BoxCollider2D _passengerSensor; // IsTrigger,检测玩家踩踏
private BoxCollider2D _col;
private SpriteRenderer _sr;
private bool _isCrumbling;
private void Awake()
{
_col = GetComponent();
_sr = GetComponent();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (_isCrumbling) return;
if (!other.CompareTag("Player")) return;
StartCoroutine(CrumbleSequence());
}
private IEnumerator CrumbleSequence()
{
_isCrumbling = true;
// 1. Warning(抖动)
_crumbleFeedback?.PlayFeedbacks();
yield return new WaitForSeconds(_warningDuration);
// 2. Crumbling 动画等待
yield return new WaitForSeconds(_crumbleDuration);
// 3. Gone:禁用碰撞体 + 隐藏 Sprite
_col.enabled = false;
_sr.enabled = false;
if (_passengerSensor != null)
_passengerSensor.enabled = false;
if (_isOneShot || _respawnDelay <= 0f)
yield break; // 永久消失
// 4. Respawn
yield return new WaitForSeconds(_respawnDelay);
_col.enabled = true;
_sr.enabled = true;
if (_passengerSensor != null)
_passengerSensor.enabled = true;
_isCrumbling = false;
}
}
}