51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using BaseGames.Feedback;
|
|
using BaseGames.Player;
|
|
|
|
namespace BaseGames.World
|
|
{
|
|
/// <summary>
|
|
/// 能力解锁触发物(Prefab)。
|
|
/// 玩家进入触发区后播放解锁演出,然后调用 PlayerStats.UnlockAbility()。
|
|
/// </summary>
|
|
[RequireComponent(typeof(Collider2D))]
|
|
public class AbilityUnlock : MonoBehaviour
|
|
{
|
|
[Header("解锁配置")]
|
|
[SerializeField] private AbilityType _abilityToUnlock;
|
|
[SerializeField] private bool _destroyAfterUnlock = true;
|
|
|
|
[Header("演出配置")]
|
|
[SerializeField] private SceneFeedback _unlockFeedback;
|
|
[SerializeField] private float _cutsceneDuration = 1.5f;
|
|
|
|
[Header("Event Channel")]
|
|
[SerializeField] private AbilityTypeEventChannelSO _onAbilityUnlocked; // EVT_AbilityUnlocked
|
|
|
|
private bool _used;
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (_used || !other.CompareTag("Player")) return;
|
|
var stats = other.GetComponentInParent<PlayerStats>();
|
|
if (stats == null || stats.HasAbility(_abilityToUnlock)) return;
|
|
StartCoroutine(UnlockSequence(stats));
|
|
}
|
|
|
|
private IEnumerator UnlockSequence(PlayerStats stats)
|
|
{
|
|
_used = true;
|
|
|
|
_unlockFeedback?.Play();
|
|
yield return new WaitForSeconds(_cutsceneDuration);
|
|
|
|
stats.UnlockAbility(_abilityToUnlock);
|
|
_onAbilityUnlocked?.Raise(_abilityToUnlock);
|
|
|
|
if (_destroyAfterUnlock)
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|