Files
pixelheros/.qoder/repowiki/zh/content/英雄系统/英雄系统.md
panw 4235e3b776 refactor(game): 移除已弃用的事件常量
- 删除 UpdateHero 和 UpdateFightHero 事件
- 移除 MISSION_UPDATE 事件常量
- 优化游戏事件枚举定义
2025-10-28 16:15:47 +08:00

594 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 英雄系统
<cite>
**本文档中引用的文件**
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts)
- [SkillSet.ts](file://assets/script/game/common/config/SkillSet.ts)
- [TalSet.ts](file://assets/script/game/common/config/TalSet.ts)
- [Hero.ts](file://assets/script/game/hero/Hero.ts)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts)
- [BuffComp.ts](file://assets/script/game/hero/BuffComp.ts)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts)
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts)
</cite>
## 目录
1. [简介](#简介)
2. [系统架构概览](#系统架构概览)
3. [英雄属性系统](#英雄属性系统)
4. [技能系统](#技能系统)
5. [天赋系统](#天赋系统)
6. [状态管理系统](#状态管理系统)
7. [组件间通信机制](#组件间通信机制)
8. [关键功能实现](#关键功能实现)
9. [性能优化考虑](#性能优化考虑)
10. [总结](#总结)
## 简介
英雄系统是游戏的核心战斗机制负责管理英雄的属性、技能、天赋和状态。本系统采用模块化设计通过ECS架构实现组件间的松耦合通信支持复杂的战斗逻辑和动态效果管理。
## 系统架构概览
英雄系统采用分层架构设计,主要包含以下核心层次:
```mermaid
graph TB
subgraph "表现层"
HV[HeroViewComp<br/>英雄视图组件]
BC[BuffComp<br/>状态显示组件]
end
subgraph "控制层"
HC[Hero<br/>英雄实体]
SC[SkillConComp<br/>技能控制组件]
TC[TalComp<br/>天赋控制组件]
end
subgraph "配置层"
HA[HeroAttrs<br/>属性配置]
SS[SkillSet<br/>技能配置]
TS[TalSet<br/>天赋配置]
end
subgraph "数据层"
HM[HeroModelComp<br/>模型数据]
MM[MonModelComp<br/>怪物数据]
end
HC --> HV
HC --> HM
SC --> HV
TC --> HV
HV --> BC
HV --> HA
SC --> SS
TC --> TS
HV --> MM
```
**图表来源**
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L1-L100)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L100)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L1-L50)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L1-L50)
## 英雄属性系统
### 属性枚举与分类
英雄属性系统定义了完整的战斗属性体系,按照功能分为多个类别:
```mermaid
classDiagram
class Attrs {
<<enumeration>>
+HP_MAX : 0
+MP_MAX : 1
+SHIELD_MAX : 2
+HP_REGEN : 3
+MP_REGEN : 4
+AP : 10
+MAP : 11
+DEF : 20
+MDEF : 21
+CRITICAL : 30
+CRITICAL_DMG : 31
+SPEED : 63
+EXP_GAIN : 64
+STRENGTH : 90
+INTELLIGENCE : 91
+AGILITY : 92
+SPIRIT : 93
+LUCK : 94
}
class BType {
<<enumeration>>
+VALUE : 0
+RATIO : 1
}
class HeroAttrs {
+getAttrs() Object
+isRatioAttr(attrType) boolean
+calculateAttributeGains() Object
+addStrength() Object
+addIntelligence() Object
+addAgility() Object
+addSpirit() Object
+addLuck() Object
}
HeroAttrs --> Attrs
HeroAttrs --> BType
```
**图表来源**
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L15-L100)
### 属性类型配置
系统通过 `AttrsType` 配置表区分属性的计算方式:
| 属性类型 | 计算方式 | 示例属性 |
|---------|---------|---------|
| 数值型 | 直接相加 | HP_MAX, AP, DEF |
| 百分比型 | 基础值×(1+百分比/100) | CRITICAL, SPEED, BUFF_UP |
### 职业属性成长系统
不同职业具有独特的属性成长曲线:
```mermaid
flowchart TD
A[基础属性输入] --> B{职业类型}
B --> |战士| C[力量→HP_MAX:3, AP:1.5, DEF:0.8]
B --> |远程| D[敏捷→CRITICAL:0.8, DODGE:0.6, AS:0.5]
B --> |法师| E[智力→MP_MAX:2, MAP:1.8, MDEF:0.8]
B --> |辅助| F[精神→MP_MAX:2, CON_RES:0.5, BUFF_UP:0.6]
B --> |刺客| G[敏捷→CRITICAL:1, DODGE:0.8, AS:0.6]
C --> H[属性增长计算]
D --> H
E --> H
F --> H
G --> H
H --> I[最终属性值]
```
**图表来源**
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L200-L400)
**章节来源**
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L546)
## 技能系统
### 技能配置结构
技能系统提供了完整的技能数据定义框架:
```mermaid
classDiagram
class SkillConfig {
+uuid : number
+name : string
+TGroup : TGroup
+SType : SType
+DTType : DTType
+DType : DType
+ap : number
+cd : number
+cost : number
+speed : number
+buffs : BuffConf[]
+neAttrs : NeAttrsConf[]
+info : string
}
class TGroup {
<<enumeration>>
+Self : 0
+Ally : 1
+Team : 2
+Enemy : 3
+All : 4
}
class SType {
<<enumeration>>
+damage : 0
+heal : 1
+shield : 2
+buff : 11
}
class BuffConf {
+buff : Attrs
+BType : BType
+value : number
+time : number
+chance : number
}
SkillConfig --> TGroup
SkillConfig --> SType
SkillConfig --> BuffConf
```
**图表来源**
- [SkillSet.ts](file://assets/script/game/common/config/SkillSet.ts#L80-L148)
### 技能控制逻辑
技能释放遵循严格的时序控制:
```mermaid
sequenceDiagram
participant Player as 玩家输入
participant SC as SkillConComp
participant HV as HeroViewComp
participant SE as SkillEnt
participant Target as 目标实体
Player->>SC : 技能冷却完成
SC->>SC : 检查英雄状态
SC->>HV : 选择目标
HV-->>SC : 返回目标坐标
SC->>HV : 播放技能特效
SC->>SE : 创建技能实体
SE->>Target : 执行伤害/效果
Target-->>SE : 反馈结果
SE-->>HV : 更新状态
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L50-L120)
### 目标选择算法
系统实现了智能的目标选择机制:
```mermaid
flowchart TD
A[开始目标选择] --> B[获取敌方实体列表]
B --> C{是否有目标?}
C --> |否| D[返回默认位置]
C --> |是| E[确定最前排目标]
E --> F[添加最前排为目标]
F --> G[随机选择剩余目标]
G --> H[返回目标坐标数组]
D --> I[结束]
H --> I
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L130-L170)
**章节来源**
- [SkillSet.ts](file://assets/script/game/common/config/SkillSet.ts#L1-L148)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L1-L177)
## 天赋系统
### 天赋类型与触发机制
天赋系统支持多种触发条件和效果类型:
```mermaid
classDiagram
class TalType {
<<enumeration>>
+LEVEL : 1
+LEVEL_UP : 2
+ACTION_COUNT : 3
+SKILL_COUNT : 4
+DAMAGE_COUNT : 5
+INIT : 6
+DEAD : 7
}
class TalEType {
<<enumeration>>
+ATTRS : 1
+SKILL : 2
+SKILL_MORE : 3
}
class ItalConf {
+uuid : number
+name : string
+desc : string
+type : TalType
+triggerType : TalEType
+chance : number
+t_value : number
+e_value : number
+e_name : number
+e_type : BType
+e_scaling : number
+e_count : number
+stackable : boolean
+maxStack : number
}
class FightStats {
+aCount : number
+sCount : number
+dCount : number
+level : number
}
ItalConf --> TalType
ItalConf --> TalEType
TalComp --> FightStats
```
**图表来源**
- [TalSet.ts](file://assets/script/game/common/config/TalSet.ts#L10-L50)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L15-L40)
### 天赋触发流程
```mermaid
flowchart TD
A[事件发生] --> B{事件类型}
B --> |等级提升| C[更新FStats.level]
B --> |普通攻击| D[更新aCount]
B --> |技能使用| E[更新sCount]
B --> |受到伤害| F[更新dCount]
C --> G[检查LEVEL天赋]
D --> H[检查ACTION_COUNT天赋]
E --> I[检查SKILL_COUNT天赋]
F --> J[检查DAMAGE_COUNT天赋]
G --> K{满足触发条件?}
H --> K
I --> K
J --> K
K --> |是| L[执行天赋效果]
K --> |否| M[等待下次检查]
L --> N[应用属性增益]
L --> O[触发技能]
L --> P[增加技能数量]
N --> Q[重新计算属性]
O --> R[技能释放]
P --> S[技能列表更新]
```
**图表来源**
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L120-L170)
**章节来源**
- [TalSet.ts](file://assets/script/game/common/config/TalSet.ts#L1-L116)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L1-L171)
## 状态管理系统
### Buff/Debuff系统架构
状态管理系统实现了完整的增益/减益效果管理:
```mermaid
classDiagram
class HeroViewComp {
+BUFFS : Record~number, Array~
+BUFFS_TEMP : Record~number, Array~
+NeAttrs : Record~number, Object~
+addBuff(buffConf) void
+removeBuff(attrIndex, value, isPermanent) void
+clearBuffs(attrIndex, isBuff) void
+recalculateSingleAttr(attrIndex) void
+updateTemporaryBuffsDebuffs(dt) void
}
class BuffInfo {
+attr : Attrs
+value : number
+remainTime : number
}
class NeAttrs {
+IN_FROST : 0
+IN_STUN : 1
+IN_BURN : 2
+IN_POISON : 3
}
HeroViewComp --> BuffInfo
HeroViewComp --> NeAttrs
```
**图表来源**
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L80-L120)
### 属性计算公式
系统采用复合计算公式处理不同类型的属性:
| 属性类型 | 计算公式 | 说明 |
|---------|---------|------|
| 数值型 | `(基础值 + 所有数值型buff之和) × (1 + 所有百分比buff之和/100)` | 攻击力、防御力等 |
| 百分比型 | `基础值 + 所有数值型buff之和 + 所有百分比buff之和` | 暴击率、闪避率等 |
### 状态更新机制
```mermaid
sequenceDiagram
participant Game as 游戏循环
participant HV as HeroViewComp
participant Buff as Buff系统
participant Attr as 属性计算
Game->>HV : update(dt)
HV->>Buff : updateTemporaryBuffsDebuffs(dt)
Buff->>Buff : 减少剩余时间
Buff->>Buff : 移除过期buff
Buff->>Attr : recalculateSingleAttr()
Attr->>Attr : 重新计算属性值
Attr-->>HV : 更新后的属性
HV->>HV : clampSingleAttr()
HV-->>Game : 状态更新完成
```
**图表来源**
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L400-L500)
**章节来源**
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
- [BuffComp.ts](file://assets/script/game/hero/BuffComp.ts#L1-L213)
## 组件间通信机制
### ECS架构下的组件交互
```mermaid
graph LR
subgraph "英雄实体"
E[Hero实体]
HV[HeroViewComp]
HC[HeroConComp]
BM[BattleMoveComp]
end
subgraph "技能系统"
SC[SkillConComp]
SE[SkillEnt]
end
subgraph "状态系统"
BC[BuffComp]
TC[TalComp]
end
E --> HV
E --> HC
E --> BM
HC --> SC
SC --> SE
HV --> BC
HV --> TC
TC --> HV
SC --> HV
```
**图表来源**
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L15-L35)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L15-L30)
### 事件驱动通信
系统通过事件机制实现组件间的解耦通信:
| 事件类型 | 发送者 | 接收者 | 用途 |
|---------|-------|-------|------|
| GameEvent.MasterCalled | Hero | Talent系统 | 英雄召唤事件 |
| GameEvent.CastHeroSkill | SkillCon | 其他组件 | 技能释放通知 |
| GameEvent.FightEnd | Battle | 所有组件 | 战斗结束事件 |
| GameEvent.HeroDead | HeroView | Talent系统 | 英雄死亡事件 |
**章节来源**
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L1-L100)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L1-L50)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L1-L50)
## 关键功能实现
### 技能释放流程
技能释放是英雄系统的核心功能,涉及多个组件的协调工作:
```mermaid
flowchart TD
A[玩家输入技能] --> B[SkillConComp接收]
B --> C{英雄状态检查}
C --> |眩晕/冰冻| D[阻止技能释放]
C --> |正常状态| E[检查冷却时间]
E --> |冷却完成| F[检查魔法值]
F --> |魔法值充足| G[选择目标]
F --> |魔法值不足| H[显示提示]
G --> I[播放技能特效]
I --> J[创建技能实体]
J --> K[执行伤害计算]
K --> L[应用Buff效果]
L --> M[更新英雄状态]
D --> N[技能释放失败]
H --> N
M --> O[技能释放成功]
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L50-L100)
### 属性升级系统
属性升级通过职业成长配置实现:
```mermaid
flowchart TD
A[属性点分配] --> B{职业类型}
B --> |力量| C[addStrength函数]
B --> |智力| D[addIntelligence函数]
B --> |敏捷| E[addAgility函数]
B --> |精神| F[addSpirit函数]
B --> |幸运| G[addLuck函数]
C --> H[计算力量成长]
D --> I[计算智力成长]
E --> J[计算敏捷成长]
F --> K[计算精神成长]
G --> L[计算幸运成长]
H --> M[应用到属性]
I --> M
J --> M
K --> M
L --> M
M --> N[重新计算所有属性]
N --> O[更新显示]
```
**图表来源**
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L500-L546)
### 天赋触发机制
天赋系统实现了复杂的触发逻辑:
```mermaid
flowchart TD
A[事件触发] --> B[更新统计数据]
B --> C[遍历天赋配置]
C --> D{检查触发条件}
D --> |满足| E[计算触发概率]
D --> |不满足| F[跳过此天赋]
E --> |触发| G[执行天赋效果]
E --> |不触发| F
G --> H{效果类型}
H --> |属性增益| I[添加Buff]
H --> |技能触发| J[释放技能]
H --> |技能增加| K[增加技能列表]
I --> L[重新计算属性]
J --> M[技能执行]
K --> N[技能可用]
L --> O[更新界面]
M --> O
N --> O
F --> P[检查下一个天赋]
P --> C
```
**图表来源**
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L100-L170)
**章节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L50-L177)
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L500-L546)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L100-L171)
## 性能优化考虑
### 内存管理优化
1. **对象池化**: 技能实体和伤害数字使用对象池减少GC压力
2. **延迟加载**: 天赋效果按需计算,避免不必要的属性重计算
3. **弱引用**: 避免组件间的强引用导致内存泄漏
### 计算优化
1. **增量计算**: 只重新计算受影响的属性,而不是全部属性
2. **缓存机制**: 缓存属性计算结果,避免重复计算
3. **批量更新**: 在同一帧内批量处理多个状态变化
### 渲染优化
1. **状态显示**: BuffComp只更新可见的状态信息
2. **特效管理**: 技能特效使用预制体池,避免频繁创建销毁
3. **层级管理**: 合理管理UI层级减少渲染开销
## 总结
英雄系统通过模块化设计和ECS架构实现了高度可扩展和可维护的战斗机制。系统的主要特点包括
1. **完整的属性体系**: 支持数值型和百分比型属性的灵活组合
2. **智能技能管理**: 提供丰富的技能配置选项和自动化的目标选择
3. **动态天赋系统**: 支持多种触发条件和效果类型的天赋配置
4. **高效状态管理**: 实现了完整的Buff/Debuff生命周期管理
5. **松耦合架构**: 通过ECS和事件机制实现组件间的解耦通信
该系统为游戏提供了坚实的战斗基础,支持复杂的游戏玩法和深度的角色养成体验。通过合理的架构设计和性能优化,确保了系统的稳定性和可扩展性。