Files
heros/.qoder/repowiki/zh/content/英雄系统/英雄系统.md
2025-10-30 16:49:19 +08:00

682 lines
17 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)
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.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)
- [HeroAttrsComp.ts](file://assets/script/game/hero/HeroAttrsComp.ts)
- [HeroAtk.ts](file://assets/script/game/hero/HeroAtk.ts)
- [EBusComp.ts](file://assets/script/game/hero/EBusComp.ts)
</cite>
## 更新摘要
**变更内容**
- 移除了 `SkillConComp` 组件重构为基于ECS系统的技能控制机制
-`is_atking` 攻击状态从视图层 `HeroViewComp` 迁移到数据层 `HeroAttrsComp`
- 拆分通用移动组件为专属的英雄和怪物移动系统
- 在攻击和死亡事件中集成视觉反馈
- 根据ECS框架重构角色视图和数据逻辑新增事件总线组件 `EBusComp`
**新增章节**
- ECS架构下的技能系统
- 攻击与死亡事件的视觉反馈
**已移除章节**
- 原有的 `SkillConComp` 技能控制组件相关内容
**来源追踪系统更新**
- 更新了受影响文件的引用链接和行号范围
- 添加了新组件和系统的文件引用
- 标记了已移除组件的废弃状态
## 目录
1. [简介](#简介)
2. [系统架构概览](#系统架构概览)
3. [英雄属性系统](#英雄属性系统)
4. [技能系统](#技能系统)
5. [天赋系统](#天赋系统)
6. [状态管理系统](#状态管理系统)
7. [组件间通信机制](#组件间通信机制)
8. [关键功能实现](#关键功能实现)
9. [性能优化考虑](#性能优化考虑)
10. [总结](#总结)
## 简介
英雄系统是游戏的核心战斗机制负责管理英雄的属性、技能、天赋和状态。本系统采用模块化设计通过ECS架构实现组件间的松耦合通信支持复杂的战斗逻辑和动态效果管理。近期重构移除了 `SkillConComp` 组件将技能控制逻辑迁移至基于ECS系统的 `HSkillSystem`,同时将攻击状态从视图层迁移至数据层,实现了更合理的数据与表现分离。
## 系统架构概览
英雄系统采用分层架构设计,主要包含以下核心层次:
```mermaid
graph TB
subgraph "表现层"
HV[HeroViewComp<br/>英雄视图组件]
BC[BuffComp<br/>状态显示组件]
end
subgraph "控制层"
HC[Hero<br/>英雄实体]
SS[HSkillSystem<br/>技能系统]
TC[TalComp<br/>天赋控制组件]
end
subgraph "配置层"
HA[HeroAttrs<br/>属性配置]
SSC[SkillSet<br/>技能配置]
TS[TalSet<br/>天赋配置]
end
subgraph "数据层"
HAC[HeroAttrsComp<br/>属性数据组件]
HM[HeroModelComp<br/>模型数据]
MM[MonModelComp<br/>怪物数据]
end
HC --> HV
HC --> HAC
SS --> HV
TC --> HV
HV --> BC
HV --> HA
SS --> SSC
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)
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.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)
### ECS架构下的技能系统
技能系统已重构为基于ECS的系统架构主要包含以下组件
```mermaid
classDiagram
class CastSkillRequestComp {
+skillIndex : number
+targetPositions : Vec3[]
+reset() void
}
class SkillCastSystem {
+filter() IMatcher
+entityEnter(e : Entity) void
+checkCastConditions() boolean
+executeCast() void
+createSkillEntity() void
}
class SkillCDSystem {
+filter() IMatcher
+update(e : Entity) void
+updateCDs(dt : number) void
}
class SkillAutocastSystem {
+filter() IMatcher
+update(e : Entity) void
+selectTargets() Vec3[]
}
CastSkillRequestComp --> SkillCastSystem
SkillCastSystem --> SkillCDSystem
SkillAutocastSystem --> CastSkillRequestComp
```
**章节来源**
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L1-L271)
### 技能施法流程
技能施法遵循ECS标记组件驱动的时序控制
```mermaid
sequenceDiagram
participant Player as 玩家输入
participant SC as SkillAutocastSystem
participant HV as HeroViewComp
participant SE as SkillEnt
participant Target as 目标实体
Player->>SC : 添加CastSkillRequest
SC->>SC : 检查英雄状态
SC->>HV : 选择目标
HV-->>SC : 返回目标坐标
SC->>HV : 播放技能特效
SC->>SE : 创建技能实体
SE->>Target : 执行伤害/效果
Target-->>SE : 反馈结果
SE-->>HV : 更新状态
```
**图表来源**
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L100-L150)
### 目标选择算法
系统实现了智能的目标选择机制:
```mermaid
flowchart TD
A[开始目标选择] --> B[获取敌方实体列表]
B --> C{是否有目标?}
C --> |否| D[返回默认位置]
C --> |是| E[确定最前排目标]
E --> F[添加最前排为目标]
F --> G[随机选择剩余目标]
G --> H[返回目标坐标数组]
D --> I[结束]
H --> I
```
**图表来源**
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L200-L250)
**章节来源**
- [SkillSet.ts](file://assets/script/game/common/config/SkillSet.ts#L1-L148)
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L1-L271)
## 天赋系统
### 天赋类型与触发机制
天赋系统支持多种触发条件和效果类型:
```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 HeroAttrsComp {
+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
}
HeroAttrsComp --> BuffInfo
HeroAttrsComp --> NeAttrs
```
**图表来源**
- [HeroAttrsComp.ts](file://assets/script/game/hero/HeroAttrsComp.ts#L80-L120)
### 属性计算公式
系统采用复合计算公式处理不同类型的属性:
| 属性类型 | 计算公式 | 说明 |
|---------|---------|------|
| 数值型 | `(基础值 + 所有数值型buff之和) × (1 + 所有百分比buff之和/100)` | 攻击力、防御力等 |
| 百分比型 | `基础值 + 所有数值型buff之和 + 所有百分比buff之和` | 暴击率、闪避率等 |
### 状态更新机制
```mermaid
sequenceDiagram
participant Game as 游戏循环
participant HAC as HeroAttrsComp
participant Buff as Buff系统
participant Attr as 属性计算
Game->>HAC : update(dt)
HAC->>Buff : updateTemporaryBuffsDebuffs(dt)
Buff->>Buff : 减少剩余时间
Buff->>Buff : 移除过期buff
Buff->>Attr : recalculateSingleAttr()
Attr->>Attr : 重新计算属性值
Attr-->>HAC : 更新后的属性
HAC->>HAC : clampSingleAttr()
HAC-->>Game : 状态更新完成
```
**图表来源**
- [HeroAttrsComp.ts](file://assets/script/game/hero/HeroAttrsComp.ts#L400-L500)
**章节来源**
- [HeroAttrsComp.ts](file://assets/script/game/hero/HeroAttrsComp.ts#L1-L489)
- [BuffComp.ts](file://assets/script/game/hero/BuffComp.ts#L1-L213)
## 组件间通信机制
### ECS架构下的组件交互
```mermaid
graph LR
subgraph "英雄实体"
E[Hero实体]
HV[HeroViewComp]
HAC[HeroAttrsComp]
BM[HeroMoveComp]
end
subgraph "技能系统"
SS[HSkillSystem]
SE[SkillEnt]
end
subgraph "状态系统"
BC[BuffComp]
TC[TalComp]
end
E --> HV
E --> HAC
E --> BM
HAC --> SS
SS --> SE
HV --> BC
HV --> TC
TC --> HV
SS --> HV
```
**图表来源**
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L15-L35)
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L15-L30)
### 事件驱动通信
系统通过事件机制实现组件间的解耦通信:
| 事件类型 | 发送者 | 接收者 | 用途 |
|---------|-------|-------|------|
| GameEvent.MasterCalled | Hero | Talent系统 | 英雄召唤事件 |
| GameEvent.CastHeroSkill | HSkillSystem | 其他组件 | 技能释放通知 |
| GameEvent.FightEnd | Battle | 所有组件 | 战斗结束事件 |
| GameEvent.HeroDead | HeroView | Talent系统 | 英雄死亡事件 |
**章节来源**
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L1-L100)
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L1-L50)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L1-L50)
## 关键功能实现
### 技能释放流程
技能释放是英雄系统的核心功能,涉及多个组件的协调工作:
```mermaid
flowchart TD
A[玩家输入技能] --> B[HSkillSystem接收]
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[技能释放成功]
```
**图表来源**
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.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)
**章节来源**
- [HSkillSystem.ts](file://assets/script/game/hero/HSkillSystem.ts#L50-L271)
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L500-L546)
- [TalComp.ts](file://assets/script/game/hero/TalComp.ts#L100-L171)
### 攻击与死亡事件的视觉反馈
攻击和死亡事件现在集成视觉反馈,增强游戏表现力:
```mermaid
sequenceDiagram
participant Attack as 攻击事件
participant HAC as HeroAttrsComp
participant HV as HeroViewComp
participant AtkSys as HeroAtkSystem
Attack->>HAC : 发起攻击
HAC->>AtkSys : doAttack()
AtkSys->>HV : do_atked()
HV->>HV : 显示伤害数字
HV->>HV : 播放受击动画
HV->>HV : 角色后退
Attack->>HAC : 造成伤害
HAC->>HAC : 更新血量
HAC->>HV : 检查死亡
HV->>HV : do_dead()
HV->>HV : 播放死亡特效
HV->>HV : 触发死亡事件
```
**章节来源**
- [HeroAtk.ts](file://assets/script/game/hero/HeroAtk.ts#L1-L247)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L200-L300)
## 性能优化考虑
### 内存管理优化
1. **对象池化**: 技能实体和伤害数字使用对象池减少GC压力
2. **延迟加载**: 天赋效果按需计算,避免不必要的属性重计算
3. **弱引用**: 避免组件间的强引用导致内存泄漏
### 计算优化
1. **增量计算**: 只重新计算受影响的属性,而不是全部属性
2. **缓存机制**: 缓存属性计算结果,避免重复计算
3. **批量更新**: 在同一帧内批量处理多个状态变化
### 渲染优化
1. **状态显示**: BuffComp只更新可见的状态信息
2. **特效管理**: 技能特效使用预制体池,避免频繁创建销毁
3. **层级管理**: 合理管理UI层级减少渲染开销
## 总结
英雄系统通过模块化设计和ECS架构实现了高度可扩展和可维护的战斗机制。系统的主要特点包括
1. **完整的属性体系**: 支持数值型和百分比型属性的灵活组合
2. **智能技能管理**: 提供丰富的技能配置选项和自动化的目标选择
3. **动态天赋系统**: 支持多种触发条件和效果类型的天赋配置
4. **高效状态管理**: 实现了完整的Buff/Debuff生命周期管理
5. **松耦合架构**: 通过ECS和事件机制实现组件间的解耦通信
近期重构将技能控制逻辑迁移至ECS系统移除了 `SkillConComp` 组件,同时将攻击状态从视图层迁移至数据层,实现了更合理的数据与表现分离。该系统为游戏提供了坚实的战斗基础,支持复杂的游戏玩法和深度的角色养成体验。通过合理的架构设计和性能优化,确保了系统的稳定性和可扩展性。