feat: Round 52 narrative systems improvements

P1-A: QuestManager.OnLoad Enum.TryParse failure warning (dev builds)
P1-B: SaveData.QuestState ObjectiveCompleted dict; BuildObjectiveCompleted
       helper; OnSave/OnLoad wiring (DataVersion 2→3)
P2-A: Quest start/complete timestamps (_startedAtUtc/_completedAtUtc dicts;
       StartedAtUtc/CompletedAtUtc in SaveData; AcceptQuest/CompleteQuest/
       OnSave/OnLoad wiring)
P2-B: DialogueManager pending queue Queue→List + priority-eviction on full
       (lowest-priority item evicted when higher-priority request arrives)
P2-C: NpcSO.localizationTable field; NpcSOEditor uses npc.localizationTable
       in TryResolveNameKey, PingLocalizationFile, and button label
P3-A: QuestSO.failConditions[] multi-fail array; Obsolete failCondition;
       DispatchEvent updates fail check to any-of-array logic with fallback
P3-B: QuestObjectiveSO.prerequisiteObjectiveId; DispatchEvent gates objective
       event routing behind prerequisite completed check
P3-C: IQuestEventPayload interface + StringQuestPayload struct; QuestObjectiveSO
       typed TryHandleEvent(IQuestEventPayload) overload; DispatchEvent string
       overload delegates to typed IQuestEventPayload overload

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-05-25 00:47:44 +08:00
parent 48f018f4b8
commit 0b28cabba4
8 changed files with 189 additions and 25 deletions

View File

@@ -175,6 +175,34 @@ namespace BaseGames.Quest
event Action<string, QuestStateEnum, QuestStateEnum> OnQuestStateChanged;
}
// =========================================================================
// IQuestEventPayload ── 强类型事件载荷接口
// =========================================================================
/// <summary>
/// 任务事件载荷接口,用于 <see cref="QuestManager"/> 强类型 <c>DispatchEvent</c> 重载。
/// 实现此接口可在不修改 <see cref="QuestObjectiveSO.TryHandleEvent"/> 原有 string 签名的前提下,
/// 逐步迁移到类型安全的载荷传递。
/// </summary>
public interface IQuestEventPayload
{
/// <summary>以字符串形式返回载荷内容,兼容旧版 TryHandleEvent(string payload) 签名。</summary>
string AsString();
}
/// <summary>
/// 字符串载荷适配器。封装现有 string payload使其可作为 <see cref="IQuestEventPayload"/> 传递。
/// 旧版 <c>DispatchEvent(QuestEventType, string)</c> 内部通过此结构透明转换,不破坏现有调用方。
/// </summary>
public readonly struct StringQuestPayload : IQuestEventPayload
{
private readonly string _value;
public StringQuestPayload(string value) => _value = value;
/// <inheritdoc/>
public string AsString() => _value;
public static implicit operator StringQuestPayload(string s) => new StringQuestPayload(s);
}
#if UNITY_EDITOR || DEVELOPMENT_BUILD
/// <summary>
/// 任务调试接口(仅编辑器 / 开发构建可用)。

View File

