Files
zeling_v2/Assets/_Game/Scripts/World/AbilityUnlock.cs

51 lines
1.6 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 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);
}
}
}