446 lines
14 KiB
Markdown
446 lines
14 KiB
Markdown
# 任务奖励配置
|
||
|
||
<cite>
|
||
**本文档中引用的文件**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts)
|
||
- [VictoryComp.ts](file://assets/script/game/map/VictoryComp.ts)
|
||
- [MissionHeroComp.ts](file://assets/script/game/map/MissionHeroComp.ts)
|
||
- [GameEvent.ts](file://assets/script/game/common/config/GameEvent.ts)
|
||
- [Guide.ts](file://assets/script/game/common/config/Guide.ts)
|
||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts)
|
||
</cite>
|
||
|
||
## 目录
|
||
1. [简介](#简介)
|
||
2. [项目结构](#项目结构)
|
||
3. [核心组件](#核心组件)
|
||
4. [架构概览](#架构概览)
|
||
5. [详细组件分析](#详细组件分析)
|
||
6. [依赖关系分析](#依赖关系分析)
|
||
7. [性能考虑](#性能考虑)
|
||
8. [故障排除指南](#故障排除指南)
|
||
9. [结论](#结论)
|
||
|
||
## 简介
|
||
|
||
本文档全面解析了《英雄传说》游戏中基于Tasks.ts文件的任务奖励配置系统。该系统负责管理游戏中的各种任务类型,包括新手指引任务和每日任务,并实现了完整的奖励发放机制。系统采用模块化设计,支持任务配置的动态加载、运行时查询和奖励数据的实时更新。
|
||
|
||
## 项目结构
|
||
|
||
任务奖励配置系统在项目中的组织结构如下:
|
||
|
||
```mermaid
|
||
graph TB
|
||
subgraph "配置层"
|
||
Tasks[Tasks.ts<br/>任务配置常量]
|
||
GameEvent[GameEvent.ts<br/>游戏事件定义]
|
||
Guide[Guide.ts<br/>引导系统]
|
||
end
|
||
subgraph "数据管理层"
|
||
SingletonModule[SingletonModuleComp.ts<br/>单例模块管理]
|
||
Initialize[Initialize.ts<br/>初始化系统]
|
||
end
|
||
subgraph "业务逻辑层"
|
||
MissionComp[MissionComp.ts<br/>任务控制组件]
|
||
VictoryComp[VictoryComp.ts<br/>胜利界面组件]
|
||
MissionHeroComp[MissionHeroComp.ts<br/>英雄任务组件]
|
||
end
|
||
subgraph "UI展示层"
|
||
MInfoComp[MInfoComp.ts<br/>任务信息组件]
|
||
MapViewComp[MapViewComp.ts<br/>地图视图组件]
|
||
end
|
||
Tasks --> MissionComp
|
||
GameEvent --> MissionComp
|
||
SingletonModule --> MissionComp
|
||
MissionComp --> VictoryComp
|
||
MissionComp --> MInfoComp
|
||
```
|
||
|
||
**图表来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L13-L28)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L1-L30)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L50)
|
||
|
||
**章节来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L1-L28)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L1-L151)
|
||
|
||
## 核心组件
|
||
|
||
### 任务配置常量
|
||
|
||
Tasks.ts文件定义了游戏中的任务配置常量数组,包含以下关键字段:
|
||
|
||
| 字段名 | 类型 | 描述 | 示例值 |
|
||
|--------|------|------|--------|
|
||
| id | number | 任务唯一标识符 | 0, 1 |
|
||
| name | string | 任务名称 | "新手指引", "每日任务" |
|
||
| description | string | 任务描述 | "新手指引描述", "每日任务描述" |
|
||
| reward | number | 奖励数值 | 100 |
|
||
| type | number | 任务类型(可选) | 0 |
|
||
|
||
### 用户数据管理
|
||
|
||
SingletonModuleComp.ts提供了完整的用户数据管理系统,包含以下核心数据结构:
|
||
|
||
| 数据字段 | 类型 | 默认值 | 描述 |
|
||
|----------|------|--------|------|
|
||
| score | number | 0 | 游戏分数 |
|
||
| mission | number | 1 | 当前关卡 |
|
||
| diamond | number | 100 | 钻石数量 |
|
||
| meat | number | 0 | 肉类资源 |
|
||
| exp | number | 0 | 经验值 |
|
||
| task | number | 0 | 任务进度 |
|
||
|
||
**章节来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L13-L28)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L37-L50)
|
||
|
||
## 架构概览
|
||
|
||
任务奖励配置系统采用分层架构设计,确保了良好的可维护性和扩展性:
|
||
|
||
```mermaid
|
||
sequenceDiagram
|
||
participant Player as 玩家
|
||
participant MissionComp as 任务控制器
|
||
participant SingletonModule as 数据管理器
|
||
participant Tasks as 任务配置
|
||
participant VictoryComp as 胜利界面
|
||
Player->>MissionComp : 开始任务
|
||
MissionComp->>Tasks : 查询任务配置
|
||
Tasks-->>MissionComp : 返回任务信息
|
||
MissionComp->>MissionComp : 执行任务逻辑
|
||
MissionComp->>Player : 显示任务进度
|
||
Player->>MissionComp : 完成任务
|
||
MissionComp->>SingletonModule : 更新用户数据
|
||
SingletonModule->>SingletonModule : 存储到云端/本地
|
||
MissionComp->>VictoryComp : 显示奖励界面
|
||
VictoryComp->>Player : 展示奖励结果
|
||
```
|
||
|
||
**图表来源**
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L70-L90)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L81-L121)
|
||
- [VictoryComp.ts](file://assets/script/game/map/VictoryComp.ts#L30-L50)
|
||
|
||
## 详细组件分析
|
||
|
||
### 任务配置组件
|
||
|
||
#### Tasks.ts - 任务配置核心
|
||
|
||
任务配置组件负责定义所有可用的任务类型及其奖励规则:
|
||
|
||
```mermaid
|
||
classDiagram
|
||
class TaskConfig {
|
||
+number id
|
||
+string name
|
||
+string description
|
||
+number reward
|
||
+number type
|
||
+validateTask() boolean
|
||
+getRewardAmount() number
|
||
}
|
||
class Tasks {
|
||
+TaskConfig[] tasks
|
||
+findTaskById(id) TaskConfig
|
||
+getDailyRewards() number[]
|
||
+getTypeRewards(type) TaskConfig[]
|
||
}
|
||
Tasks --> TaskConfig : "包含多个"
|
||
```
|
||
|
||
**图表来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L13-L28)
|
||
|
||
#### 任务类型与奖励规则
|
||
|
||
系统支持多种任务类型,每种类型具有不同的奖励计算规则:
|
||
|
||
| 任务类型 | ID | 奖励计算 | 特殊规则 |
|
||
|----------|----|---------|---------|
|
||
| 新手指引 | 0 | 固定奖励 | 一次性奖励,不可重复 |
|
||
| 每日任务 | 1 | 固定奖励 | 每日刷新,可重复完成 |
|
||
|
||
**章节来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L13-L28)
|
||
|
||
### 任务执行组件
|
||
|
||
#### MissionComp.ts - 任务控制中心
|
||
|
||
任务控制组件管理整个任务生命周期,包括任务开始、执行和完成:
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
Start([任务开始]) --> LoadConfig["加载任务配置"]
|
||
LoadConfig --> InitData["初始化任务数据"]
|
||
InitData --> MonitorEvents["监控游戏事件"]
|
||
MonitorEvents --> EventCheck{"事件检查"}
|
||
EventCheck --> |怪物死亡| UpdateProgress["更新任务进度"]
|
||
EventCheck --> |英雄死亡| CheckGameOver{"检查游戏结束"}
|
||
EventCheck --> |任务完成| RewardCalculation["计算奖励"]
|
||
UpdateProgress --> MonitorEvents
|
||
CheckGameOver --> |失败| EndMission["结束任务"]
|
||
CheckGameOver --> |继续| MonitorEvents
|
||
RewardCalculation --> AwardRewards["发放奖励"]
|
||
AwardRewards --> ShowVictory["显示胜利界面"]
|
||
ShowVictory --> EndMission
|
||
EndMission --> ResetData["重置任务数据"]
|
||
ResetData --> End([任务结束])
|
||
```
|
||
|
||
**图表来源**
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L70-L150)
|
||
|
||
#### 奖励发放机制
|
||
|
||
奖励发放通过以下流程实现:
|
||
|
||
```mermaid
|
||
sequenceDiagram
|
||
participant MissionComp as 任务控制器
|
||
participant SingletonModule as 数据管理器
|
||
participant VictoryComp as 胜利界面
|
||
participant Player as 玩家
|
||
MissionComp->>MissionComp : 检查任务完成条件
|
||
MissionComp->>MissionComp : 计算奖励数值
|
||
MissionComp->>SingletonModule : 更新用户数据
|
||
SingletonModule->>SingletonModule : 验证数据完整性
|
||
SingletonModule->>SingletonModule : 同步到云端/本地
|
||
SingletonModule-->>MissionComp : 确认更新成功
|
||
MissionComp->>VictoryComp : 打开胜利界面
|
||
VictoryComp->>Player : 显示奖励详情
|
||
Player->>VictoryComp : 确认奖励领取
|
||
VictoryComp->>MissionComp : 完成奖励流程
|
||
```
|
||
|
||
**图表来源**
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L47-L50)
|
||
- [VictoryComp.ts](file://assets/script/game/map/VictoryComp.ts#L30-L50)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L120-L140)
|
||
|
||
**章节来源**
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L47-L50)
|
||
- [VictoryComp.ts](file://assets/script/game/map/VictoryComp.ts#L30-L50)
|
||
|
||
### 用户数据管理组件
|
||
|
||
#### SingletonModuleComp.ts - 数据管理中心
|
||
|
||
数据管理中心负责维护玩家的全局状态和持久化存储:
|
||
|
||
```mermaid
|
||
classDiagram
|
||
class SingletonModuleComp {
|
||
+Object data
|
||
+Object vmdata
|
||
+number fight_hero
|
||
+Array heros
|
||
+Array monsters
|
||
+updateGold(amount) boolean
|
||
+updateFihgtHero(heroId) boolean
|
||
+addHero(hero_uuid) boolean
|
||
+updateCloudData() boolean
|
||
+getCloudData() void
|
||
}
|
||
class UserData {
|
||
+number gold
|
||
+number exp
|
||
+number diamond
|
||
+number meat
|
||
+number task
|
||
+number score
|
||
+number mission
|
||
}
|
||
class MissionData {
|
||
+number mon_num
|
||
+number hero_num
|
||
+number fight_time
|
||
+boolean in_fight
|
||
+number level
|
||
+number coin
|
||
}
|
||
SingletonModuleComp --> UserData : "管理"
|
||
SingletonModuleComp --> MissionData : "管理"
|
||
```
|
||
|
||
**图表来源**
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L37-L50)
|
||
|
||
**章节来源**
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L37-L87)
|
||
|
||
### 初始化系统
|
||
|
||
#### Initialize.ts - 系统初始化
|
||
|
||
初始化系统负责在游戏启动时加载必要的配置和数据:
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
Start([游戏启动]) --> CheckPlatform{"检查平台"}
|
||
CheckPlatform --> |微信客户端| LoadCloud["加载云端数据"]
|
||
CheckPlatform --> |其他平台| LoadLocal["加载本地数据"]
|
||
LoadCloud --> LoginCloud["云端登录"]
|
||
LoginCloud --> ValidateData{"验证数据"}
|
||
ValidateData --> |成功| OverrideLocal["覆盖本地数据"]
|
||
ValidateData --> |失败| UseDefault["使用默认数据"]
|
||
LoadLocal --> UseDefault
|
||
OverrideLocal --> InitComplete["初始化完成"]
|
||
UseDefault --> InitComplete
|
||
InitComplete --> StartGame["启动游戏"]
|
||
```
|
||
|
||
**图表来源**
|
||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L80-L140)
|
||
|
||
**章节来源**
|
||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L80-L140)
|
||
|
||
## 依赖关系分析
|
||
|
||
任务奖励配置系统的依赖关系如下:
|
||
|
||
```mermaid
|
||
graph LR
|
||
subgraph "外部依赖"
|
||
OopsFramework[Oops Framework]
|
||
WeChatAPI[微信API]
|
||
end
|
||
subgraph "内部模块"
|
||
Tasks[Tasks配置]
|
||
GameEvent[游戏事件]
|
||
SingletonModule[单例模块]
|
||
MissionComp[任务组件]
|
||
VictoryComp[胜利组件]
|
||
end
|
||
Tasks --> MissionComp
|
||
GameEvent --> MissionComp
|
||
SingletonModule --> MissionComp
|
||
MissionComp --> VictoryComp
|
||
SingletonModule --> WeChatAPI
|
||
MissionComp --> OopsFramework
|
||
VictoryComp --> OopsFramework
|
||
```
|
||
|
||
**图表来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L1-L10)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L1-L15)
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L15)
|
||
|
||
**章节来源**
|
||
- [Tasks.ts](file://assets/script/game/common/config/Tasks.ts#L1-L10)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L1-L15)
|
||
|
||
## 性能考虑
|
||
|
||
### 任务配置加载优化
|
||
|
||
1. **静态配置缓存**:任务配置作为常量数组,在游戏启动时一次性加载到内存
|
||
2. **按需查询**:通过ID索引快速定位特定任务配置
|
||
3. **类型检查**:运行时验证任务配置的完整性
|
||
|
||
### 数据更新性能
|
||
|
||
1. **批量更新**:将多个数据变更合并为单次更新操作
|
||
2. **异步处理**:云端数据同步采用异步方式,避免阻塞主线程
|
||
3. **增量更新**:只更新发生变化的数据项
|
||
|
||
### 内存管理
|
||
|
||
1. **对象池**:复用任务相关对象,减少垃圾回收压力
|
||
2. **弱引用**:对临时对象使用弱引用,便于及时回收
|
||
3. **定时清理**:定期清理过期的任务数据
|
||
|
||
## 故障排除指南
|
||
|
||
### 常见问题及解决方案
|
||
|
||
#### 任务奖励未发放
|
||
|
||
**问题症状**:任务完成后未获得预期奖励
|
||
|
||
**排查步骤**:
|
||
1. 检查任务配置中的reward字段是否正确设置
|
||
2. 验证用户数据更新逻辑是否正常执行
|
||
3. 确认云端同步功能是否工作正常
|
||
|
||
**解决方案**:
|
||
```typescript
|
||
// 检查任务配置
|
||
if (!task.reward || task.reward <= 0) {
|
||
console.error("任务奖励配置错误:", task);
|
||
return false;
|
||
}
|
||
|
||
// 验证数据更新
|
||
if (!smc.updateGold(task.reward)) {
|
||
console.error("奖励发放失败");
|
||
return false;
|
||
}
|
||
```
|
||
|
||
#### 任务进度不更新
|
||
|
||
**问题症状**:任务进度显示异常或不更新
|
||
|
||
**排查步骤**:
|
||
1. 检查GameEvent监听器是否正确注册
|
||
2. 验证任务完成条件判断逻辑
|
||
3. 确认UI组件的数据绑定是否正确
|
||
|
||
#### 云端数据同步失败
|
||
|
||
**问题症状**:本地数据与云端数据不一致
|
||
|
||
**排查步骤**:
|
||
1. 检查网络连接状态
|
||
2. 验证微信API初始化是否成功
|
||
3. 查看错误日志获取详细信息
|
||
|
||
**解决方案**:
|
||
```typescript
|
||
// 云端数据同步检查
|
||
async function syncDataWithCloud() {
|
||
try {
|
||
const result = await smc.updateCloudData();
|
||
if (!result) {
|
||
throw new Error("云端数据同步失败");
|
||
}
|
||
console.log("数据同步成功");
|
||
} catch (error) {
|
||
console.error("数据同步异常:", error);
|
||
// 回滚到本地数据
|
||
smc.getCloudData();
|
||
}
|
||
}
|
||
```
|
||
|
||
**章节来源**
|
||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L81-L121)
|
||
- [MissionComp.ts](file://assets/script/game/map/MissionComp.ts#L47-L50)
|
||
|
||
## 结论
|
||
|
||
《英雄传说》任务奖励配置系统通过模块化的设计和清晰的职责分离,实现了高效、可靠的任务管理功能。系统支持多种任务类型,具备完善的奖励发放机制,并提供了灵活的扩展接口。
|
||
|
||
### 系统优势
|
||
|
||
1. **配置灵活**:通过Tasks.ts文件集中管理任务配置
|
||
2. **数据安全**:支持云端和本地双重存储机制
|
||
3. **性能优化**:采用缓存和异步处理提升响应速度
|
||
4. **易于维护**:清晰的代码结构和完善的错误处理
|
||
|
||
### 扩展建议
|
||
|
||
1. **任务类型扩展**:增加更多任务类型如成就任务、挑战任务
|
||
2. **奖励多样化**:支持多种奖励类型如道具、技能等
|
||
3. **数据分析**:添加任务完成率统计和玩家行为分析
|
||
4. **社交功能**:支持任务分享和好友竞争功能
|
||
|
||
该系统为游戏提供了坚实的任务管理基础,能够有效支撑游戏的核心玩法和用户粘性。 |