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

@@ -74,7 +74,7 @@ export class Hero extends ecs.Entity {
model.is_master = true;
// ✅ 初始化技能数据(迁移到 HeroSkillsComp
skillsComp.initSkills(hero.skills,uuid);
skillsComp.initSkills(hero.skills, uuid, this);
// 设置基础属性
model.base_ap = hero.ap;

View File

@@ -4,6 +4,7 @@ import { smc } from "../common/SingletonModuleComp";
import { Attrs, AttrsType, BType, NeAttrs } from "../common/config/HeroAttrs";
import { BuffConf, SkillSet } from "../common/config/SkillSet";
import { HeroInfo, AttrSet, HeroUpSet } from "../common/config/heroSet";
import { HeroSkillsComp } from "./HeroSkills";
@ecs.register('HeroAttrs')
@@ -33,6 +34,10 @@ export class HeroAttrsComp extends ecs.Comp {
Attrs: any = []; // 最终属性数组经过Buff计算后
NeAttrs: any = []; // 负面状态数组
// ==================== 技能距离缓存 ====================
maxSkillDistance: number = 0; // 最远技能攻击距离缓存受MP影响
minSkillDistance: number = 0; // 最近技能攻击距离缓存不受MP影响用于停止位置判断
// ==================== Buff/Debuff 系统 ====================
/** 持久型buff数组 - 不会自动过期 */
BUFFS: Record<number, Array<{value: number, BType: BType}>> = {};
@@ -347,6 +352,41 @@ export class HeroAttrsComp extends ecs.Comp {
return this.NeAttrs[NeAttrs.IN_FROST]?.time > 0;
}
// ==================== 技能距离缓存管理 ====================
/**
* 更新技能距离缓存
* 在技能初始化、新增技能、MP变化时调用
* @param skillsComp 技能组件
*/
public updateSkillDistanceCache(skillsComp: HeroSkillsComp): void {
if (!skillsComp) {
this.maxSkillDistance = 0;
this.minSkillDistance = 0;
return;
}
// 最远距离使用当前MP可施放的技能
this.maxSkillDistance = skillsComp.getMaxSkillDistance(this.mp);
// 最近距离使用所有技能中的最小距离不考虑MP限制用于停止位置判断
this.minSkillDistance = skillsComp.getAbsoluteMinSkillDistance();
}
/**
* 获取缓存的最远技能攻击距离
* @returns 最远攻击距离
*/
public getCachedMaxSkillDistance(): number {
return this.maxSkillDistance;
}
/**
* 获取缓存的最近技能攻击距离
* @returns 最近攻击距离
*/
public getCachedMinSkillDistance(): number {
return this.minSkillDistance;
}
reset() {
@@ -370,6 +410,9 @@ export class HeroAttrsComp extends ecs.Comp {
this.NeAttrs = [];
this.BUFFS = {};
this.BUFFS_TEMP = {};
// 重置技能距离缓存
this.maxSkillDistance = 0;
this.minSkillDistance = 0;
this.is_dead = false;
this.is_count_dead = false;
this.is_atking = false;
@@ -457,6 +500,9 @@ export class HeroAttrSystem extends ecs.ComblockSystem
// 1. 更新临时 Buff/Debuff时间递减过期自动移除
model.updateTemporaryBuffsDebuffs(this.dt);
// 记录MP变化前的值
const oldMp = model.mp;
if(this.timer.update(this.dt)){
// 2. HP/MP 自然回复(业务规则)
model.mp += HeroUpSet.MP
@@ -471,6 +517,14 @@ export class HeroAttrSystem extends ecs.ComblockSystem
model.hp = model.Attrs[Attrs.HP_MAX];
}
// 4. 如果MP发生变化更新最大技能距离缓存最小距离不受MP影响
if (model.mp !== oldMp) {
const skillsComp = e.get(HeroSkillsComp);
if (skillsComp) {
model.updateSkillDistanceCache(skillsComp);
}
}
// 每 60 帧输出一次统计
this.frameCount++;
if (this.frameCount % 60 === 0 && this.entityCount === 1) {

View File

@@ -1,6 +1,7 @@
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { HeroViewComp } from "./HeroViewComp";
import { HeroAttrsComp } from "./HeroAttrsComp";
import { HeroSkillsComp } from "./HeroSkills";
import { smc } from "../common/SingletonModuleComp";
import { FacSet } from "../common/config/GameSet";
import { HType } from "../common/config/heroSet";
@@ -30,7 +31,7 @@ export class HeroMoveComp extends ecs.Comp {
@ecs.register('HeroMoveSystem')
export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
filter(): ecs.IMatcher {
return ecs.allOf(HeroMoveComp, HeroViewComp, HeroAttrsComp);
return ecs.allOf(HeroMoveComp, HeroViewComp, HeroAttrsComp, HeroSkillsComp);
}
update(e: ecs.Entity) {
@@ -44,8 +45,10 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
if (model.fac !== FacSet.HERO) return;
if (!move.moving) return;
const shouldStop = this.checkEnemiesInFace(e);
model.is_atking = this.checkEnemiesInRange(e, model.Attrs[Attrs.DIS]);
const shouldStopInFace = this.checkEnemiesInFace(e);
const shouldStopAtMinRange = this.shouldStopAtMinSkillRange(e);
const shouldStop = shouldStopInFace || shouldStopAtMinRange;
model.is_atking = this.checkEnemiesInSkillRange(e);
// 更新渲染层级
this.updateRenderOrder(e);
@@ -236,6 +239,36 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
return found;
}
/** 检测技能攻击范围内敌人 */
private checkEnemiesInSkillRange(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
const heroAttrs = entity.get(HeroAttrsComp);
if (!currentView || !currentView.node || !heroAttrs) return false;
const currentPos = currentView.node.position;
const team = heroAttrs.fac;
// 使用缓存的最远技能攻击距离判断攻击时机
const maxSkillDistance = heroAttrs.getCachedMaxSkillDistance();
if (maxSkillDistance === 0) return false;
let found = false;
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
if (distance <= maxSkillDistance) {
found = true;
return true;
}
}
});
return found;
}
/** 更新渲染层级 */
private updateRenderOrder(entity: ecs.Entity) {
const currentView = entity.get(HeroViewComp);
@@ -283,4 +316,31 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
return distance < occupationRange; // 如果距离小于占用范围,认为被占用
});
}
/** 检查是否应该基于最近技能距离停止移动 */
private shouldStopAtMinSkillRange(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
const heroAttrs = entity.get(HeroAttrsComp);
if (!currentView || !currentView.node || !heroAttrs) return false;
const currentPos = currentView.node.position;
const team = heroAttrs.fac;
// 使用缓存的最近技能攻击距离
const minSkillDistance = heroAttrs.getCachedMinSkillDistance();
if (minSkillDistance === 0) return false;
// 检查是否有敌人在最近技能距离内
return ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
return distance <= minSkillDistance;
}
return false;
});
}
}

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 = {};
}