@@ -126,6 +126,10 @@ namespace BaseGames.Quest
/// <summary>任务暂停时记录的 realtimeSinceStartup供 ResumeQuest 计算暂停持续时长并日志输出)。</summary>
private readonly Dictionary<string, float> _pauseTimestamps = new();
#endif
/// <summary>questId → 接取时间Unix 秒UTC。用于存档和统计分析。</summary>
private readonly Dictionary<string, long> _startedAtUtc = new();
/// <summary>questId → 完成时间Unix 秒UTC。0 表示未完成或旧存档未记录。</summary>
private readonly Dictionary<string, long> _completedAtUtc = new();
/// <summary>供需要直接订阅 SO 频道的系统使用(推荐使用 IQuestEventSource C# 事件)。</summary>
public StringEventChannelSO QuestStartedChannel => _onQuestStarted;
/// <summary>供需要直接订阅 SO 频道的系统使用(推荐使用 IQuestEventSource C# 事件)。</summary>
@@ -248,6 +252,7 @@ namespace BaseGames.Quest
if (!CanAccept(questId)) return;
var oldState = GetState(questId);
_questStates[questId] = QuestStateEnum.Active;
_startedAtUtc[questId] = System.DateTimeOffset.UtcNow.ToUnixTimeSeconds();
OnQuestStateChanged?.Invoke(questId, oldState, QuestStateEnum.Active);
Chan_QuestStarted?.Raise(questId);
OnQuestStarted?.Invoke(questId);
@@ -357,6 +362,7 @@ namespace BaseGames.Quest
// 先更新状态再发放奖励:确保 Apply 即使抛出异常,任务状态也已正确写入
_questStates[questId] = QuestStateEnum.Completed;
_completedAtUtc[questId] = System.DateTimeOffset.UtcNow.ToUnixTimeSeconds();
_notifiedReadyQuests.Remove(questId);
OnQuestStateChanged?.Invoke(questId, QuestStateEnum.Active, QuestStateEnum.Completed);
Chan_QuestCompleted?.Raise(questId);
@@ -661,9 +667,12 @@ namespace BaseGames.Quest
{
data.Quests.QuestStates[id] = new BaseGames.Core.Save.QuestState
{
DataVersion = 2,
Status = state.ToString(),
ObjectiveProgress = BuildObjectiveProgress(id),
DataVersion = 3,
Status = state.ToString(),
ObjectiveProgress = BuildObjectiveProgress(id),
ObjectiveCompleted = BuildObjectiveCompleted(id),
StartedAtUtc = _startedAtUtc.TryGetValue(id, out var sta) ? sta : 0L,
CompletedAtUtc = _completedAtUtc.TryGetValue(id, out var cta) ? cta : 0L,
};
}
// 将本地 NPC 好感度缓存回写到存档
@@ -680,6 +689,13 @@ namespace BaseGames.Quest
{
if (System.Enum.TryParse<QuestStateEnum>(saved.Status, out var parsedState))
_questStates[id] = parsedState;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
else
Debug.LogWarning(
$"[QuestManager] OnLoad任务 '{id}' 的 Status 值 '{saved.Status}' 无法解析为 QuestStateEnum" +
"已忽略(该任务将不出现在运行时 _questStates等同于 Unavailable。" +
"请检查存档文件是否损坏,或任务状态枚举定义是否发生变更。");
#endif
var quest = GetQuestSO(id);
if (quest?.objectives == null) continue;
@@ -700,6 +716,10 @@ namespace BaseGames.Quest
if (!_objectiveStates.TryGetValue(compositeKey, out var os))
os = _objectiveStates[compositeKey] = new QuestObjectiveState();
os.progressCount = count;
// DataVersion >= 3从存档恢复 completed 标志(防止 GetRequiredCount 变更后判定漂移)
if (saved.ObjectiveCompleted != null &&
saved.ObjectiveCompleted.TryGetValue(obj.objectiveId, out bool done))
os.completed = done;
}
}
else if (saved.ProgressCounts != null
@@ -718,6 +738,9 @@ namespace BaseGames.Quest
}
}
#pragma warning restore CS0618
// DataVersion >= 3恢复任务开始 / 完成时间戳0 = 旧存档未记录,跳过)
if (saved.StartedAtUtc != 0) _startedAtUtc[id] = saved.StartedAtUtc;
if (saved.CompletedAtUtc != 0) _completedAtUtc[id] = saved.CompletedAtUtc;
}
// 从存档恢复 NPC 好感度缓存(供 CanAccept 门槛检查使用)
_npcRelations = new Dictionary<string, int>(data.World.NpcRelations);
@@ -946,12 +969,43 @@ namespace BaseGames.Quest
return dict;
}
/// <summary>
/// 构建当前任务各目标的 completed 标志字典objectiveId → completed
/// 存入 <see cref="BaseGames.Core.Save.QuestState.ObjectiveCompleted"/>
/// 防止版本迭代中 <c>GetRequiredCount</c> 变更后,进度数值与实际完成状态脱钩。
/// </summary>
private Dictionary<string, bool> BuildObjectiveCompleted(string questId)
{
var quest = GetQuestSO(questId);
if (quest?.objectives == null) return new Dictionary<string, bool>(0);
var dict = new Dictionary<string, bool>(quest.objectives.Length, System.StringComparer.Ordinal);
foreach (var obj in quest.objectives)
{
if (obj == null || string.IsNullOrEmpty(obj.objectiveId)) continue;
_objectiveStates.TryGetValue(GetCompositeKey(questId, obj.objectiveId), out var os);
dict[obj.objectiveId] = os?.completed ?? false;
}
return dict;
}
// ── 事件路由 ─────────────────────────────────────────────────────────
// 统一分派入口:所有事件频道均路由到此方法,由各目标 SO 自行判断是否匹配。
// 新增目标类型只需在 QuestObjectiveSO 子类中 override TryHandleEvent
// 此处无需任何修改。
/// <summary>
/// 字符串载荷重载(向后兼容)。内部将 string 包装为 <see cref="StringQuestPayload"/> 后委托给强类型重载。
/// 所有外部调用方(订阅者、频道处理器)保持原有 string 签名,无需修改。
/// </summary>
private void DispatchEvent(QuestEventType eventType, string payload)
=> DispatchEvent(eventType, new StringQuestPayload(payload));
/// <summary>
/// 强类型载荷主实现。子类目标 SO 可 override <c>TryHandleEvent(QuestEventType, IQuestEventPayload, QuestObjectiveState)</c>
/// 以直接获取结构化载荷,避免字符串解析开销。
/// </summary>
private void DispatchEvent(QuestEventType eventType, IQuestEventPayload payload)
{
// ─ 第1次遍历更新目标进度 + 同步收集失败候选 ─────────────────────
// 将 CheckQuestFailConditions 内联到此处,避免对 _questStates 的独立第2次迭代。
@@ -973,6 +1027,13 @@ namespace BaseGames.Quest
foreach (var obj in quest.objectives)
{
if (obj == null || string.IsNullOrEmpty(obj.objectiveId)) continue;
// P3-B前置目标顺序依赖检查——前置目标未完成时跳过本目标的事件路由
if (!string.IsNullOrEmpty(obj.prerequisiteObjectiveId))
{
string prereqKey = GetCompositeKey(qid, obj.prerequisiteObjectiveId);
if (!_objectiveStates.TryGetValue(prereqKey, out var prereqOs) || !prereqOs.completed)
continue;
}
string compositeKey = GetCompositeKey(qid, obj.objectiveId);
if (!_objectiveStates.TryGetValue(compositeKey, out var os))
os = _objectiveStates[compositeKey] = new QuestObjectiveState();
@@ -1002,10 +1063,29 @@ namespace BaseGames.Quest
// 失败条件检查(同次遍历内完成,惰性分配 toFail 列表)
// Paused 任务在此处已被跳过(见上方 state != Active continue
// 设计意图:暂停期间目标冻结,失败条件也不判定,恢复后再继续检查。
if (quest.canFail && quest.failCondition != null && CheckObjective(qid, quest.failCondition))
if (quest.canFail)
{
toFail ??= new List<string>();
toFail.Add(qid);
// P3-A多失败条件支持——failConditions 数组中任意一个达成即失败
bool triggered = false;
if (quest.failConditions != null && quest.failConditions.Length > 0)
{
foreach (var fc in quest.failConditions)
{
if (fc != null && CheckObjective(qid, fc)) { triggered = true; break; }
}
}
else
{
// 向后兼容旧版单一 failCondition 字段Obsolete将在后续版本移除
#pragma warning disable CS0618
triggered = quest.failCondition != null && CheckObjective(qid, quest.failCondition);
#pragma warning restore CS0618
}
if (triggered)
{
toFail ??= new List<string>();
toFail.Add(qid);
}
}
}

View File

@@ -46,6 +46,10 @@ namespace BaseGames.Quest
public string displayTextKey;
[Tooltip("勾选后此目标为可选项:完成可获奖励,但不阻塞任务交接。")]
public bool IsOptional;
[Tooltip("前置目标 objectiveId留空 = 无依赖,目标与其他目标并行激活)。\n" +
"设置后,在指定目标的 completed 标志置为 true 之前,本目标不接收任何事件路由,即便事件满足匹配条件。\n" +
"用于实现顺序解锁的目标链(如先对话再交物品)。")]
public string prerequisiteObjectiveId;
/// <summary>
/// 目标所需完成数量。用于 UI 显示进度条分母(如 "3/5 击败")。
@@ -75,6 +79,14 @@ namespace BaseGames.Quest
public virtual bool TryHandleEvent(QuestEventType eventType, string payload, QuestObjectiveState state)
=> false;
/// <summary>
/// 强类型载荷重载(<see cref="IQuestEventPayload"/>)。
/// 默认实现委托给 string 版本(向后兼容),子类可 override 以直接消费结构化载荷,避免字符串解析。
/// QuestManager 优先调用此重载。
/// </summary>
public virtual bool TryHandleEvent(QuestEventType eventType, IQuestEventPayload payload, QuestObjectiveState state)
=> TryHandleEvent(eventType, payload?.AsString(), state);
/// <summary>
/// 在 DataHub / 编辑器工具中显示的类型徽章文字。
/// 子类应 override 返回简洁中文标签(如 "[对话]")。

View File

@@ -83,9 +83,14 @@ namespace BaseGames.Quest
public RewardSO reward;
[Header("失败条件(可选)")]
[Tooltip("勾选后failCondition 目标一旦完成,本任务立即失败并触发 EVT_QuestFailed 事件。")]
[Tooltip("勾选后failConditions 中任意一个目标完成,本任务立即失败并触发 EVT_QuestFailed 事件。")]
public bool canFail;
[Tooltip("失败判定目标。canFail=true 时有效;此目标达成即视为任务失败。")]
[Tooltip("失败判定目标列表(任意一个达成即失败)。canFail=true 时有效。\n" +
"支持多个失败条件如「BOSS 在限时内未被击败」OR「关键 NPC 死亡」)。")]
public QuestObjectiveSO[] failConditions;
[System.Obsolete("已废弃,请改用 failConditions数组支持多个失败条件。保留以兼容现有资产序列化。")]
[HideInInspector]
[Tooltip("(旧版单一失败条件,已被 failConditions 数组取代。保留以兼容现有资产。)")]
public QuestObjectiveSO failCondition;
[Header("接取/完成对话")]