docs(technical): 更新交互系统和收集系统技术方案

This commit is contained in:
2026-06-11 10:52:14 +08:00
parent c4ea51b7e6
commit 8ee21e5e80
2 changed files with 291 additions and 24 deletions
+162 -17
View File
@@ -2,8 +2,8 @@
> 任务ID: P-009
> 对应执行案: `docs/design/收集系统-执行案.md` (v0.1.1)
> 关联系统: 物品系统(上游单向接收)、交互系统(间接,通过物品系统委托)
> 日期: 2026-06-07
> 关联系统: 物品系统(上游单向接收)、交互系统(间接,通过物品系统委托)、GameInstance 持久化层
> 日期: 2026-06-07(2026-06-11 更新:加入多场景持久化架构)
---
@@ -15,6 +15,16 @@
**预估工期**:2-3 天
**新增/修改代码量**:约 250-350 行 C++ + 3 个 UMG 蓝图
### 跨场景持久化架构
> 收集进度是玩家核心持久数据。当前设计中 `UCollectionManager` 将「行为控制」(UI、通知、动画)和「持久数据」(CollectedIDs、Progress)混在同一组件,随 Character 销毁而丢失。需要通过数据层分离解决:
- **持久数据层**:收集状态(CollectedIDs、Progress、bIsComplete)存储在 `UChildHoodGameInstance` 中(跨关卡存活)
- **行为层**:`UCollectionManager` 仅负责运行时 UI 控制和委托监听,BeginPlay 时从 GameInstance 恢复状态
- **交互物恢复**:场景中的已收集物品由交互系统通过 `InteractableComponent::RestorePersistentState()` 处理(见交互系统技术方案第 12 节)
v0.1 为单场景原型,此架构以接口预留形式存在,不影响当前功能。详见 [第 12 节:跨场景持久化设计](#12-跨场景持久化设计)。
---
## 1. 需求功能点罗列
@@ -123,6 +133,17 @@ classDiagram
-CheckCompletion() void
-ShowCompletionPrompt() void
-UpdateProgressUI() void
-LoadStateFromGameInstance() void
-SaveStateToGameInstance() void
}
class UChildHoodGameInstance {
-TSet~FName~ CollectedItemIDs
-int32 CollectionProgress
-bool bCollectionComplete
+GetCollectionState(TSet_FName_&, int32&, bool&) void
+SetCollectionState(TSet_FName_&, int32, bool) void
+ResetAll() void
}
class UCollectionNotifWidget {
@@ -175,6 +196,7 @@ classDiagram
AChildHoodCharacter *-- UCollectionManager : 组合
UCollectionManager --> UInteractionManager : 关联(绑定委托)
UCollectionManager ..> UChildHoodGameInstance : 读取/写入持久状态
UCollectionManager o-- UCollectionNotifWidget : 聚合(管理生命周期)
UCollectionManager o-- UCollectionProgressWidget : 聚合(管理生命周期)
UCollectionManager ..> UCollectionCompleteWidget : 按需创建
@@ -188,7 +210,7 @@ classDiagram
| 组合 `*--` | 生命周期绑定 | Character 持有 CollectionManager;CollectionManager 内含 Config |
| 聚合 `o--` | 引用但非拥有 | CollectionManager 管理通知/进度 Widget 的创建和销毁 |
| 关联 `-->` | 持久引用 | CollectionManager 绑定 InteractionManager 的委托 |
| 依赖 `..>` | 临时使用 | CollectionManager 按需创建完成提示 Widget |
| 依赖 `..>` | 临时使用 | CollectionManager 从 GameInstance 读取/写入持久状态;按需创建完成提示 Widget |
### 新增类
@@ -489,6 +511,8 @@ void UCollectionManager::ProcessNewCollection(FName ItemId)
// 步骤 3: 记录
CollectedIDs.Add(ItemId);
Progress = CollectedIDs.Num(); // 用 Set 大小作为权威进度值,避免状态不一致
// 持久化到 GameInstance
SaveStateToGameInstance();
```
---
@@ -519,6 +543,7 @@ void UCollectionManager::ProcessNewCollection(FName ItemId)
if (Progress >= Config.TotalCount && !bIsComplete)
{
bIsComplete = true;
SaveStateToGameInstance(); // 持久化完成状态
CheckCompletion(); // 触发完成表现
}
}
@@ -637,10 +662,8 @@ void UCollectionManager::BeginPlay()
{
Super::BeginPlay();
// 初始化状态
Progress = 0;
CollectedIDs.Empty();
bIsComplete = false;
// 从 GameInstance 恢复持久状态(v0.1 单场景等效于空数据初始化)
LoadStateFromGameInstance();
// 创建进度 UI
if (ProgressWidgetClass)
@@ -652,7 +675,12 @@ void UCollectionManager::BeginPlay()
if (ProgressWidget)
{
ProgressWidget->AddToViewport(5);
ProgressWidget->UpdateProgress(0, Config.TotalCount);
ProgressWidget->UpdateProgress(Progress, Config.TotalCount);
// 已完成时恢复完成样式
if (bIsComplete)
{
ProgressWidget->SetCompleteStyle();
}
}
}
}
@@ -772,20 +800,34 @@ void UCollectionManager::ShowCompletionPrompt()
#### 重启状态处理
**实现**:v0.1 无持久化。CollectionManager::BeginPlay() 中完全重置:
**实现**:收集状态通过 `UChildHoodGameInstance` 跨场景持久化。CollectionManager::BeginPlay() 中从 GameInstance 恢复:
```cpp
void UCollectionManager::BeginPlay()
void UCollectionManager::LoadStateFromGameInstance()
{
Super::BeginPlay();
Progress = 0;
CollectedIDs.Empty();
bIsComplete = false;
// ... 创建 UI、绑定委托
if (UChildHoodGameInstance* GI = GetWorld()->GetGameInstance<UChildHoodGameInstance>())
{
GI->GetCollectionState(CollectedIDs, Progress, bIsComplete);
}
else
{
// 回退:无 GameInstance 时初始化为空(编辑器预览等场景)
Progress = 0;
CollectedIDs.Empty();
bIsComplete = false;
}
}
void UCollectionManager::SaveStateToGameInstance()
{
if (UChildHoodGameInstance* GI = GetWorld()->GetGameInstance<UChildHoodGameInstance>())
{
GI->SetCollectionState(CollectedIDs, Progress, bIsComplete);
}
}
```
重启后进度 UI 恢复显示 `📦 杂物 0/10`。
v0.1 单场景下:GameInstance 以空数据初始化,等效于 `📦 杂物 0/10`。场景内正常收集后状态写入 GameInstance,若通过 Seamless Travel 切换关卡则自动恢复。非无缝跳转(OpenLevel)时 GameInstance 仍存活,新场景的 CollectionManager 可读取之前的数据。
#### 收集通知与拾取通知时序
@@ -819,6 +861,7 @@ void UCollectionManager::BeginPlay()
### 注意事项
1. **委托绑定时机**:CollectionManager 在 BeginPlay() 中通过 `GetOwner()->GetComponentByClass<UInteractionManager>()` 获取引用并绑定。需确保 InteractionManager 已初始化。若为 nullptr 则在 Tick 中延迟绑定(最多重试 N 帧后报错)。
2. **持久化读写时机**:`SaveStateToGameInstance()` 在每次 ProcessNewCollection 记录后和完成判定后调用,保证 GameInstance 中的数据始终最新。`LoadStateFromGameInstance()` 仅在 BeginPlay 中调用一次。
2. **v0.1 无拾取通知 UI**:执行文档中拾取通知由物品系统负责,但 v0.1 物品系统仅为委托层(InteractionManager 上的委托)。收集通知的 0.3s 延迟仍按设计实现(为拾取通知预留时间窗口),即使拾取通知 UI 暂未实现。
3. **通知移除回调**:UCollectionNotifWidget 淡出完成后需通知 CollectionManager 从 ActiveNotifications 中移除自身引用。使用委托或弱引用回调实现,避免循环引用。
4. **FName 作为 ID**:收集系统使用 FName(ItemId)作为去重键,与 InteractionManager 委托广播的 ItemId 类型一致。
@@ -847,14 +890,33 @@ InteractionManager (ExecuteAnimEvent)
├─ 查品类 (Category == TrackedCategory?) → 不匹配则忽略
├─ 查重复 (CollectedIDs.Contains?) → 已存在则忽略
├─ 记录 (CollectedIDs.Add + Progress = Num())
├─ SaveStateToGameInstance() → UChildHoodGameInstance (持久化)
├─ UpdateProgressUI() → WBP_CollectionProgress (文本 + 弹性动画)
├─ SetTimer(NotifDelay) → ShowCollectionNotification() → WBP_CollectionNotification
└─ 完成判定 (Progress >= TotalCount)
→ bIsComplete = true
→ SaveStateToGameInstance() (持久化完成状态)
→ SetCompleteStyle() → WBP_CollectionProgress (金色)
→ SetTimer(TotalDelay) → ShowCompletionPrompt() → WBP_CollectionComplete
```
### 跨场景恢复流
```
场景切换 (OpenLevel / Seamless Travel)
→ 新场景加载
→ AChildHoodCharacter::BeginPlay()
→ UCollectionManager::BeginPlay()
→ LoadStateFromGameInstance()
→ UChildHoodGameInstance::GetCollectionState(OutIDs, OutProgress, OutComplete)
→ 恢复 CollectedIDs / Progress / bIsComplete
→ 创建进度 UI(显示已恢复的进度值)
→ 若 bIsComplete → 恢复完成样式
→ 场景中 AInteractableActor::BeginPlay()
→ UInteractableComponent::RestorePersistentState(GI)
→ GI->IsItemCollected(ItemId) → 已收集则隐藏 + 禁用
```
---
## 10. 可调参数汇总
@@ -886,10 +948,11 @@ InteractionManager (ExecuteAnimEvent)
Source/ChildHood/
├── Collection/
│ ├── CollectionTypes.h # FCollectionConfig 结构体定义
│ ├── CollectionManager.h/.cpp # 收集系统核心组件
│ ├── CollectionManager.h/.cpp # 收集系统核心组件(含 GameInstance 读写)
│ ├── CollectionNotifWidget.h/.cpp # 收集通知 UI 基类
│ ├── CollectionProgressWidget.h/.cpp # 收集进度 UI 基类
│ └── CollectionCompleteWidget.h/.cpp # 完成提示 UI 基类
├── ChildHoodGameInstance.h/.cpp # 新增:跨关卡持久数据持有者(收集状态在此持久化)
├── ChildHoodCharacter.h/.cpp # 修改:添加 CollectionManager 成员和访问器
Content/
@@ -928,3 +991,85 @@ Content/
> **关键路径**:6b-1 → 6b-2 → 6b-4 → 6b-5 → 6b-6。6b-3(进度 UI)可在 6b-1 完成后并行。总关键路径约 3 天。
> **前置依赖**:交互系统的 InteractionManager 组件需已完成(特别是 OnItemPickedUp/OnItemFound 委托),方可进行 6b-2 的委托绑定测试。
---
## 12. 跨场景持久化设计
> 收集进度是玩家的核心持久数据,必须在场景切换时保留。本节定义收集系统如何通过 `UChildHoodGameInstance` 实现跨场景数据持久化。
### 架构原则
```
┌──────────────────────────────────────────────────┐
│ UChildHoodGameInstance │ ← 跨关卡存活
│ ├── CollectedItemIDs: TSet<FName> │ 所有场景共享
│ ├── CollectionProgress: int32 │
│ └── bCollectionComplete: bool │
└──────────────────────────────────────────────────┘
↕ BeginPlay 加载 / 收集时回写
┌──────────────────────────────────────────────────┐
│ UCollectionManager (每个场景重建) │ ← 行为层
│ ├── 从 GameInstance 读取恢复进度 │ 不持有持久数据所有权
│ ├── 收集时写入 GameInstance │
│ └── 控制 UI / 通知 / 动画 │
└──────────────────────────────────────────────────┘
↕ 场景恢复
┌──────────────────────────────────────────────────┐
│ 场景中的 AInteractableActor │ ← 由交互系统负责恢复
│ └── RestorePersistentState() │ (见交互系统技术方案第 12 节)
└──────────────────────────────────────────────────┘
```
### UChildHoodGameInstance 收集相关接口
```cpp
UCLASS()
class UChildHoodGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
// === 收集状态 ===
void GetCollectionState(TSet<FName>& OutIDs, int32& OutProgress, bool& OutComplete) const;
void SetCollectionState(const TSet<FName>& IDs, int32 Progress, bool bComplete);
// === 物品消耗状态(交互系统也使用)===
bool IsItemCollected(FName ItemId) const;
void MarkItemCollected(FName ItemId);
// === 门状态(交互系统也使用)===
bool IsDoorOpen(FName DoorId) const;
void SetDoorOpen(FName DoorId, bool bIsOpen);
// === 重置 ===
void ResetAll();
private:
// 收集系统
TSet<FName> CollectedItemIDs;
int32 CollectionProgress = 0;
bool bCollectionComplete = false;
// 交互系统
TMap<FName, bool> DoorStates;
};
```
> **注意**:`CollectedItemIDs` 同时被 `IsItemCollected()`(交互物恢复)和 `GetCollectionState()`(收集进度恢复)使用,确保两系统数据一致性。
### 读写时机
| 时机 | 方法 | 说明 |
|------|------|------|
| CollectionManager::BeginPlay | `LoadStateFromGameInstance()` | 场景加载时恢复进度 |
| ProcessNewCollection 记录后 | `SaveStateToGameInstance()` | 每次新收集时同步写入 |
| 完成判定后 | `SaveStateToGameInstance()` | 完成状态立即持久化 |
| InteractableComponent::Consume | `GI->MarkItemCollected()` | 交互物消耗状态写入(由交互系统调用) |
### v0.1 行为
- v0.1 为单场景原型,不存在跨场景切换
- `UChildHoodGameInstance` 在 v0.1 中以空数据初始化,所有查询返回默认值
- `LoadStateFromGameInstance()` 等效于初始化为 0
- 后续多场景版本只需在 Project Settings 中将 GameInstance Class 设为 `UChildHoodGameInstance`,收集系统代码无需修改
- 后续版本可通过 USaveGame 将 GameInstance 数据序列化到磁盘,实现跨会话持久化