feat: 优化存档管理,增强链状态获取逻辑,添加 ISaveable 接口支持
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
using BaseGames.Core.Save;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.EventChain
|
||||
@@ -12,7 +13,7 @@ namespace BaseGames.EventChain
|
||||
/// 订阅游戏各系统的 SO 事件频道,转发为中继 C# 事件供 ChainCondition.Register() 绑定。
|
||||
/// 每当收到新事件时,检查所有链的触发条件。
|
||||
/// </summary>
|
||||
public class EventChainManager : MonoBehaviour
|
||||
public class EventChainManager : MonoBehaviour, ISaveable
|
||||
{
|
||||
[Header("所有事件链")]
|
||||
[SerializeField] private EventChainSO[] _chains;
|
||||
@@ -49,11 +50,8 @@ namespace BaseGames.EventChain
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 从存档恢复已完成链
|
||||
var sm = ServiceLocator.GetOrDefault<ISaveService>();
|
||||
if (sm != null)
|
||||
foreach (var id in sm.GetCompletedChains())
|
||||
_completedChains.Add(id);
|
||||
// 不在此处读取存档数据——LoadAsync 尚未完成,_current 为 null。
|
||||
// 已完成链的恢复由 ISaveable.OnLoad 在 LoadAsync 完成后处理。
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -64,6 +62,8 @@ namespace BaseGames.EventChain
|
||||
_onRoomEntered?.Subscribe(id => { OnRoomEntered?.Invoke(id); EvaluateAll(); }).AddTo(_subs);
|
||||
_onDialogueCompleted?.Subscribe(id => { OnDialogueCompleted?.Invoke(id); EvaluateAll(); }).AddTo(_subs);
|
||||
|
||||
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Register(this);
|
||||
|
||||
// 向每个 Condition 注册中继事件
|
||||
// 先重置 SO 资产上的运行时状态,防止跨 PlayMode 会话或多场景加载时状态残留
|
||||
if (_chains == null) return;
|
||||
@@ -79,6 +79,7 @@ namespace BaseGames.EventChain
|
||||
private void OnDisable()
|
||||
{
|
||||
_subs.Clear();
|
||||
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Unregister(this);
|
||||
|
||||
if (_chains == null) return;
|
||||
foreach (var chain in _chains)
|
||||
@@ -87,6 +88,25 @@ namespace BaseGames.EventChain
|
||||
cond?.Unregister(this);
|
||||
}
|
||||
|
||||
// ── ISaveable ─────────────────────────────────────────────────────────
|
||||
/// <summary>
|
||||
/// SetChainCompleted 通过 ISaveService 直接写入 SaveData,OnSave 无需操作。
|
||||
/// </summary>
|
||||
public void OnSave(SaveData data) { }
|
||||
|
||||
/// <summary>
|
||||
/// LoadAsync 完成后由 GameSaveManager.Register 补发,恢复本会话前已完成的链。
|
||||
/// 仅恢复状态为 "Completed" 的条目;其他状态(如未来扩展的 "Failed")不影响重入检查。
|
||||
/// </summary>
|
||||
public void OnLoad(SaveData data)
|
||||
{
|
||||
_completedChains.Clear();
|
||||
if (data?.EventChains?.ChainStates == null) return;
|
||||
foreach (var kv in data.EventChains.ChainStates)
|
||||
if (kv.Value == "Completed")
|
||||
_completedChains.Add(kv.Key);
|
||||
}
|
||||
|
||||
// ── 评估逻辑 ──────────────────────────────────────────────────────
|
||||
/// <summary>收到新事件时立即评估所有链条件(无帧延迟)。</summary>
|
||||
private void EvaluateAll() => DoEvaluateAll();
|
||||
|
||||
Reference in New Issue
Block a user