Files
zeling_v2/Assets/_Game/Scripts/Progression/HPContainerPickup.cs

68 lines
2.5 KiB
C#
Raw Permalink 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.Core;
using BaseGames.Core.Events;
using BaseGames.Input;
namespace BaseGames.Progression
{
/// <summary>
/// HP 容器拾取物(架构 09_ProgressionModule §14
/// 永久 MaxHP +2 的可拾取物件,拾取后通过事件频道通知 SaveSystem。
/// 利用 SaveManager.Data.World.CollectedIds 防止重复拾取。
/// </summary>
public class HPContainerPickup : MonoBehaviour
{
[SerializeField] private string _collectibleId; // 存档用唯一 ID
[SerializeField] private InputReaderSO _inputReader;
[Header("Event Channels")]
[SerializeField] private StringEventChannelSO _onMaxHPContainerPickedUp; // → SaveSystem
[SerializeField] private IntEventChannelSO _onMaxHPChanged; // → HUDController
private bool _pickedUp;
private void Start()
{
// 若本局已拾取(存档),直接隐藏
if (!string.IsNullOrEmpty(_collectibleId) &&
ServiceLocator.GetOrDefault<ISaveService>()?.IsWorldCollected(_collectibleId) == true)
{
gameObject.SetActive(false);
_pickedUp = true;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (_pickedUp || !other.CompareTag("Player")) return;
if (!string.IsNullOrEmpty(_collectibleId) &&
ServiceLocator.GetOrDefault<ISaveService>()?.IsWorldCollected(_collectibleId) == true) return;
StartCoroutine(PickupSequence());
}
private IEnumerator PickupSequence()
{
_pickedUp = true;
// 禁用输入(切换到 UI 模式)
_inputReader?.EnableUIInput();
gameObject.SetActive(false);
// 等待特效播放Feel MMF_Player 可通过外部引用补充)
yield return new WaitForSeconds(0.8f);
// 零耦合:通过事件频道通知 SaveSystemSaveSystem 负责 MaxHP+2 及写入 CollectedIds
_onMaxHPContainerPickedUp?.Raise(_collectibleId);
// 通知 HUD 更新SaveSystem 写入后应重新查询 PlayerStats.MaxHP
_onMaxHPChanged?.Raise((ServiceLocator.GetOrDefault<ISaveService>()?.GetPlayerMaxHP() ?? 0) + 2);
yield return new WaitForSeconds(0.5f);
// 恢复输入
_inputReader?.EnableGameplayInput();
}
}
}