refactor(hero): 重构技能系统变量命名和自动施法系统

将技能相关变量名从skillId改为s_uuid以提高一致性
重命名自动施法系统文件并优化目标选择方法命名
删除旧版自动施法系统文件,新增重构后的实现
This commit is contained in:
2025-10-31 09:00:37 +08:00
parent 2f19433a0a
commit a1c605238d
4 changed files with 39 additions and 44 deletions

View File

@@ -37,14 +37,14 @@ export class HeroSkillsComp extends ecs.Comp {
/**
* 初始化技能列表
* @param skillIds 技能配置ID数组
* @param sUuids 技能配置ID数组
*/
initSkills(skillIds: number[]) {
initSkills(sUuids: number[]) {
this.skills = [];
for (const skillId of skillIds) {
const config = SkillSet[skillId];
for (const s_uuid of sUuids) {
const config = SkillSet[s_uuid];
if (!config) {
console.warn(`[HeroSkills] 技能配置不存在: ${skillId}`);
console.warn(`[HeroSkills] 技能配置不存在: ${s_uuid}`);
continue;
}
@@ -61,10 +61,10 @@ export class HeroSkillsComp extends ecs.Comp {
/**
* 添加单个技能
*/
addSkill(skillId: number) {
const config = SkillSet[skillId];
addSkill(s_uuid: number) {
const config = SkillSet[s_uuid];
if (!config) {
console.warn(`[HeroSkills] 技能配置不存在: ${skillId}`);
console.warn(`[HeroSkills] 技能配置不存在: ${s_uuid}`);
return;
}

View File

@@ -21,8 +21,8 @@ import { HeroSkillsComp } from "./HeroSkills";
* - AI系统复用
* - AI策略
*/
@ecs.register('SkillAutocastSystem')
export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
@ecs.register('SACastSystem')
export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
filter(): ecs.IMatcher {
return ecs.allOf(HeroSkillsComp, HeroAttrsComp, HeroViewComp);
@@ -55,7 +55,7 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
// ✅ 添加施法请求标记组件
const request = e.add(CastSkillRequestComp) as CastSkillRequestComp;
request.skillIndex = skillIndex;
request.targetPositions = this.selectTargets(heroView);
request.targets = this.sTargets(heroView);
// 一次只施放一个技能
break;
@@ -65,7 +65,7 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
/**
*
*/
private selectTargets(caster: HeroViewComp): Vec3[] {
private sTargets(caster: HeroViewComp): Vec3[] {
// 简化版:选择最前方的敌人
const targets: Vec3[] = [];
@@ -82,7 +82,7 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
/**
*
*/
private selectDamageTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
private sDamageTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
const targets: Vec3[] = [];
const heroModel = caster.ent.get(HeroAttrsComp);
if (!heroModel) return targets;
@@ -97,7 +97,7 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
// 如果没有找到敌人,使用默认位置
if (targets.length === 0) {
targets.push(...this.selectDefaultTargets(caster, heroModel.fac));
targets.push(...this.sDefaultTargets(caster, heroModel.fac));
}
return targets;
@@ -106,7 +106,7 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
/**
*
*/
private selectHealTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
private sHealTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
const targets: Vec3[] = [];
const heroModel = caster.ent.get(HeroAttrsComp);
if (!heroModel) return targets;
@@ -129,23 +129,23 @@ export class SkillAutocastSystem extends ecs.ComblockSystem implements ecs.ISyst
/**
* BUFF技能目标
*/
private selectBuffTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
private sBuffTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
// BUFF技能通常施放在自己或友军身上
return this.selectHealTargets(caster, config, maxTargets);
return this.sHealTargets(caster, config, maxTargets);
}
/**
* DEBUFF技能目标
*/
private selectDebuffTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
private sDebuffTargets(caster: HeroViewComp, config: any, maxTargets: number): Vec3[] {
// DEBUFF技能通常施放在敌人身上
return this.selectDamageTargets(caster, config, maxTargets);
return this.sDamageTargets(caster, config, maxTargets);
}
/**
*
*/
private selectDefaultTargets(caster: HeroViewComp, faction: number): Vec3[] {
private sDefaultTargets(caster: HeroViewComp, faction: number): Vec3[] {
const targets: Vec3[] = [];
const defaultX = faction === 0 ? 400 : -400;
targets.push(v3(defaultX, 0, 0));

View File

@@ -6,9 +6,10 @@ import { HeroViewComp } from "./HeroViewComp";
import { SkillSet, SType } from "../common/config/SkillSet";
import { SkillEnt } from "../skill/SkillEnt";
import { HeroSkillsComp } from "./HeroSkills";
import { Skill } from "../skill/Skill";
/**
* ==================== 技能施法系统 ====================
* ==================== 技能施法系统 手动施法====================
*
* 职责:
* 1. 监听 CastSkillRequestComp 标记组件
@@ -37,20 +38,20 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
* 实体进入时触发(即请求施法时)
*/
entityEnter(e: ecs.Entity): void {
const skillsData = e.get(HeroSkillsComp);
const skills = e.get(HeroSkillsComp);
const heroModel = e.get(HeroAttrsComp);
const request = e.get(CastSkillRequestComp);
const heroView = e.get(HeroViewComp);
// 1. 验证数据完整性
if (!skillsData || !heroModel || !request || !heroView) {
if (!skills || !heroModel || !request || !heroView) {
console.warn("[SkillCastSystem] 数据不完整,取消施法");
e.remove(CastSkillRequestComp);
return;
}
// 2. 获取技能数据
const skill = skillsData.getSkill(request.skillIndex);
const skill = skills.getSkill(request.skillIndex);
if (!skill) {
console.warn(`[SkillCastSystem] 技能索引无效: ${request.skillIndex}`);
e.remove(CastSkillRequestComp);
@@ -58,17 +59,17 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
}
// 3. 检查施法条件
if (!this.checkCastConditions(skillsData, heroModel, request.skillIndex)) {
if (!this.checkCastConditions(skills, heroModel, request.skillIndex)) {
e.remove(CastSkillRequestComp);
return;
}
// 4. 执行施法
this.executeCast(e, skill, request.targetPositions, heroView);
this.executeCast(e, skill, request.targets, heroView);
// 5. 扣除资源和重置CD
heroModel.mp -= skill.cost;
skillsData.resetCD(request.skillIndex);
skills.resetCD(request.skillIndex);
// 6. 移除请求标记
e.remove(CastSkillRequestComp);
@@ -77,7 +78,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
/**
* 检查施法条件
*/
private checkCastConditions(skillsData: HeroSkillsComp, heroModel: HeroAttrsComp, skillIndex: number): boolean {
private checkCastConditions(skills: HeroSkillsComp, heroModel: HeroAttrsComp, skillIndex: number): boolean {
// 检查角色状态
if (heroModel.is_dead) {
return false;
@@ -89,7 +90,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
}
// 检查CD和MP
if (!skillsData.canCast(skillIndex, heroModel.mp)) {
if (!skills.canCast(skillIndex, heroModel.mp)) {
return false;
}
@@ -99,7 +100,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
/**
* 执行施法
*/
private executeCast(casterEntity: ecs.Entity, skill: any, targetPositions: Vec3[], heroView: HeroViewComp) {
private executeCast(casterEntity: ecs.Entity, skill: any, targets: Vec3[], heroView: HeroViewComp) {
const config = SkillSet[skill.uuid];
if (!config) {
console.error("[SkillCastSystem] 技能配置不存在:", skill.uuid);
@@ -112,7 +113,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
// 2. 延迟创建技能实体(等待动画)
const delay = config.with ?? 0.3; // 施法前摇时间
heroView.scheduleOnce(() => {
this.createSkillEntity(skill.uuid, heroView, targetPositions);
this.createSkill(skill.uuid, heroView, targets);
}, delay);
const heroModel = casterEntity.get(HeroAttrsComp);
@@ -122,7 +123,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
/**
* 创建技能实体
*/
private createSkillEntity(skillId: number, caster: HeroViewComp, targetPositions: Vec3[]) {
private createSkill(skillId: number, caster: HeroViewComp, targets: Vec3[]) {
// 检查节点有效性
if (!caster.node || !caster.node.isValid) {
console.warn("[SkillCastSystem] 施法者节点无效");
@@ -137,13 +138,7 @@ export class SkillCastSystem extends ecs.ComblockSystem implements ecs.IEntityEn
}
// ✅ 使用现有的 SkillEnt 创建技能
const skillEnt = ecs.getEntity<SkillEnt>(SkillEnt);
skillEnt.load(
caster.node.position, // 起始位置
parent, // 父节点
skillId, // 技能ID
targetPositions, // 目标位置数组
caster, // 施法者
);
// const skill = ecs.getEntity<Skill>(Skill);
}
}

View File

@@ -15,11 +15,11 @@ export class CastSkillRequestComp extends ecs.Comp {
skillIndex: number = 0;
/** 目标位置数组(由请求者提供) */
targetPositions: Vec3[] = [];
targets: Vec3[] = [];
reset() {
this.skillIndex = 0;
this.targetPositions = [];
this.targets = [];
}
}
/**
@@ -33,7 +33,7 @@ export class CastSkillRequestComp extends ecs.Comp {
@ecs.register('SkillHit')
export class SkillHitComp extends ecs.Comp {
/** 技能ID */
skillId: number = 0;
s_uuid: number = 0;
/** 命中位置 */
hitPosition: Vec3 = new Vec3();
@@ -45,7 +45,7 @@ export class SkillHitComp extends ecs.Comp {
casterId: number = 0;
reset() {
this.skillId = 0;
this.s_uuid = 0;
this.hitPosition.set(0, 0, 0);
this.damage = 0;
this.casterId = 0;