feat(战斗系统): 实现基于技能距离的智能移动和攻击逻辑

重构英雄和怪物移动系统,引入技能距离缓存机制
在HeroAttrsComp中添加技能距离缓存管理
修改HeroSkillsComp以支持技能距离计算
更新移动系统使用技能距离判断攻击时机和停止位置
调整怪物配置统一使用水球技能
This commit is contained in:
2025-11-03 22:59:56 +08:00
parent 914ab0e8b9
commit 8152523e10
9 changed files with 333 additions and 27 deletions

View File

@@ -14,6 +14,7 @@ export interface SkillSlot {
cd_max: number; // 最大CD时间
cost: number; // MP消耗
level: number; // 技能等级(预留)
dis: number; // 攻击距离
}
/**
@@ -41,8 +42,10 @@ export class HeroSkillsComp extends ecs.Comp {
/**
* 初始化技能列表
* @param sUuids 技能配置ID数组
* @param uuid 英雄UUID
* @param entity 实体对象(用于更新技能距离缓存)
*/
initSkills(sUuids: number[], uuid: number) {
initSkills(sUuids: number[], uuid: number, entity?: ecs.Entity) {
this.skills = [];
for (let i = 0; i < sUuids.length; i++) {
const s_uuid = sUuids[i];
@@ -59,14 +62,25 @@ export class HeroSkillsComp extends ecs.Comp {
cd_max: cdMax,
cost: config.cost,
level: 1,
dis: Number(config.dis),
};
}
// 更新技能距离缓存
if (entity) {
const attrsComp = entity.get(HeroAttrsComp);
if (attrsComp) {
attrsComp.updateSkillDistanceCache(this);
}
}
}
/**
* 添加单个技能
* @param s_uuid 技能配置ID
* @param entity 实体对象(用于更新技能距离缓存)
*/
addSkill(s_uuid: number) {
addSkill(s_uuid: number, entity?: ecs.Entity) {
const config = SkillSet[s_uuid];
if (!config) {
console.warn(`[HeroSkills] 技能配置不存在: ${s_uuid}`);
@@ -77,8 +91,17 @@ export class HeroSkillsComp extends ecs.Comp {
cd: 0,
cd_max: config.cd,
cost: config.cost,
level: 1
level: 1,
dis: Number(config.dis),
};
// 更新技能距离缓存
if (entity) {
const attrsComp = entity.get(HeroAttrsComp);
if (attrsComp) {
attrsComp.updateSkillDistanceCache(this);
}
}
}
/**
@@ -156,6 +179,87 @@ export class HeroSkillsComp extends ecs.Comp {
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;
}
/**
* 获取可施放技能中的最远攻击距离
* @param mp 当前MP值
* @returns 最远攻击距离如果没有可用技能返回0
*/
getMaxSkillDistance(mp: number): number {
const readySkills = this.getReadySkills(mp);
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;
}
/**
* 获取可施放技能中的最近攻击距离
* @param mp 当前MP值
* @returns 最近攻击距离如果没有可用技能返回0
*/
getMinSkillDistance(mp: number): number {
const readySkills = this.getReadySkills(mp);
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 = {};
}