View File

@@ -88,7 +88,7 @@ export class Monster extends ecs.Entity {
model.Attrs[Attrs.DIS] = hero.dis;
// ✅ 初始化技能数据(迁移到 HeroSkillsComp
skillsComp.initSkills(hero.skills,uuid);
skillsComp.initSkills(hero.skills, uuid, this);
this.add(view);
oops.message.dispatchEvent("monster_load",this)

View File

@@ -1,6 +1,7 @@
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { HeroViewComp } from "./HeroViewComp";
import { HeroAttrsComp } from "./HeroAttrsComp";
import { HeroSkillsComp } from "./HeroSkills";
import { smc } from "../common/SingletonModuleComp";
import { FacSet, IndexSet } from "../common/config/GameSet";
import { Attrs } from "../common/config/HeroAttrs";
@@ -32,7 +33,7 @@ export class MonMoveComp extends ecs.Comp {
@ecs.register('MonMoveSystem')
export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
filter(): ecs.IMatcher {
return ecs.allOf(MonMoveComp, HeroViewComp, HeroAttrsComp);
return ecs.allOf(MonMoveComp, HeroViewComp, HeroAttrsComp, HeroSkillsComp);
}
update(e: ecs.Entity) {
@@ -46,8 +47,10 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
if (model.fac !== FacSet.MON) return;
if (!move.moving) return;
const shouldStop = this.checkEnemiesInFace(e);
model.is_atking = this.checkEnemiesInRange(e, model.Attrs[Attrs.DIS]);
const shouldStopInFace = this.checkEnemiesInFace(e);
const shouldStopAtMinRange = this.shouldStopAtMinSkillRange(e);
const shouldStop = shouldStopInFace || shouldStopAtMinRange;
model.is_atking = this.checkEnemiesInSkillRange(e);
// 🔥 移除渲染层级更新:各线路固定,后召唤的天然层级更高,无需动态调整
@@ -110,6 +113,36 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
return found;
}
/** 检测技能攻击范围内敌人 */
private checkEnemiesInSkillRange(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
const heroAttrs = entity.get(HeroAttrsComp);
if (!currentView || !currentView.node || !heroAttrs) return false;
const currentPos = currentView.node.position;
const team = heroAttrs.fac;
// 使用缓存的最远技能攻击距离判断攻击时机
const maxSkillDistance = heroAttrs.getCachedMaxSkillDistance();
if (maxSkillDistance === 0) return false;
let found = false;
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
if (distance <= maxSkillDistance) {
found = true;
return true;
}
}
});
return found;
}
/** 检测面前是否有敌人 */
private checkEnemiesInFace(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
@@ -132,4 +165,31 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
});
return found;
}
/** 检查是否应该基于最近技能距离停止移动 */
private shouldStopAtMinSkillRange(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
const heroAttrs = entity.get(HeroAttrsComp);
if (!currentView || !currentView.node || !heroAttrs) return false;
const currentPos = currentView.node.position;
const team = heroAttrs.fac;
// 使用缓存的最近技能攻击距离
const minSkillDistance = heroAttrs.getCachedMinSkillDistance();
if (minSkillDistance === 0) return false;
// 检查是否有敌人在最近技能距离内
return ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
return distance <= minSkillDistance;
}
return false;
});
}
}

View File

@@ -54,6 +54,9 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
const config = SkillSet[skill.s_uuid];
if (!config || config.SType !== SType.damage) continue;
// 检查是否有敌人在技能攻击范围内
if (!this.hasEnemyInSkillRange(heroView, heroAttrs, skill.dis)) continue;
// ✅ 开始执行施法
this.startCast(e,skill);
@@ -276,6 +279,31 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
return allies;
}
/**
* 检查技能攻击范围内是否有敌人
*/
private hasEnemyInSkillRange(heroView: HeroViewComp, heroAttrs: HeroAttrsComp, skillDistance: number): boolean {
if (!heroView || !heroView.node) return false;
const currentPos = heroView.node.position;
const team = heroAttrs.fac;
let found = false;
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
if (distance <= skillDistance) {
found = true;
return true;
}
}
});
return found;
}
}