490 lines
14 KiB
Markdown
490 lines
14 KiB
Markdown
# 属性系统
|
||
|
||
<cite>
|
||
**本文档引用的文件**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts)
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts)
|
||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||
- [Hero.ts](file://assets/script/game/hero/Hero.ts)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts)
|
||
- [RogueConfig.ts](file://assets/script/game/map/RogueConfig.ts)
|
||
- [Mission.ts](file://assets/script/game/common/config/Mission.ts)
|
||
</cite>
|
||
|
||
## 目录
|
||
1. [简介](#简介)
|
||
2. [项目结构](#项目结构)
|
||
3. [核心组件](#核心组件)
|
||
4. [架构概览](#架构概览)
|
||
5. [详细组件分析](#详细组件分析)
|
||
6. [依赖关系分析](#依赖关系分析)
|
||
7. [性能考虑](#性能考虑)
|
||
8. [故障排除指南](#故障排除指南)
|
||
9. [结论](#结论)
|
||
|
||
## 简介
|
||
|
||
英雄属性系统是游戏战斗机制的核心组成部分,负责管理角色的基础属性、动态计算和战斗中的属性变化。该系统通过strengthMultiplier强度倍率实现了基于配置数据的动态属性缩放机制,支持HP、AP、DEF、SPEED等基础属性的精确计算,并提供了完整的buff/debuff管理系统。
|
||
|
||
系统采用模块化设计,将属性定义、计算逻辑、配置管理和运行时控制分离,确保了良好的可维护性和扩展性。通过HeroAttrs.ts中的getAttrs与getNeAttrs函数,系统建立了完整的属性容器构建逻辑,支持英雄和怪物的统一属性管理。
|
||
|
||
## 项目结构
|
||
|
||
属性系统的核心文件分布在以下目录结构中:
|
||
|
||
```mermaid
|
||
graph TB
|
||
subgraph "配置层"
|
||
A[HeroAttrs.ts] --> B[属性枚举定义]
|
||
A --> C[属性类型配置]
|
||
A --> D[职业成长配置]
|
||
E[heroSet.ts] --> F[英雄配置数据]
|
||
E --> G[怪物配置数据]
|
||
end
|
||
subgraph "实体层"
|
||
H[Hero.ts] --> I[英雄实体]
|
||
J[Mon.ts] --> K[怪物实体]
|
||
end
|
||
subgraph "视图层"
|
||
L[HeroViewComp.ts] --> M[属性计算引擎]
|
||
L --> N[Buff系统]
|
||
L --> O[负面状态管理]
|
||
end
|
||
A --> L
|
||
E --> H
|
||
E --> J
|
||
H --> L
|
||
J --> L
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L546)
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts#L1-L152)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
|
||
|
||
**章节来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L100)
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts#L1-L50)
|
||
|
||
## 核心组件
|
||
|
||
### 属性枚举系统
|
||
|
||
属性系统定义了完整的属性分类体系,涵盖基础生存、攻击、防御、特殊效果等多个维度:
|
||
|
||
| 属性类别 | 属性名称 | 类型 | 描述 |
|
||
|---------|---------|------|------|
|
||
| 基础生存 | HP_MAX | 数值型 | 最大生命值 |
|
||
| 基础生存 | MP_MAX | 数值型 | 最大魔法值 |
|
||
| 攻击属性 | AP | 数值型 | 攻击力 |
|
||
| 攻击属性 | MAP | 数值型 | 魔法攻击力 |
|
||
| 防御属性 | DEF | 数值型 | 物理防御 |
|
||
| 防御属性 | MDEF | 数值型 | 魔法防御 |
|
||
| 特殊效果 | SPEED | 百分比型 | 移动速度加成 |
|
||
| 特殊效果 | CRITICAL | 百分比型 | 暴击率 |
|
||
|
||
### Buff类型系统
|
||
|
||
系统支持两种类型的Buff效果:
|
||
|
||
```mermaid
|
||
classDiagram
|
||
class BType {
|
||
+VALUE : 0
|
||
+RATIO : 1
|
||
}
|
||
class BuffInstance {
|
||
+number value
|
||
+BType BType
|
||
+number remainTime
|
||
}
|
||
class HeroViewComp {
|
||
+Record~number, Array~ BUFFS
|
||
+Record~number, Array~ BUFFS_TEMP
|
||
+recalculateSingleAttr(attrIndex)
|
||
+addBuff(buffConf)
|
||
+clearBuffs(attrIndex, isBuff)
|
||
}
|
||
HeroViewComp --> BuffInstance
|
||
BuffInstance --> BType
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L5-L15)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L95-L110)
|
||
|
||
**章节来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L20-L100)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L72-L120)
|
||
|
||
## 架构概览
|
||
|
||
属性系统采用分层架构设计,实现了清晰的职责分离:
|
||
|
||
```mermaid
|
||
graph TD
|
||
A[配置层] --> B[计算层]
|
||
B --> C[运行时层]
|
||
C --> D[渲染层]
|
||
A1[属性枚举] --> A
|
||
A2[职业配置] --> A
|
||
A3[Buff配置] --> A
|
||
B1[属性计算] --> B
|
||
B2[强度倍率] --> B
|
||
B3[成长曲线] --> B
|
||
C1[属性容器] --> C
|
||
C2[Buff系统] --> C
|
||
C3[负面状态] --> C
|
||
D1[视觉效果] --> D
|
||
D2[伤害显示] --> D
|
||
D3[状态提示] --> D
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L546)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
|
||
|
||
## 详细组件分析
|
||
|
||
### hero_init方法中的强度倍率计算
|
||
|
||
hero_init方法是属性系统的核心入口,负责根据strengthMultiplier强度倍率进行动态属性计算:
|
||
|
||
```mermaid
|
||
sequenceDiagram
|
||
participant Client as 客户端
|
||
participant Mon as Monster
|
||
participant Hero as Hero
|
||
participant Calc as 属性计算器
|
||
participant View as HeroViewComp
|
||
Client->>Mon : hero_init(strengthMultiplier)
|
||
Mon->>Calc : 计算基础属性
|
||
Calc->>Calc : baseHp = hero.hp * strengthMultiplier
|
||
Calc->>Calc : baseAp = hero.ap * strengthMultiplier
|
||
Calc->>Calc : baseDef = hero.def * strengthMultiplier
|
||
Mon->>View : 初始化属性容器
|
||
View->>View : getAttrs()
|
||
View->>View : getNeAttrs()
|
||
View->>View : 设置基础属性
|
||
View->>View : initAttrs()
|
||
View-->>Client : 属性初始化完成
|
||
```
|
||
|
||
**图表来源**
|
||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L61-L108)
|
||
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L65-L100)
|
||
|
||
#### 强度倍率的应用机制
|
||
|
||
强度倍率通过以下方式影响属性计算:
|
||
|
||
1. **基础属性缩放**:`baseHp = hero.hp * strengthMultiplier`
|
||
2. **攻击力调整**:`baseAp = hero.ap * strengthMultiplier`
|
||
3. **防御力计算**:`baseDef = hero.def * strengthMultiplier`
|
||
|
||
这种设计允许系统根据关卡进度、难度系数等因素动态调整怪物强度,而无需修改英雄配置数据。
|
||
|
||
**章节来源**
|
||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L61-L108)
|
||
- [Hero.ts](file://assets/script/game/hero/Hero.ts#L65-L100)
|
||
|
||
### hv.Attrs与hv.NeAttrs属性系统
|
||
|
||
#### 属性容器初始化流程
|
||
|
||
属性容器的初始化遵循严格的步骤序列:
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
A[开始初始化] --> B[清空现有Buff]
|
||
B --> C[获取英雄配置]
|
||
C --> D[重置基础属性]
|
||
D --> E[初始化其他属性]
|
||
E --> F[加载初始Buff]
|
||
F --> G[属性计算引擎就绪]
|
||
D1[Attrs.HP_MAX = base_hp] --> D
|
||
D2[Attrs.MP_MAX = base_mp] --> D
|
||
D3[Attrs.DEF = base_def] --> D
|
||
D4[Attrs.AP = base_ap] --> D
|
||
D5[Attrs.MAP = base_map] --> D
|
||
D6[Attrs.SPEED = base_speed] --> D
|
||
D7[Attrs.DIS = base_dis] --> D
|
||
E1[设置其他属性为0] --> E
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L120-L180)
|
||
|
||
#### 属性容器构建逻辑
|
||
|
||
getAttrs和getNeAttrs函数负责构建属性容器:
|
||
|
||
```mermaid
|
||
classDiagram
|
||
class Attrs {
|
||
<<enumeration>>
|
||
HP_MAX : 0
|
||
MP_MAX : 1
|
||
AP : 10
|
||
DEF : 20
|
||
SPEED : 63
|
||
+getAttrs() : object
|
||
}
|
||
class NeAttrs {
|
||
<<enumeration>>
|
||
IN_FROST : 0
|
||
IN_STUN : 1
|
||
IN_BURN : 2
|
||
IN_POISON : 3
|
||
+getNeAttrs() : object
|
||
}
|
||
class HeroViewComp {
|
||
+any Attrs
|
||
+any NeAttrs
|
||
+initAttrs()
|
||
+recalculateSingleAttr(attrIndex)
|
||
}
|
||
HeroViewComp --> Attrs
|
||
HeroViewComp --> NeAttrs
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L16-L30)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L72-L120)
|
||
|
||
**章节来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L16-L30)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L120-L180)
|
||
|
||
### 怪物数据共享机制
|
||
|
||
怪物通过heroSet.ts中的英雄配置数据实现数据共享:
|
||
|
||
#### 怪物配置复用
|
||
|
||
```mermaid
|
||
graph LR
|
||
A[HeroInfo配置] --> B[英雄数据]
|
||
A --> C[怪物数据]
|
||
B1[uuid: 5001] --> B
|
||
B2[name: 刘邦] --> B
|
||
B3[type: warrior] --> B
|
||
B4[hp: 125] --> B
|
||
B5[ap: 15] --> B
|
||
C1[uuid: 5201] --> C
|
||
C2[name: 兽人战士] --> C
|
||
C3[type: warrior] --> C
|
||
C4[hp: 25] --> C
|
||
C5[ap: 5] --> C
|
||
B -.->|复用| C
|
||
```
|
||
|
||
**图表来源**
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts#L70-L152)
|
||
|
||
#### 怪物强度动态调整
|
||
|
||
系统通过RogueConfig.ts提供怪物强度的动态调整机制:
|
||
|
||
| 参数 | 默认值 | 说明 |
|
||
|------|--------|------|
|
||
| stageMultiplier | 1 + (stageNumber - 1) * 0.1 | 关卡倍率,每关增加10% |
|
||
| levelMultiplier | 1 + (level - 1) * 0.05 | 等级倍率,每级增加5% |
|
||
| totalMultiplier | stageMultiplier * levelMultiplier | 总强度倍率 |
|
||
|
||
**章节来源**
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts#L70-L152)
|
||
- [RogueConfig.ts](file://assets/script/game/map/RogueConfig.ts#L180-L205)
|
||
|
||
### 属性计算引擎
|
||
|
||
#### 属性计算公式
|
||
|
||
系统采用双重计算模式,根据属性类型选择不同的计算公式:
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
A[属性计算开始] --> B{属性类型判断}
|
||
B --> |数值型| C[数值型计算]
|
||
B --> |百分比型| D[百分比型计算]
|
||
C --> C1[基础值 + 数值Buff]
|
||
C1 --> C2[× (1 + 百分比Buff/100)]
|
||
C2 --> C3[Math.floor结果)
|
||
D --> D1[基础值 + 数值Buff]
|
||
D1 --> D2[+ 百分比Buff]
|
||
D2 --> D3[直接结果]
|
||
C3 --> E[属性值规范化]
|
||
D3 --> E
|
||
E --> F[属性计算完成]
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L250-L320)
|
||
|
||
#### Buff系统管理
|
||
|
||
Buff系统支持持久型和临时型两种Buff类型:
|
||
|
||
```mermaid
|
||
classDiagram
|
||
class BuffSystem {
|
||
+Record~number, Array~ BUFFS
|
||
+Record~number, Array~ BUFFS_TEMP
|
||
+addBuff(buffConf)
|
||
+removeBuff(attrIndex, value, isPermanent)
|
||
+updateTemporaryBuffsDebuffs(dt)
|
||
+recalculateSingleAttr(attrIndex)
|
||
}
|
||
class BuffConf {
|
||
+number buff
|
||
+BType BType
|
||
+number value
|
||
+number time
|
||
+number chance
|
||
}
|
||
class BuffInstance {
|
||
+number value
|
||
+BType BType
|
||
+number remainTime
|
||
}
|
||
BuffSystem --> BuffConf
|
||
BuffSystem --> BuffInstance
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L180-L250)
|
||
|
||
**章节来源**
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L180-L350)
|
||
|
||
### 职业成长曲线系统
|
||
|
||
#### 职业属性增长配置
|
||
|
||
系统为每种职业定义了独特的属性成长曲线:
|
||
|
||
```mermaid
|
||
graph TB
|
||
subgraph "战士职业成长"
|
||
A1[力量→生命值: 3.0]
|
||
A2[力量→攻击力: 1.5]
|
||
A3[力量→防御力: 0.8]
|
||
A4[敏捷→暴击率: 0.3]
|
||
A5[精神→吸血: 0.4]
|
||
end
|
||
subgraph "刺客职业成长"
|
||
B1[力量→生命值: 1.8]
|
||
B2[力量→攻击力: 1.3]
|
||
B3[力量→防御力: 0.3]
|
||
B4[敏捷→暴击率: 1.0]
|
||
B5[幸运→暴击率: 1.5]
|
||
end
|
||
subgraph "法师职业成长"
|
||
C1[智力→魔法值: 2.0]
|
||
C2[智力→魔法攻击: 1.8]
|
||
C3[智力→魔法防御: 0.8]
|
||
C4[智力→作用范围: 0.3]
|
||
end
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L266-L439)
|
||
|
||
#### 成长系数计算
|
||
|
||
系统提供了多种快捷方法来计算属性增长:
|
||
|
||
| 方法名 | 输入参数 | 输出 | 用途 |
|
||
|--------|----------|------|------|
|
||
| addStrength | heroType, strengthPoints | 属性增长映射 | 计算力量属性增长 |
|
||
| addIntelligence | heroType, intelligencePoints | 属性增长映射 | 计算智力属性增长 |
|
||
| addAgility | heroType, agilityPoints | 属性增长映射 | 计算敏捷属性增长 |
|
||
| calculateTotalAttributeGains | heroType, baseAttrs | 总属性增长 | 计算多属性总增长 |
|
||
|
||
**章节来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L435-L544)
|
||
|
||
## 依赖关系分析
|
||
|
||
属性系统的依赖关系呈现清晰的层次结构:
|
||
|
||
```mermaid
|
||
graph TD
|
||
A[HeroAttrs.ts] --> B[HeroViewComp.ts]
|
||
C[heroSet.ts] --> D[Hero.ts]
|
||
C --> E[Mon.ts]
|
||
B --> D
|
||
B --> E
|
||
F[RogueConfig.ts] --> E
|
||
G[Mission.ts] --> F
|
||
A --> H[属性枚举]
|
||
A --> I[属性类型]
|
||
A --> J[职业配置]
|
||
K[GameEvent.ts] --> B
|
||
L[SkillSet.ts] --> B
|
||
```
|
||
|
||
**图表来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L10)
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L20)
|
||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L1-L15)
|
||
|
||
**章节来源**
|
||
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts#L1-L10)
|
||
- [heroSet.ts](file://assets/script/game/common/config/heroSet.ts#L1-L10)
|
||
|
||
## 性能考虑
|
||
|
||
### 属性计算优化策略
|
||
|
||
1. **延迟计算**:只在必要时重新计算属性值
|
||
2. **批量更新**:合并多个属性变更操作
|
||
3. **缓存机制**:避免重复计算相同属性
|
||
4. **增量更新**:只更新受影响的属性
|
||
|
||
### 内存管理
|
||
|
||
- 使用对象池管理Buff实例
|
||
- 及时清理过期的临时Buff
|
||
- 避免属性容器的频繁重建
|
||
|
||
### 计算复杂度
|
||
|
||
属性计算的时间复杂度为O(n),其中n为活跃Buff的数量。系统通过以下方式优化性能:
|
||
|
||
- 使用索引快速定位属性
|
||
- 批量处理相似类型的Buff
|
||
- 延迟执行非关键属性计算
|
||
|
||
## 故障排除指南
|
||
|
||
### 常见问题及解决方案
|
||
|
||
#### 属性计算异常
|
||
|
||
**问题**:属性值超出预期范围
|
||
**原因**:Buff叠加过多或计算公式错误
|
||
**解决方案**:检查Buff配置,验证属性值规范化逻辑
|
||
|
||
#### 性能问题
|
||
|
||
**问题**:属性更新导致帧率下降
|
||
**原因**:频繁的属性重新计算
|
||
**解决方案**:实施属性变更批处理,减少不必要的重新计算
|
||
|
||
#### 数据同步问题
|
||
|
||
**问题**:客户端与服务器属性不一致
|
||
**原因**:配置数据版本不匹配
|
||
**解决方案**:实施配置版本控制,确保数据一致性
|
||
|
||
**章节来源**
|
||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L315-L354)
|
||
|
||
## 结论
|
||
|
||
英雄属性系统通过strengthMultiplier强度倍率实现了灵活的动态属性计算机制,支持HP、AP、DEF、SPEED等基础属性的精确缩放。系统采用模块化设计,将属性定义、计算逻辑、配置管理和运行时控制分离,确保了良好的可维护性和扩展性。
|
||
|
||
通过HeroAttrs.ts中的getAttrs与getNeAttrs函数,系统建立了完整的属性容器构建逻辑,支持英雄和怪物的统一属性管理。职业成长曲线系统为不同职业提供了特色化的属性发展路径,增强了游戏的策略深度。
|
||
|
||
怪物通过heroSet.ts中的英雄配置数据实现数据共享,配合RogueConfig.ts中的强度倍率计算,实现了自定义属性成长曲线和难度系数调整功能。系统提供了丰富的配置选项和性能优化建议,为游戏开发提供了强大的属性管理基础设施。 |