61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using UnityEngine;
|
||
using BaseGames.Core.Events;
|
||
using BaseGames.Player;
|
||
|
||
namespace BaseGames.World
|
||
{
|
||
/// <summary>
|
||
/// 能力门禁(架构 09_ProgressionModule §2)。
|
||
/// 使用 PlayerStats.HasAbility() 检测玩家是否持有所需能力;
|
||
/// 订阅 AbilityTypeEventChannelSO 实时响应能力解锁事件。
|
||
/// </summary>
|
||
[RequireComponent(typeof(Collider2D))]
|
||
public class AbilityGate : MonoBehaviour
|
||
{
|
||
[SerializeField] protected AbilityType _requiredAbility;
|
||
[SerializeField] private GameObject _blockingObject; // 关卡障碍物 GO(禁/启用)
|
||
[SerializeField] private GameObject _hintUI; // 提示 UI(能力图标 + "???")
|
||
[SerializeField] private string _gateId; // 存档用(预留)
|
||
|
||
[Header("引用")]
|
||
[SerializeField] protected PlayerStats _playerStats; // Inspector 注入
|
||
[SerializeField] private AbilityTypeEventChannelSO _onAbilityUnlocked; // EVT_AbilityUnlocked
|
||
|
||
private readonly CompositeDisposable _subs = new();
|
||
|
||
/// <summary>子类可重写以实现额外通行条件检测。</summary>
|
||
protected virtual bool EvaluateAccess()
|
||
=> _playerStats != null && _playerStats.HasAbility(_requiredAbility);
|
||
|
||
private void Start()
|
||
{
|
||
ApplyState(EvaluateAccess());
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
_onAbilityUnlocked?.Subscribe(OnAbilityUnlocked).AddTo(_subs);
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
_subs.Clear();
|
||
}
|
||
|
||
private void OnAbilityUnlocked(AbilityType ability)
|
||
{
|
||
if (ability != _requiredAbility) return;
|
||
ApplyState(true);
|
||
}
|
||
|
||
/// <summary>外部调用:强制开启门禁(不检查条件)。</summary>
|
||
public void Open() => ApplyState(true);
|
||
|
||
private void ApplyState(bool unlocked)
|
||
{
|
||
if (_blockingObject != null) _blockingObject.SetActive(!unlocked);
|
||
if (_hintUI != null) _hintUI.SetActive(!unlocked);
|
||
}
|
||
}
|
||
}
|