Files
pixelheros/assets/script/game/hero/HeroSkills.ts
panw 3a8f015a78 refactor: 移除调试日志并统一使用日志工具
- 删除多个文件中的 console.log/console.warn/console.error 调试输出
- 将日志输出统一替换为 mLogger 工具,支持调试模式控制
- 清理注释掉的调试代码和空方法体
2026-02-03 16:49:24 +08:00

286 lines
8.3 KiB
TypeScript
Raw Permalink 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.
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { GameEvent } from "../common/config/GameEvent";
import { Attrs } from "../common/config/HeroAttrs";
import { HeroInfo } from "../common/config/heroSet";
import { HSSet, SkillSet } from "../common/config/SkillSet";
import { HeroAttrsComp } from "./HeroAttrsComp";
import { mLogger } from "../common/Logger";
import { _decorator } from "cc";
const { property } = _decorator;
/**
* ==================== 技能槽位数据 ====================
* 单个技能的运行时数据
*/
export interface SkillSlot {
s_uuid: number; // 技能配置ID
cd: number; // 当前CD时间递减
cd_max: number; // 最大CD时间
level: number; // 技能等级(预留)
dis: number; // 攻击距离
hset: HSSet; // 技能设定, 0:普通攻击, 1:一般技能, 2:必杀技
}
/**
* ==================== 英雄技能数据组件 ====================
*
* 职责:
* 1. 存储角色拥有的技能列表
* 2. 管理技能CD状态
* 3. 提供技能查询接口
*
* 设计理念:
* - 只存数据,不含施法逻辑
* - CD 更新由 HSkillSystem 负责
* - 施法判定由 HSkillSystem 负责
*/
@ecs.register('HeroSkills')
export class HeroSkillsComp extends ecs.Comp {
@property({ tooltip: "是否启用调试日志" })
private debugMode: boolean = false;
// ==================== 技能槽位列表 ====================
/** 技能槽位数组最多4个技能 */
skills: Record<number, SkillSlot> = {};
max_auto: boolean = true;
/** AI 检测计时器 */
ai_timer: number = 0;
onLoad() {
}
onDestroy() {
}
// ==================== 辅助方法 ====================
/**
* 初始化技能列表
* @param sUuids 技能配置ID数组
* @param uuid 英雄UUID
* @param entity 实体对象(用于更新技能距离缓存)
*/
initSkills(sUuids: number[], uuid: number) {
this.skills = [];
for (let i = 0; i < sUuids.length; i++) {
const s_uuid = sUuids[i];
const config = SkillSet[s_uuid];
if (!config) {
mLogger.warn(this.debugMode, 'HeroSkills', `[HeroSkills] 技能配置不存在: ${s_uuid}`);
continue;
}
// 第0个技能的 cd_max 取 herosinfo[uuid].as
const cdMax = i === 0 ? HeroInfo[uuid].as : config.cd;
let hset = HSSet.atk;
if(i ===1) hset = HSSet.skill;
if(i ===2) hset = HSSet.max;
this.skills[s_uuid] = {
s_uuid: config.uuid,
cd: 0,
cd_max: cdMax,
level: 1,
dis: Number(config.dis),
hset: hset,
};
}
// 更新技能距离缓存
if (this.ent) {
const attrsComp = this.ent.get(HeroAttrsComp);
if (attrsComp) {
attrsComp.updateSkillDistanceCache(this);
}
}
}
/**
* 添加单个技能
* @param s_uuid 技能配置ID
* @param hset 技能类型
*/
addSkill(s_uuid: number, hset: HSSet=HSSet.skill) {
const config = SkillSet[s_uuid];
if (!config) {
mLogger.warn(this.debugMode, 'HeroSkills', `[HeroSkills] 技能配置不存在: ${s_uuid}`);
return;
}
this.skills[s_uuid] = {
s_uuid: config.uuid,
cd: 0,
cd_max: config.cd,
level: 1,
dis: Number(config.dis),
hset: hset,
};
// 更新技能距离缓存
if (this.ent) {
const attrsComp = this.ent.get(HeroAttrsComp);
if (attrsComp) {
attrsComp.updateSkillDistanceCache(this);
}
}
}
/**
* 获取指定s_uuid的技能
*/
getSkill(s_uuid: number): SkillSlot | null {
return this.skills[s_uuid] ?? null;
}
/**
* 检查技能是否可施放通过s_uuid
* @param s_uuid 技能配置ID
*/
canCast(s_uuid: number): boolean {
const skill = this.getSkill(s_uuid);
if (!skill) return false;
// 检查CD
return skill.cd <= 0;
}
/**
* 重置技能CD开始冷却通过索引
*/
resetCD(s_uuid: number) {
let attrsCom = this.ent.get(HeroAttrsComp);
if (!attrsCom) return;
const skill = this.getSkill(s_uuid);
if (!skill) return;
const speedAttr = skill.hset === HSSet.atk ? Attrs.AS : Attrs.SS;
const rawSpeed = attrsCom.Attrs?.[speedAttr] ?? 0;
const speedBonus = Math.max(-0.9, rawSpeed / 100);
const speedMultiplier = 1 / (1 + speedBonus);
skill.cd = Math.max(0, skill.cd_max * speedMultiplier);
}
/**
* 更新所有技能CD每帧调用
* @param dt 时间增量
*/
updateCDs(dt: number) {
for (const s_uuid in this.skills) {
const skill = this.skills[Number(s_uuid)];
if (skill.cd > 0) {
skill.cd -= dt;
if (skill.cd < 0) {
skill.cd = 0;
}
}
}
}
/**
* 获取所有可施放的技能索引
*/
getReadySkills(): number[] {
const ready: number[] = [];
for (const s_uuid in this.skills) {
if (this.canCast(Number(s_uuid))) {
ready.push(Number(s_uuid));
}
}
return ready;
}
/**
* 检查技能攻击距离是否足够
* @param s_uuid 技能配置ID
* @param distance 目标距离
* @returns 是否在攻击范围内
*/
canReachTarget(s_uuid: number, distance: number): boolean {
const skill = this.getSkill(s_uuid);
if (!skill) {
return false;
}
return distance <= skill.dis;
}
/**
* 获取技能的攻击距离
* @param s_uuid 技能配置ID
* @returns 攻击距离如果技能不存在返回0
*/
getSkillDistance(s_uuid: number): number {
const skill = this.getSkill(s_uuid);
return skill ? skill.dis : 0;
}
/**
* 获取可施放技能中的最远攻击距离
* @returns 最远攻击距离如果没有可用技能返回0
*/
getMaxSkillDistance(): number {
const readySkills = this.getReadySkills();
if (readySkills.length === 0) return 0;
let maxDistance = 0;
for (const s_uuid of readySkills) {
const skill = this.getSkill(s_uuid);
if (skill && skill.dis > maxDistance) {
maxDistance = skill.dis;
}
}
return maxDistance;
}
/**
* 获取可施放技能中的最近攻击距离
* @returns 最近攻击距离如果没有可用技能返回0
*/
getMinSkillDistance(): number {
const readySkills = this.getReadySkills();
if (readySkills.length === 0) return 0;
let minDistance = Number.MAX_VALUE;
for (const s_uuid of readySkills) {
const skill = this.getSkill(s_uuid);
if (skill && skill.dis < minDistance) {
minDistance = skill.dis;
}
}
return minDistance === Number.MAX_VALUE ? 0 : minDistance;
}
/**
* 获取所有技能中的最小攻击距离不考虑MP限制
* 用于移动停止判断,让英雄在合适位置等待回蓝
* @returns 最小攻击距离如果没有技能返回0
*/
getAbsoluteMinSkillDistance(): number {
const skillIds = Object.keys(this.skills).map(Number);
if (skillIds.length === 0) return 0;
let minDistance = Number.MAX_VALUE;
for (const s_uuid of skillIds) {
const skill = this.getSkill(s_uuid);
if (skill && skill.dis < minDistance) {
minDistance = skill.dis;
}
}
return minDistance === Number.MAX_VALUE ? 0 : minDistance;
}
reset() {
this.skills = {};
}
setMaxAuto(on: boolean) {
this.max_auto = on;
}
}