feat: 增强存档管理和成就系统,添加事件通知以支持凹槽成就的动态更新

This commit is contained in:
2026-05-20 16:03:10 +08:00
parent c50f8a6cf7
commit 8c66640a6d
3 changed files with 33 additions and 10 deletions

View File

@@ -51,7 +51,17 @@ namespace BaseGames.Core.Save
}
// ── ISaveable 注册 ────────────────────────────────────────────────────
public void Register(ISaveable s) => _saveables.Add(s);
/// <summary>
/// 注册 ISaveable。若当前已有存档数据_current != null
/// 立即对该组件补发 OnLoad使跨场景延迟注册的组件也能正确恢复状态。
/// </summary>
public void Register(ISaveable s)
{
if (!_saveables.Add(s)) return;
if (_current != null)
s.OnLoad(_current);
}
public void Unregister(ISaveable s) => _saveables.Remove(s);
// ── 游玩时间追踪 ──────────────────────────────────────────────────────

View File

@@ -26,16 +26,29 @@ namespace BaseGames.Equipment
[SerializeField] private CharmEventChannelSO _onCharmUnequipped;
[SerializeField] private VoidEventChannelSO _onEquipmentChanged;
[Tooltip("AchievementManager 解锁授予凹槽成就时发布;订阅后调用 IncreaseNotches(1)。")]
[SerializeField] private VoidEventChannelSO _onAchievementNotchGranted;
private readonly List<CharmSO> _equipped = new(4);
private readonly List<CharmSO> _collected = new(32);
private int _currentNotchCapacity;
private int _usedNotches;
private EquipmentContext _ctx;
private readonly CompositeDisposable _subs = new();
// ── 生命周期 ──────────────────────────────────────────────────────────
private void OnEnable() => ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Register(this);
private void OnDisable() => ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Unregister(this);
private void OnEnable()
{
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Register(this);
_onAchievementNotchGranted?.Subscribe(() => IncreaseNotches(1)).AddTo(_subs);
}
private void OnDisable()
{
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Unregister(this);
_subs.Clear();
}
private void Awake()
{

View File

@@ -29,9 +29,11 @@ namespace BaseGames.Progression
[Header("事件频道")]
[SerializeField] private AchievementEventChannelSO _onAchievementUnlocked;
[Tooltip("成就授予凹槽时发布EquipmentManager 订阅后调用 IncreaseNotches(1)。")]
[SerializeField] private VoidEventChannelSO _onNotchGranted;
// ── 运行时状态 ─────────────────────────────────────────────────────────
private readonly Dictionary<string, AchievementRuntimeState> _states = new();
private SaveData _saveRef;
private void Awake()
@@ -104,7 +106,6 @@ namespace BaseGames.Progression
/// </summary>
public void EvaluateAll(SaveData save)
{
_saveRef = save;
foreach (var state in _states.Values)
{
if (state.IsUnlocked) continue;
@@ -151,11 +152,10 @@ namespace BaseGames.Progression
ServiceLocator.Get<IPlatformService>()?.UnlockAchievement(state.Achievement.achievementId);
#endif
// 若成就授予凹槽,写入存档数值
if (state.Achievement.grantsNotch && _saveRef != null)
{
_saveRef.Equipment.MaxNotches++;
}
// 若成就授予凹槽,通过事件通知 EquipmentManager 更新运行时状态;
// 不直接写 SaveData避免与 EquipmentManager.OnSave 的数据竞争。
if (state.Achievement.grantsNotch)
_onNotchGranted?.Raise();
Debug.Log($"[Achievement] 解锁:{state.Achievement.displayName} ({state.Achievement.achievementId})");
}