using System.Collections; using UnityEngine; using BaseGames.Core; using BaseGames.Core.Events; using BaseGames.Input; namespace BaseGames.Progression { /// /// HP 容器拾取物(架构 09_ProgressionModule §14)。 /// 永久 MaxHP +2 的可拾取物件,拾取后通过事件频道通知 SaveSystem。 /// 利用 SaveManager.Data.World.CollectedIds 防止重复拾取。 /// 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()?.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()?.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); // 零耦合:通过事件频道通知 SaveSystem(SaveSystem 负责 MaxHP+2 及写入 CollectedIds) _onMaxHPContainerPickedUp?.Raise(_collectibleId); // 通知 HUD 更新(SaveSystem 写入后应重新查询 PlayerStats.MaxHP) _onMaxHPChanged?.Raise((ServiceLocator.GetOrDefault()?.GetPlayerMaxHP() ?? 0) + 2); yield return new WaitForSeconds(0.5f); // 恢复输入 _inputReader?.EnableGameplayInput(); } } }