Files
pixelheros/.qoder/repowiki/zh/content/技能执行机制/技能冷却管理.md
2025-10-30 16:49:19 +08:00

440 lines
13 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>
**本文档引用的文件**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts)
- [SkillSet.ts](file://assets/script/game/common/config/SkillSet.ts)
- [GameEvent.ts](file://assets/script/game/common/config/GameEvent.ts)
- [BoxSet.ts](file://assets/script/game/common/config/BoxSet.ts)
- [HeroAttrs.ts](file://assets/script/game/common/config/HeroAttrs.ts)
</cite>
## 目录
1. [简介](#简介)
2. [系统架构概览](#系统架构概览)
3. [核心组件分析](#核心组件分析)
4. [冷却机制实现](#冷却机制实现)
5. [状态管理与控制流](#状态管理与控制流)
6. [定时器资源管理](#定时器资源管理)
7. [最佳实践指南](#最佳实践指南)
8. [故障排除](#故障排除)
9. [总结](#总结)
## 简介
SkillConComp是游戏技能冷却管理系统的核心组件负责维护和管理角色技能的冷却状态。该系统采用基于update循环的帧累加机制通过精确的时间累积和多重条件判断来实现智能的技能触发逻辑。系统不仅处理技能冷却计算还集成状态检测、资源管理和生命周期控制等功能。
## 系统架构概览
技能冷却管理系统采用分层架构设计,主要由以下层次组成:
```mermaid
graph TB
subgraph "技能冷却系统架构"
A[SkillConComp] --> B[HeroViewComp]
A --> C[SkillSet配置]
A --> D[GameEvent事件]
A --> E[定时器管理]
B --> F[技能列表]
B --> G[状态检测]
B --> H[属性计算]
C --> I[技能配置]
C --> J[冷却参数]
D --> K[战斗结束事件]
D --> L[暂停事件]
E --> M[setTimeout管理]
E --> N[cleanup机制]
end
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L1-L177)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
## 核心组件分析
### SkillConComp组件
SkillConComp是技能冷却管理的主要控制器继承自CCComp基类实现了完整的冷却逻辑和生命周期管理。
#### 主要属性和方法
| 属性/方法 | 类型 | 描述 | 默认值 |
|-----------|------|------|--------|
| HeroView | any | 关联的HeroView组件 | null |
| HeroEntity | any | 关联的实体对象 | null |
| TALCOMP | any | 技能组件 | null |
| skill_cd | number | 全局技能冷却 | 0 |
| _timers | object | 定时器集合 | {} |
#### 生命周期管理
```mermaid
sequenceDiagram
participant Init as 初始化阶段
participant Load as 加载阶段
participant Start as 启动阶段
participant Update as 更新循环
participant Destroy as 销毁阶段
Init->>Load : init()
Load->>Start : onLoad()
Start->>Update : start()
Update->>Update : update(dt)
Update->>Destroy : onDestroy()
Destroy->>Init : clear_timer()
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L20-L35)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L160-L177)
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L1-L177)
### HeroViewComp组件
HeroViewComp作为技能状态的承载者提供了技能列表管理和状态查询功能。
#### 技能状态管理
| 状态属性 | 类型 | 描述 | 用途 |
|----------|------|------|------|
| skills | array | 当前技能列表 | 存储技能配置和冷却状态 |
| mp | number | 当前魔法值 | 冷却触发条件之一 |
| is_atking | boolean | 是否处于攻击状态 | 影响技能触发时机 |
**节来源**
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L80-L90)
## 冷却机制实现
### 帧累加冷却计算
技能冷却采用基于update循环的帧累加机制每帧将deltaTime累加到技能的当前冷却时间上。
#### 冷却计算流程
```mermaid
flowchart TD
Start([update循环开始]) --> CheckState{检查游戏状态}
CheckState --> |非战斗状态| Skip[跳过冷却计算]
CheckState --> |战斗状态| CheckStun{检查眩晕状态}
CheckStun --> |眩晕中| Skip
CheckStun --> |清醒状态| GetSkills[获取技能列表]
GetSkills --> LoopSkills[遍历技能数组]
LoopSkills --> AddDeltaTime[累加dt到cd]
AddDeltaTime --> CheckConditions{检查触发条件}
CheckConditions --> |cd > cd_max且mp充足| TriggerSkill[触发技能]
CheckConditions --> |不满足条件| NextSkill[下一个技能]
TriggerSkill --> ResetCD[重置冷却时间]
ResetCD --> DeductMP[扣除魔法值]
DeductMP --> NextSkill
NextSkill --> MoreSkills{还有更多技能?}
MoreSkills --> |是| LoopSkills
MoreSkills --> |否| End([循环结束])
Skip --> End
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L37-L55)
#### 双重条件判断逻辑
冷却触发采用严格的双重条件判断机制:
1. **冷却时间条件**`skills[i].cd > skills[i].cd_max`
2. **魔法值条件**`this.HeroView.mp >= skills[i].cost`
只有当这两个条件同时满足时,技能才会被触发。
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L42-L50)
### 技能触发机制
#### 触发条件详解
```mermaid
flowchart TD
A[技能触发检查] --> B{cd > cd_max?}
B --> |否| C[继续冷却]
B --> |是| D{mp >= cost?}
D --> |否| C
D --> |是| E{技能类型检查}
E --> |伤害技能| F{is_atking?}
E --> |其他技能| G[直接触发]
F --> |是| H{SType.damage?}
F --> |否| C
H --> |是| I[触发技能]
H --> |否| C
G --> I
I --> J[重置cd=0]
J --> K[扣除mp]
K --> L[播放特效]
L --> M[执行技能逻辑]
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L42-L50)
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L57-L65)
## 状态管理与控制流
### 游戏状态检测
系统通过多个层面的状态检测来决定是否进行冷却计算:
#### 状态检查层次
| 检查层级 | 检查内容 | 实现位置 | 作用 |
|----------|----------|----------|------|
| Mission状态 | `smc.mission.play` | update方法 | 检查整体游戏状态 |
| Pause状态 | `smc.mission.pause` | update方法 | 检查暂停状态 |
| 状态异常 | `this.HeroView.isStun()` | update方法 | 检查眩晕状态 |
| 状态异常 | `this.HeroView.isFrost()` | update方法 | 检查冰冻状态 |
#### 状态检测流程
```mermaid
sequenceDiagram
participant Update as update循环
participant SM as smc.mission
participant HeroView as HeroView
participant Skills as 技能列表
Update->>SM : 检查play状态
SM-->>Update : 返回状态
Update->>SM : 检查pause状态
SM-->>Update : 返回状态
Update->>HeroView : 检查眩晕状态
HeroView-->>Update : 返回状态
Update->>HeroView : 检查冰冻状态
HeroView-->>Update : 返回状态
Update->>Skills : 获取技能列表
Skills-->>Update : 返回技能数组
Update->>Update : 执行冷却计算
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L37-L40)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L465-L470)
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L37-L40)
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L465-L470)
### 非战斗状态处理
在非战斗状态下,系统会完全跳过冷却计算,确保资源的有效利用:
#### 非战斗状态判断
```typescript
// 非战斗状态检查逻辑
if(!smc.mission.play || smc.mission.pause) return
```
这种设计避免了在菜单界面、加载画面等非战斗状态下进行不必要的计算开销。
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L37-L40)
## 定时器资源管理
### 定时器生命周期
SkillConComp实现了完整的定时器生命周期管理包括创建、使用和清理三个阶段。
#### 定时器管理架构
```mermaid
graph LR
subgraph "定时器生命周期"
A[创建定时器] --> B[存储到_timers]
B --> C[技能执行]
C --> D[定时器回调]
D --> E[清理定时器]
E --> F[内存释放]
end
subgraph "清理策略"
G[战斗结束] --> H[clear_timer]
I[组件销毁] --> J[onDestroy]
K[手动重置] --> L[reset]
end
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L15-L16)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L150-L155)
#### 定时器清理机制
系统提供了多种定时器清理机制:
| 清理方式 | 触发条件 | 实现方法 | 作用范围 |
|----------|----------|----------|----------|
| 战斗结束清理 | GameEvent.FightEnd | clear_timer() | 所有定时器 |
| 组件销毁清理 | onDestroy() | onDestroy() | 当前组件 |
| 手动重置清理 | reset() | reset() | 当前组件 |
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L150-L155)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L160-L177)
### 资源释放过程
#### 定时器释放流程
```mermaid
sequenceDiagram
participant Event as 战斗结束事件
participant Comp as SkillConComp
participant Timer as 定时器系统
participant Memory as 内存管理
Event->>Comp : GameEvent.FightEnd
Comp->>Comp : clear_timer()
Comp->>Timer : Object.values(_timers).forEach(clearTimeout)
Timer->>Memory : 释放定时器资源
Memory-->>Timer : 确认释放
Timer-->>Comp : 清理完成
Comp->>Comp : _timers = {}
Note over Comp,Memory : 组件销毁时自动清理
Comp->>Comp : onDestroy()
Comp->>Timer : 再次清理所有定时器
Comp->>Comp : 重置_timers对象
```
**图表来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L150-L155)
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L160-L177)
**节来源**
- [SkillConComp.ts](file://assets/script/game/hero/SkillConComp.ts#L150-L177)
## 最佳实践指南
### 暂停状态下的冷却处理
在游戏暂停状态下,系统会完全跳过冷却计算,这是推荐的最佳实践:
#### 暂停状态处理
```typescript
// 推荐的暂停状态处理
update(dt: number) {
if(!smc.mission.play || smc.mission.pause) return
// 冷却计算逻辑
}
```
这种方式确保了在暂停状态下不会产生额外的计算开销,同时保持了冷却状态的准确性。
### 战斗结束后的清理
战斗结束后必须及时清理定时器资源,避免内存泄漏:
#### 战斗结束清理
```typescript
// 战斗结束事件监听
init(): void {
this.on(GameEvent.FightEnd, this.clear_timer, this);
}
// 清理方法实现
public clear_timer() {
Object.values(this._timers).forEach(clearTimeout);
}
```
### 非play状态的处理
在非战斗状态(如菜单界面)下,系统会自动跳过冷却计算:
#### 非play状态检查
```typescript
// 非play状态检查
if(!smc.mission.play || smc.mission.pause) return
```
这种设计确保了系统只在需要的时候进行计算,提高了整体性能。
## 故障排除
### 常见问题及解决方案
#### 问题1技能冷却不准确
**症状**:技能冷却时间显示不正确
**原因**update循环被中断或dt值异常
**解决方案**
- 检查smc.mission.play状态
- 验证dt参数的有效性
- 确保update方法正常执行
#### 问题2定时器泄漏
**症状**:内存使用持续增长
**原因**:定时器未正确清理
**解决方案**
- 在onDestroy中清理定时器
- 确保GameEvent.FightEnd事件正确触发
- 检查定时器ID的唯一性
#### 问题3技能无法触发
**症状**:技能达到冷却时间但不触发
**原因**:多重条件判断失败
**解决方案**
- 检查mp值是否足够
- 验证is_atking状态
- 确认技能配置正确
### 调试技巧
#### 冷却状态监控
```typescript
// 添加调试日志
update(dt: number) {
console.log(`冷却状态 - play: ${smc.mission.play}, pause: ${smc.mission.pause}`);
console.log(`技能冷却 - 当前mp: ${this.HeroView.mp}`);
}
```
#### 定时器状态检查
```typescript
// 定时器状态监控
public debugTimers() {
console.log('当前定时器:', Object.keys(this._timers));
console.log('定时器数量:', Object.values(this._timers).length);
}
```
## 总结
SkillConComp技能冷却管理系统是一个设计精良的组件具有以下特点
### 核心优势
1. **精确的帧累加机制**基于update循环的冷却计算确保了时间精度
2. **多重条件判断**:双条件触发机制保证了技能使用的合理性
3. **完善的生命周期管理**:从创建到销毁的完整资源管理
4. **灵活的状态控制**:支持各种游戏状态下的冷却处理
### 设计亮点
- **模块化架构**:清晰的职责分离和组件交互
- **资源优化**:智能的状态检查避免不必要的计算
- **错误处理**:完善的异常情况处理机制
- **扩展性**:良好的接口设计支持功能扩展
### 性能考虑
系统通过智能的状态检查和条件判断,有效减少了不必要的计算开销,在保证功能完整性的同时维持了良好的性能表现。
这个技能冷却管理系统为游戏提供了稳定可靠的技能控制机制,是游戏战斗系统的重要组成部分。