英雄 位移 完成
This commit is contained in:
@@ -2,7 +2,7 @@ import { HeroViewComp } from "../../../hero/HeroViewComp";
|
||||
import { BattleMoveComp } from "./BattleMoveComp";
|
||||
import { ecs } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { smc } from "../../SingletonModuleComp";
|
||||
import { BoxSet, FacSet } from "../../config/BoxSet";
|
||||
import { FacSet } from "../../config/BoxSet";
|
||||
import { oops } from "db://oops-framework/core/Oops";
|
||||
import { GameEvent } from "../../config/GameEvent";
|
||||
import { FightSet } from "../../config/Mission";
|
||||
@@ -39,6 +39,84 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
|
||||
view.status_change("idle");
|
||||
return;
|
||||
}
|
||||
|
||||
// 英雄阵营特殊逻辑:检查是否有敌人存在
|
||||
if (view.fac == FacSet.HERO) {
|
||||
const hasEnemies = this.checkEnemiesExist(e);
|
||||
const inAttackRange = view.is_atking;
|
||||
|
||||
// 如果有敌人,继续前进
|
||||
if (hasEnemies) {
|
||||
// 找到最近的敌人,调整移动方向
|
||||
const nearestEnemy = this.findNearestEnemy(e);
|
||||
if (nearestEnemy) {
|
||||
const enemyX = nearestEnemy.node.position.x;
|
||||
const currentX = view.node.position.x;
|
||||
|
||||
// 根据敌人位置调整移动方向和朝向
|
||||
if (enemyX > currentX) {
|
||||
move.direction = 1; // 向右移动
|
||||
view.node.setScale(1, 1, 1); // 面向右侧
|
||||
} else {
|
||||
move.direction = -1; // 向左移动
|
||||
view.node.setScale(-1, 1, 1); // 面向左侧
|
||||
}
|
||||
|
||||
// 继续向敌人方向移动
|
||||
const delta = ((view.speed-view.DEBUFF_SLOW)/3) * this.dt * move.direction;
|
||||
const newX = view.node.position.x + delta;
|
||||
|
||||
// 对于英雄,允许更自由的移动范围
|
||||
if (newX >= -420 && newX <= 420) { // 使用地图边界
|
||||
view.status_change("move");
|
||||
view.node.setPosition(newX, view.node.position.y, 0);
|
||||
} else {
|
||||
view.status_change("idle");
|
||||
}
|
||||
}
|
||||
this.checkEnemiesInBase(e);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有敌人,回到targetX
|
||||
if (!hasEnemies) {
|
||||
const currentX = view.node.position.x;
|
||||
const targetX = move.targetX;
|
||||
|
||||
// 如果不在目标位置,移动到目标位置
|
||||
if (Math.abs(currentX - targetX) > 1) {
|
||||
// 确定移动方向:如果当前位置在目标位置右侧,向左移动;否则向右移动
|
||||
const direction = currentX > targetX ? -1 : 1;
|
||||
const delta = ((view.speed-view.DEBUFF_SLOW)/3) * this.dt * direction;
|
||||
const newX = view.node.position.x + delta;
|
||||
|
||||
// 设置朝向
|
||||
if (direction === 1) {
|
||||
view.node.setScale(1, 1, 1); // 面向右侧
|
||||
} else {
|
||||
view.node.setScale(-1, 1, 1); // 面向左侧
|
||||
}
|
||||
|
||||
// 确保不会超过目标位置
|
||||
if (direction === 1 && newX > targetX) {
|
||||
view.node.setPosition(targetX, view.node.position.y, 0);
|
||||
} else if (direction === -1 && newX < targetX) {
|
||||
view.node.setPosition(targetX, view.node.position.y, 0);
|
||||
} else {
|
||||
view.node.setPosition(newX, view.node.position.y, 0);
|
||||
}
|
||||
view.status_change("move");
|
||||
} else {
|
||||
view.status_change("idle");
|
||||
// 回到targetX后,面向右侧
|
||||
move.direction = 1;
|
||||
view.node.setScale(1, 1, 1); // 面向右侧
|
||||
}
|
||||
this.checkEnemiesInBase(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 计算移动量
|
||||
const delta = ((view.speed-view.DEBUFF_SLOW)/3) * this.dt * move.direction;
|
||||
const newX = view.node.position.x + delta;
|
||||
@@ -63,6 +141,41 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
|
||||
// console.log(`[${view.hero_name}] 类型:${view.type} 是否停止:${shouldStop} 方向:${move.direction} 位置:${view.node.position.x.toFixed(1)}`);
|
||||
}
|
||||
|
||||
/** 检查是否存在敌人 */
|
||||
private checkEnemiesExist(entity: ecs.Entity): boolean {
|
||||
const team = entity.get(HeroViewComp).fac;
|
||||
let hasEnemies = false;
|
||||
ecs.query(ecs.allOf(HeroViewComp)).some(e => {
|
||||
const view = e.get(HeroViewComp);
|
||||
if (view.fac !== team && !view.is_dead) {
|
||||
hasEnemies = true;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return hasEnemies;
|
||||
}
|
||||
|
||||
/** 找到最近的敌人 */
|
||||
private findNearestEnemy(entity: ecs.Entity): HeroViewComp | null {
|
||||
const currentPos = entity.get(HeroViewComp).node.position;
|
||||
const team = entity.get(HeroViewComp).fac;
|
||||
let nearestEnemy: HeroViewComp | null = null;
|
||||
let minDistance = Infinity;
|
||||
|
||||
ecs.query(ecs.allOf(HeroViewComp)).forEach(e => {
|
||||
const view = e.get(HeroViewComp);
|
||||
if (view.fac !== team && !view.is_dead) {
|
||||
const distance = Math.abs(currentPos.x - view.node.position.x);
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
nearestEnemy = view;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return nearestEnemy;
|
||||
}
|
||||
|
||||
/** 验证目标位置有效性 */
|
||||
private validatePosition(newX: number, move: BattleMoveComp): boolean {
|
||||
// 我方不能超过右边界,敌方不能超过左边界
|
||||
@@ -76,7 +189,7 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
|
||||
const view = entity.get(HeroViewComp);
|
||||
return view.node.position.x === -1000 || view.node.position.x === 1000;
|
||||
}
|
||||
/**驾车敌人是否进入我放基地 */
|
||||
/**检测敌人是否进入我方基地 */
|
||||
private checkEnemiesInBase(entity: ecs.Entity) {
|
||||
const view = entity.get(HeroViewComp);
|
||||
if(view.fac==FacSet.MON){
|
||||
|
||||
Reference in New Issue
Block a user