docs(technical): 更新交互系统和收集系统技术方案
This commit is contained in:
+129
-7
@@ -1,8 +1,8 @@
|
||||
# 交互系统 技术设计文档(v0.1)
|
||||
|
||||
> 对应执行案: `docs/design/交互系统-执行案.md` (v0.1.2)
|
||||
> 关联系统: 角色控制系统、物品系统、收集系统
|
||||
> 日期: 2026-06-07
|
||||
> 关联系统: 角色控制系统、物品系统、收集系统、GameInstance 持久化层
|
||||
> 日期: 2026-06-07(2026-06-11 更新:加入多场景持久化架构)
|
||||
|
||||
---
|
||||
|
||||
@@ -14,6 +14,16 @@
|
||||
**预估工期**:5-7 天
|
||||
**新增/修改代码量**:约 600-800 行 C++ + 3 个 UMG 蓝图
|
||||
|
||||
### 跨场景持久化架构
|
||||
|
||||
> 本项目后续将支持多场景切换(如城郊 → 乡下)。UE5 的 `OpenLevel()` 会销毁整个旧 World,包括 Character 及其所有 UActorComponent。因此需要将**持久数据**与**运行时行为**分离:
|
||||
|
||||
- **持久数据层**:`UChildHoodGameInstance`(跨关卡存活),存储已收集物品 ID、门状态等
|
||||
- **行为层**:`UInteractionManager` / `UInteractableComponent`(随 Character/场景销毁),仅负责运行时交互逻辑
|
||||
- **恢复机制**:`AInteractableActor::BeginPlay()` 时查询 GameInstance,若物品已收集则隐藏并禁用交互
|
||||
|
||||
v0.1 为单场景原型,此架构以接口预留形式存在,不影响当前功能。详见 [第 12 节:跨场景持久化设计](#12-跨场景持久化设计)。
|
||||
|
||||
---
|
||||
|
||||
## 1. 需求功能点罗列
|
||||
@@ -130,6 +140,16 @@ classDiagram
|
||||
+GetInteractionManager() UInteractionManager*
|
||||
}
|
||||
|
||||
class UChildHoodGameInstance {
|
||||
-TSet~FName~ CollectedItemIDs
|
||||
-TMap~FName_bool~ DoorStates
|
||||
+IsItemCollected(FName ItemId) bool
|
||||
+MarkItemCollected(FName ItemId) void
|
||||
+IsDoorOpen(FName DoorId) bool
|
||||
+SetDoorOpen(FName DoorId, bool bIsOpen) void
|
||||
+ResetAll() void
|
||||
}
|
||||
|
||||
class AChildHoodPlayerController {
|
||||
+SetupInputComponent() void
|
||||
+OnInteractPressed() void
|
||||
@@ -186,6 +206,7 @@ classDiagram
|
||||
+ToggleDoor() void
|
||||
+IsDoorOpen() bool
|
||||
+GetWorldLocation() FVector
|
||||
+RestorePersistentState(UChildHoodGameInstance*) void
|
||||
#OnDetectionOverlapBegin(UPrimitiveComponent*, AActor*) void
|
||||
#OnDetectionOverlapEnd(UPrimitiveComponent*, AActor*) void
|
||||
}
|
||||
@@ -230,6 +251,8 @@ classDiagram
|
||||
AInteractableActor *-- UStaticMeshComponent : 组合
|
||||
UInteractableComponent *-- FInteractableData : 组合(内含)
|
||||
UInteractableComponent o-- USphereComponent : 聚合(引用子对象)
|
||||
UInteractableComponent ..> UChildHoodGameInstance : 恢复持久状态
|
||||
UInteractionManager ..> UChildHoodGameInstance : 写入消耗/门状态
|
||||
```
|
||||
|
||||
### 关系类型说明
|
||||
@@ -239,15 +262,17 @@ classDiagram
|
||||
| 组合 `*--` | 生命周期绑定 | Character 持有 InteractionManager;InteractableActor 持有 InteractableComponent |
|
||||
| 聚合 `o--` | 引用但非拥有 | InteractionManager 引用 PushCameraActor;InteractableComponent 引用 DetectionSphere(子对象) |
|
||||
| 关联 `-->` | 持久引用 | Character 关联 PlayerController;InteractionManager 引用 InteractableComponent 列表 |
|
||||
| 依赖 `..>` | 临时使用 | InteractableComponent 从 GameInstance 恢复状态;InteractionManager 向 GameInstance 写入消耗/门状态 |
|
||||
|
||||
### 新增类
|
||||
|
||||
| 类名 | 类型 | 职责 | 位置 |
|
||||
|------|------|------|------|
|
||||
| `UInteractionManager` | UActorComponent | 交互全流程状态机、检测选择、类型分发、摄像机/转向/UI 控制 | `Source/ChildHood/Interaction/` |
|
||||
| `UInteractableComponent` | UActorComponent | 单个交互物配置数据、触发区域、运行时状态 | `Source/ChildHood/Interaction/` |
|
||||
| `UInteractableComponent` | UActorComponent | 单个交互物配置数据、触发区域、运行时状态、跨场景持久状态恢复 | `Source/ChildHood/Interaction/` |
|
||||
| `AInteractableActor` | AActor | 通用交互物 Actor 基类,持有 Mesh + InteractableComponent | `Source/ChildHood/Interaction/` |
|
||||
| `APushCameraActor` | AActor | 推镜用临时摄像机,仅含 UCameraComponent | `Source/ChildHood/Interaction/` |
|
||||
| `UChildHoodGameInstance` | UGameInstance | 跨关卡持久数据持有者:已收集物品 ID、门状态等 | `Source/ChildHood/` |
|
||||
|
||||
### 修改类
|
||||
|
||||
@@ -599,6 +624,7 @@ void UInteractionManager::SwitchTarget(int32 Direction)
|
||||
- **结束回到 Free**:Exit 阶段完成后 `State = Free`
|
||||
- **检测标记恢复**:回到 Free 后检查 `NearbyInteractables.Num() > 0`,若有则立即更新提示 UI
|
||||
- **oneShot 消耗**:`Consume()` 设置 `bIsConsumed = true`,注销检测区域(`DetectionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision)`),从 NearbyInteractables 中移除
|
||||
- **跨场景持久化**:`Consume()` 同时调用 `GameInstance->MarkItemCollected(ItemId)` 写入持久层,确保场景切换后仍可恢复已消耗状态
|
||||
|
||||
---
|
||||
|
||||
@@ -794,6 +820,11 @@ void UInteractionManager::EnterAnimLockPhase()
|
||||
if (Data.Type == EInteractType::Operate && Data.OperateSubType == EOperateSubType::Door)
|
||||
{
|
||||
CurrentInteractable->ToggleDoor(); // bDoorIsOpen = !bDoorIsOpen
|
||||
// 持久化门状态到 GameInstance
|
||||
if (UChildHoodGameInstance* GI = GetWorld()->GetGameInstance<UChildHoodGameInstance>())
|
||||
{
|
||||
GI->SetDoorOpen(Data.ItemId, CurrentInteractable->IsDoorOpen());
|
||||
}
|
||||
// 通知 Actor 处理门 Mesh 旋转
|
||||
if (AInteractableActor* Owner = Cast<AInteractableActor>(CurrentInteractable->GetOwner()))
|
||||
Owner->OnInteractStarted(CurrentInteractable);
|
||||
@@ -917,7 +948,7 @@ void UInteractionManager::EnterAnimLockPhase()
|
||||
1. **Tick 开关优化**:State == Free 且 NearbyInteractables 为空时,关闭 TickComponent(`SetComponentTickEnabled(false)`)。注册首个交互物时开启。
|
||||
2. **推镜摄像机生命周期**:APushCameraActor 在 InteractionManager::BeginPlay 中 Spawn,随 Character 生命周期,不反复创建/销毁。
|
||||
3. **委托通知时机**:OnItemPickedUp / OnItemFound 在 ExecuteAnimEvent() 中广播,此时动画尚未结束。物品系统和收集系统需处理"动画未结束但已通知"的情况(v0.1 物品系统尚未实现,此通知仅作为架构预留)。
|
||||
4. **门状态持久化**:v0.1 门状态仅保存在内存中(bDoorIsOpen),关卡重新加载后重置。不需要持久化到存档。
|
||||
4. **门状态持久化**:门状态通过 `UChildHoodGameInstance` 持久化。每次开关门时写入 `GI->SetDoorOpen()`,`InteractableComponent::RestorePersistentState()` 在 BeginPlay 时读取恢复。v0.1 单场景下等效于首次加载时所有门为关闭状态。
|
||||
5. **NPC 对话简化**:v0.1 Talk 类型在架构上与 Examine 共用 Active 阶段逻辑,仅文本来源不同。后续版本加入对话树时需扩展 Active 阶段。
|
||||
|
||||
---
|
||||
@@ -988,10 +1019,11 @@ AInteractableActor (编辑器实例)
|
||||
Source/ChildHood/
|
||||
├── Interaction/
|
||||
│ ├── InteractionTypes.h # 枚举、结构体、委托定义
|
||||
│ ├── InteractionManager.h/.cpp # 交互状态机核心组件
|
||||
│ ├── InteractableComponent.h/.cpp # 可交互物组件(数据+检测)
|
||||
│ ├── InteractableActor.h/.cpp # 通用交互物 Actor 基类
|
||||
│ ├── InteractionManager.h/.cpp # 交互状态机核心组件(含 GameInstance 写入调用)
|
||||
│ ├── InteractableComponent.h/.cpp # 可交互物组件(数据+检测+持久状态恢复)
|
||||
│ ├── InteractableActor.h/.cpp # 通用交互物 Actor 基类(BeginPlay 调用恢复)
|
||||
│ └── PushCameraActor.h/.cpp # 推镜用临时摄像机
|
||||
├── ChildHoodGameInstance.h/.cpp # 新增:跨关卡持久数据持有者
|
||||
├── ChildHoodCharacter.h/.cpp # 修改:添加交互支持
|
||||
└── ChildHoodPlayerController.h/.cpp # 修改:添加交互输入绑定
|
||||
|
||||
@@ -1013,6 +1045,96 @@ Content/
|
||||
|
||||
---
|
||||
|
||||
## 12. 跨场景持久化设计
|
||||
|
||||
> UE5 的 `OpenLevel()`(非无缝跳转)会销毁整个旧 World,包括 Character 及其所有 UActorComponent。本节定义交互系统如何通过 `UChildHoodGameInstance` 实现跨场景数据持久化。
|
||||
|
||||
### 架构原则
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────────┐
|
||||
│ UChildHoodGameInstance │ ← 跨关卡存活
|
||||
│ ├── CollectedItemIDs: TSet<FName> │ 交互物消耗状态
|
||||
│ └── DoorStates: TMap<FName, bool> │ 门开关状态
|
||||
└──────────────────────────────────────────┘
|
||||
↕ 读取/写入
|
||||
┌──────────────────────────────────────────┐
|
||||
│ 场景中的 AInteractableActor │ ← 随场景加载/销毁
|
||||
│ └── BeginPlay → RestorePersistentState │ 查询并恢复状态
|
||||
└──────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### UChildHoodGameInstance 接口
|
||||
|
||||
```cpp
|
||||
UCLASS()
|
||||
class UChildHoodGameInstance : public UGameInstance
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
// === 物品消耗状态 ===
|
||||
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;
|
||||
TMap<FName, bool> DoorStates;
|
||||
};
|
||||
```
|
||||
|
||||
### 恢复流程
|
||||
|
||||
`AInteractableActor::BeginPlay()` 或 `UInteractableComponent::BeginPlay()` 中:
|
||||
|
||||
```cpp
|
||||
void UInteractableComponent::RestorePersistentState(UChildHoodGameInstance* GI)
|
||||
{
|
||||
if (!GI) return;
|
||||
|
||||
// oneShot 物品:已消耗 → 隐藏 + 禁用检测
|
||||
if (Data.bOneShot && GI->IsItemCollected(Data.ItemId))
|
||||
{
|
||||
bIsConsumed = true;
|
||||
if (USphereComponent* Sphere = DetectionSphere)
|
||||
Sphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
||||
if (AInteractableActor* Owner = Cast<AInteractableActor>(GetOwner()))
|
||||
Owner->SetActorHiddenInGame(true);
|
||||
}
|
||||
|
||||
// 门:恢复开关状态
|
||||
if (Data.Type == EInteractType::Operate && Data.OperateSubType == EOperateSubType::Door)
|
||||
{
|
||||
bDoorIsOpen = GI->IsDoorOpen(Data.ItemId);
|
||||
// 通知 Actor 应用视觉状态
|
||||
if (AInteractableActor* Owner = Cast<AInteractableActor>(GetOwner()))
|
||||
Owner->OnInteractStarted(this); // 复用已有的 Mesh 旋转逻辑
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 写入时机
|
||||
|
||||
| 事件 | 调用位置 | 写入方法 |
|
||||
|------|---------|---------|
|
||||
| oneShot 物品被消耗 | `UInteractableComponent::Consume()` | `GI->MarkItemCollected(ItemId)` |
|
||||
| 门开关状态变化 | `InteractionManager::ExecuteAnimEvent()` Door 分支 | `GI->SetDoorOpen(ItemId, bIsOpen)` |
|
||||
|
||||
### v0.1 行为
|
||||
|
||||
- v0.1 为单场景原型,不存在跨场景切换
|
||||
- `UChildHoodGameInstance` 在 v0.1 中以空数据初始化,所有 `IsItemCollected()` 返回 false
|
||||
- 后续多场景版本只需在 Project Settings 中将 GameInstance Class 设为 `UChildHoodGameInstance`,无需修改交互系统代码
|
||||
|
||||
---
|
||||
|
||||
## 工期预估与实现拆分
|
||||
|
||||
### 工作量估算
|
||||
|
||||
Reference in New Issue
Block a user