refactor(MonMove): 重构怪物移动系统,实现基于职业类型的智能战术
将怪物移动逻辑拆分为近战、中程和远程三种策略 优化状态检查和移动逻辑分发 移除不必要的渲染层级更新
This commit is contained in:
@@ -5,6 +5,8 @@ import { HeroSkillsComp } from "./HeroSkills";
|
|||||||
import { smc } from "../common/SingletonModuleComp";
|
import { smc } from "../common/SingletonModuleComp";
|
||||||
import { FacSet, IndexSet } from "../common/config/GameSet";
|
import { FacSet, IndexSet } from "../common/config/GameSet";
|
||||||
import { Attrs } from "../common/config/HeroAttrs";
|
import { Attrs } from "../common/config/HeroAttrs";
|
||||||
|
import { HType } from "../common/config/heroSet";
|
||||||
|
import { SkillRange } from "../common/config/SkillSet";
|
||||||
|
|
||||||
/** 怪物移动组件 */
|
/** 怪物移动组件 */
|
||||||
@ecs.register('MonMove')
|
@ecs.register('MonMove')
|
||||||
@@ -40,8 +42,9 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
|
|||||||
}
|
}
|
||||||
|
|
||||||
update(e: ecs.Entity) {
|
update(e: ecs.Entity) {
|
||||||
if (!smc.mission.play ) return;
|
// 1. 全局状态检查
|
||||||
if(smc.mission.pause) return
|
if (!smc.mission.play || smc.mission.pause) return;
|
||||||
|
|
||||||
const view = e.get(HeroViewComp);
|
const view = e.get(HeroViewComp);
|
||||||
|
|
||||||
// 如果英雄死亡(停止怪物行动标志为true),则停止怪物移动
|
// 如果英雄死亡(停止怪物行动标志为true),则停止怪物移动
|
||||||
@@ -52,169 +55,213 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
|
|||||||
|
|
||||||
const move = e.get(MonMoveComp);
|
const move = e.get(MonMoveComp);
|
||||||
const model = e.get(HeroAttrsComp);
|
const model = e.get(HeroAttrsComp);
|
||||||
// const view = e.get(HeroViewComp);
|
|
||||||
|
|
||||||
// 只处理怪物
|
// 只处理怪物
|
||||||
if (model.fac !== FacSet.MON) return;
|
if (model.fac !== FacSet.MON) return;
|
||||||
if (!move.moving) return;
|
if (!move.moving) return;
|
||||||
|
|
||||||
const shouldStopInFace = this.checkEnemiesInFace(e);
|
// 2. 异常状态检查 (死亡/复活/眩晕/冰冻)
|
||||||
const shouldStopAtMinRange = this.shouldStopAtMinSkillRange(e);
|
if (model.is_stop || model.is_dead || model.isStun() || model.isFrost()) {
|
||||||
let shouldStop = shouldStopInFace || shouldStopAtMinRange;
|
view.status_change("idle");
|
||||||
model.is_atking = this.checkEnemiesInSkillRange(e);
|
return;
|
||||||
|
|
||||||
// x > 280 强制移动,允许攻击
|
|
||||||
if (view.node.position.x > 280) {
|
|
||||||
shouldStop = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 移除渲染层级更新:各线路固定,后召唤的天然层级更高,无需动态调整
|
this.updateRenderOrder(e);
|
||||||
|
|
||||||
if (!shouldStop) {
|
// 检查是否需要y轴靠近
|
||||||
if (model.is_stop || model.is_dead || model.isStun() || model.isFrost()) {
|
this.checkAndSetTargetY(e);
|
||||||
view.status_change("idle");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查是否需要y轴靠近
|
// 3. 核心移动逻辑分发
|
||||||
this.checkAndSetTargetY(e);
|
const nearestEnemy = this.findNearestEnemy(e);
|
||||||
|
|
||||||
// 怪物移动逻辑:同时向目标x和y方向移动
|
if (nearestEnemy) {
|
||||||
const deltaX = (model.Attrs[Attrs.SPEED]/3) * this.dt * move.direction;
|
// 战斗状态:根据职业类型和rangeType执行智能战术
|
||||||
const newX = view.node.position.x + deltaX;
|
this.processCombatLogic(e, move, view, model, nearestEnemy);
|
||||||
|
} else {
|
||||||
|
// 非战斗状态:向目标移动
|
||||||
|
this.processMarchLogic(e, move, view, model);
|
||||||
|
model.is_atking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 战斗移动逻辑分发
|
||||||
|
* 根据 rangeType 决定走位策略
|
||||||
|
*/
|
||||||
|
private processCombatLogic(e: ecs.Entity, move: MonMoveComp, view: HeroViewComp, model: HeroAttrsComp, enemy: HeroViewComp) {
|
||||||
|
// 优先使用 rangeType 判断,如果没有则回退到 type 判断
|
||||||
|
let rangeType = model.rangeType;
|
||||||
|
|
||||||
|
// 兼容性处理:如果数据未配置 rangeType,根据旧的职业类型推断
|
||||||
|
if (rangeType === undefined) {
|
||||||
|
if (model.type === HType.warrior || model.type === HType.assassin) {
|
||||||
|
rangeType = SkillRange.Melee;
|
||||||
|
} else if (model.type === HType.remote) {
|
||||||
|
rangeType = SkillRange.Long;
|
||||||
|
} else {
|
||||||
|
rangeType = SkillRange.Mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (rangeType) {
|
||||||
|
case SkillRange.Melee:
|
||||||
|
this.processMeleeLogic(e, move, view, model, enemy);
|
||||||
|
break;
|
||||||
|
case SkillRange.Mid:
|
||||||
|
this.processMidLogic(e, move, view, model, enemy);
|
||||||
|
break;
|
||||||
|
case SkillRange.Long:
|
||||||
|
this.processLongLogic(e, move, view, model, enemy);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
this.processMidLogic(e, move, view, model, enemy); // 默认中程
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 近战逻辑 (Melee)
|
||||||
|
* 策略:无脑突进,贴脸输出
|
||||||
|
* 范围:< 75 (攻击距离)
|
||||||
|
*/
|
||||||
|
private processMeleeLogic(e: ecs.Entity, move: MonMoveComp, view: HeroViewComp, model: HeroAttrsComp, enemy: HeroViewComp) {
|
||||||
|
const currentX = view.node.position.x;
|
||||||
|
const enemyX = enemy.node.position.x;
|
||||||
|
const dist = Math.abs(currentX - enemyX);
|
||||||
|
const attackRange = 75; // 保持原有的近战判定
|
||||||
|
|
||||||
|
move.direction = enemyX > currentX ? 1 : -1;
|
||||||
|
|
||||||
|
if (dist <= attackRange) {
|
||||||
|
view.status_change("idle");
|
||||||
|
model.is_atking = true;
|
||||||
|
} else {
|
||||||
|
const speed = model.Attrs[Attrs.SPEED] / 3;
|
||||||
|
this.moveEntity(view, move.direction, speed);
|
||||||
|
model.is_atking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中程逻辑 (Mid)
|
||||||
|
* 策略:保持在中距离,灵活输出
|
||||||
|
* 范围:120 - 360
|
||||||
|
*/
|
||||||
|
private processMidLogic(e: ecs.Entity, move: MonMoveComp, view: HeroViewComp, model: HeroAttrsComp, enemy: HeroViewComp) {
|
||||||
|
const currentX = view.node.position.x;
|
||||||
|
const enemyX = enemy.node.position.x;
|
||||||
|
const dist = Math.abs(currentX - enemyX);
|
||||||
|
|
||||||
|
const minRange = 120;
|
||||||
|
const maxRange = 360;
|
||||||
|
|
||||||
|
move.direction = enemyX > currentX ? 1 : -1;
|
||||||
|
|
||||||
|
if (dist < minRange) {
|
||||||
|
// 太近了,后撤
|
||||||
|
this.performRetreat(view, move, model, currentX);
|
||||||
|
} else if (dist > maxRange) {
|
||||||
|
// 太远了,追击
|
||||||
|
const speed = model.Attrs[Attrs.SPEED] / 3;
|
||||||
|
this.moveEntity(view, move.direction, speed);
|
||||||
|
model.is_atking = false;
|
||||||
|
} else {
|
||||||
|
// 距离合适,站桩输出
|
||||||
|
view.status_change("idle");
|
||||||
|
model.is_atking = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 远程逻辑 (Long)
|
||||||
|
* 策略:保持在远距离,最大化生存
|
||||||
|
* 范围:360 - 720
|
||||||
|
*/
|
||||||
|
private processLongLogic(e: ecs.Entity, move: MonMoveComp, view: HeroViewComp, model: HeroAttrsComp, enemy: HeroViewComp) {
|
||||||
|
const currentX = view.node.position.x;
|
||||||
|
const enemyX = enemy.node.position.x;
|
||||||
|
const dist = Math.abs(currentX - enemyX);
|
||||||
|
|
||||||
|
const minRange = 360;
|
||||||
|
const maxRange = 720;
|
||||||
|
|
||||||
|
move.direction = enemyX > currentX ? 1 : -1;
|
||||||
|
|
||||||
|
if (dist < minRange) {
|
||||||
|
// 太近了,后撤 (远程单位对距离更敏感)
|
||||||
|
this.performRetreat(view, move, model, currentX);
|
||||||
|
} else if (dist > maxRange) {
|
||||||
|
// 太远了,追击
|
||||||
|
const speed = model.Attrs[Attrs.SPEED] / 3;
|
||||||
|
this.moveEntity(view, move.direction, speed);
|
||||||
|
model.is_atking = false;
|
||||||
|
} else {
|
||||||
|
// 距离合适,站桩输出
|
||||||
|
view.status_change("idle");
|
||||||
|
model.is_atking = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 执行后撤逻辑 */
|
||||||
|
private performRetreat(view: HeroViewComp, move: MonMoveComp, model: HeroAttrsComp, currentX: number) {
|
||||||
|
const safeRetreatX = currentX - move.direction * 50;
|
||||||
|
// 怪物活动范围通常宽一些
|
||||||
|
if (safeRetreatX >= -450 && safeRetreatX <= 450) {
|
||||||
|
const retreatSpeed = (model.Attrs[Attrs.SPEED] / 3) * 0.8;
|
||||||
|
this.moveEntity(view, -move.direction, retreatSpeed);
|
||||||
|
model.is_atking = false;
|
||||||
|
} else {
|
||||||
|
// 退无可退,被迫反击
|
||||||
|
view.status_change("idle");
|
||||||
|
model.is_atking = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进军逻辑 (无敌人时)
|
||||||
|
* 策略:向目标X移动 (通常是屏幕左侧)
|
||||||
|
*/
|
||||||
|
private processMarchLogic(e: ecs.Entity, move: MonMoveComp, view: HeroViewComp, model: HeroAttrsComp) {
|
||||||
|
const currentX = view.node.position.x;
|
||||||
|
const targetX = move.targetX;
|
||||||
|
|
||||||
|
if (Math.abs(currentX - targetX) > 5) {
|
||||||
|
const dir = targetX > currentX ? 1 : -1;
|
||||||
|
const speed = model.Attrs[Attrs.SPEED] / 3;
|
||||||
|
|
||||||
let newY = view.node.position.y;
|
// 修正朝向
|
||||||
if (move.targetY !== 0) {
|
move.direction = dir;
|
||||||
const deltaY = (model.Attrs[Attrs.SPEED]/3) * this.dt * Math.sign(move.targetY - newY);
|
|
||||||
newY = newY + deltaY;
|
|
||||||
if (Math.abs(newY - move.targetY) < Math.abs(deltaY)) {
|
|
||||||
newY = move.targetY;
|
|
||||||
move.targetY = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 限制移动范围
|
this.moveEntity(view, dir, speed);
|
||||||
if (this.validatePosition(newX, move)) {
|
|
||||||
view.status_change("move");
|
|
||||||
view.node.setPosition(newX, newY, 0);
|
|
||||||
} else {
|
|
||||||
view.status_change("idle");
|
|
||||||
move.moving = false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
view.status_change("idle");
|
view.status_change("idle");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 验证目标位置有效性 */
|
/** 通用移动执行 */
|
||||||
private validatePosition(newX: number, move: MonMoveComp): boolean {
|
private moveEntity(view: HeroViewComp, direction: number, speed: number) {
|
||||||
// 我方不能超过右边界,敌方不能超过左边界
|
const delta = speed * this.dt * direction;
|
||||||
return move.direction === 1 ?
|
const newX = view.node.position.x + delta;
|
||||||
newX <= move.targetX :
|
|
||||||
newX >= move.targetX;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 检测攻击范围内敌人 */
|
|
||||||
private checkEnemiesInRange(entity: ecs.Entity, range: number): boolean {
|
|
||||||
const currentView = entity.get(HeroViewComp);
|
|
||||||
if (!currentView || !currentView.node) return false;
|
|
||||||
|
|
||||||
const currentPos = currentView.node.position;
|
// 处理Y轴移动
|
||||||
const team = entity.get(HeroAttrsComp).fac;
|
const move = view.ent.get(MonMoveComp);
|
||||||
let found = false;
|
let newY = view.node.position.y;
|
||||||
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
|
if (move && move.targetY !== 0) {
|
||||||
const model = e.get(HeroAttrsComp);
|
const deltaY = speed * this.dt * Math.sign(move.targetY - newY);
|
||||||
const view = e.get(HeroViewComp);
|
newY = newY + deltaY;
|
||||||
if (!view || !view.node) return false;
|
if (Math.abs(newY - move.targetY) < Math.abs(deltaY)) {
|
||||||
const distance = Math.abs(currentPos.x - view.node.position.x);
|
newY = move.targetY;
|
||||||
if (model.fac !== team && !model.is_dead) {
|
move.targetY = 0;
|
||||||
if (distance <= range) {
|
|
||||||
found = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 检测技能攻击范围内敌人 */
|
// 地图边界限制
|
||||||
private checkEnemiesInSkillRange(entity: ecs.Entity): boolean {
|
if (newX >= -450 && newX <= 450) {
|
||||||
const currentView = entity.get(HeroViewComp);
|
view.node.setPosition(newX, newY, 0);
|
||||||
const heroAttrs = entity.get(HeroAttrsComp);
|
view.status_change("move");
|
||||||
|
} else {
|
||||||
if (!currentView || !currentView.node || !heroAttrs) return false;
|
view.status_change("idle");
|
||||||
|
}
|
||||||
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);
|
|
||||||
if (!currentView || !currentView.node) return false;
|
|
||||||
|
|
||||||
const currentPos = currentView.node.position;
|
|
||||||
const team = entity.get(HeroAttrsComp).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 <= 75) {
|
|
||||||
found = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
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;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 检查并设置y轴目标位置 */
|
/** 检查并设置y轴目标位置 */
|
||||||
@@ -258,4 +305,35 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
|
|||||||
move.targetY = heroY + direction * 50;
|
move.targetY = heroY + direction * 50;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private findNearestEnemy(entity: ecs.Entity): HeroViewComp | null {
|
||||||
|
const currentView = entity.get(HeroViewComp);
|
||||||
|
if (!currentView?.node) return null;
|
||||||
|
|
||||||
|
const currentPos = currentView.node.position;
|
||||||
|
const myFac = entity.get(HeroAttrsComp).fac;
|
||||||
|
|
||||||
|
let nearest: HeroViewComp | null = null;
|
||||||
|
let minDis = Infinity;
|
||||||
|
|
||||||
|
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).forEach(e => {
|
||||||
|
const m = e.get(HeroAttrsComp);
|
||||||
|
// 找对立阵营且存活的
|
||||||
|
if (m.fac !== myFac && !m.is_dead) {
|
||||||
|
const v = e.get(HeroViewComp);
|
||||||
|
if (v?.node) {
|
||||||
|
const d = Math.abs(currentPos.x - v.node.position.x);
|
||||||
|
if (d < minDis) {
|
||||||
|
minDis = d;
|
||||||
|
nearest = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return nearest;
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateRenderOrder(entity: ecs.Entity) {
|
||||||
|
// 🔥 移除渲染层级更新:各线路固定,后召唤的天然层级更高,无需动态调整
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user