From 27ad7784c9a8a4f37ea1d078a0a1d3f890463085 Mon Sep 17 00:00:00 2001 From: walkpan Date: Fri, 2 Jan 2026 23:38:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(HeroMove):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E6=9C=9D=E5=90=91=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 简化英雄移动系统,删除currentFacing缓存和setFacing方法 --- assets/script/game/hero/HeroMove.ts | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/assets/script/game/hero/HeroMove.ts b/assets/script/game/hero/HeroMove.ts index bfbcf85a..94c5bac4 100644 --- a/assets/script/game/hero/HeroMove.ts +++ b/assets/script/game/hero/HeroMove.ts @@ -16,14 +16,11 @@ export class HeroMoveComp extends ecs.Comp { targetX: number = 0; /** 是否处于移动状态 */ moving: boolean = true; - /** 当前朝向缓存:避免频繁setScale */ - currentFacing: number = 1; reset() { this.direction = 1; this.targetX = 0; this.moving = true; - this.currentFacing = 1; } } @@ -75,10 +72,8 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd // 根据敌人位置调整移动方向和朝向 if (enemyX > currentX) { move.direction = 1; // 向右移动 - this.setFacing(view, move, 1); // 🔥 使用优化的转向方法 } else { move.direction = -1; // 向左移动 - this.setFacing(view, move, -1); // 🔥 使用优化的转向方法 } // 继续向敌人方向移动 @@ -112,9 +107,6 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd const delta = (model.Attrs[Attrs.SPEED]/3) * this.dt * direction; const newX = view.node.position.x + delta; - // 🔥 使用优化的转向方法 - this.setFacing(view, move, direction); - // 确保不会超过目标位置 if (direction === 1 && newX > finalTargetX) { view.node.setPosition(finalTargetX, view.node.position.y, 0); @@ -128,7 +120,6 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd view.status_change("idle"); // 到达目标位置后,面向右侧(敌人方向) move.direction = 1; - this.setFacing(view, move, 1); // 🔥 使用优化的转向方法 } } else { view.status_change("idle"); @@ -136,24 +127,6 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd } } - /** - * 🔥 优化的转向方法:只在真正需要改变朝向时才调用setScale - * 避免频繁的setScale调用导致碰撞器重新计算 - */ - private setFacing(view: HeroViewComp, move: HeroMoveComp, newFacing: number) { - // 只有当朝向真正改变时才更新 - if (move.currentFacing !== newFacing) { - move.currentFacing = newFacing; - view.node.setScale(newFacing*view.node.scale.x, 1*view.node.scale.y); - - // 安全获取top节点 - const topNode = view.node.getChildByName("top"); - if (topNode) { - topNode.setScale(newFacing*topNode.scale.x, 1*topNode.scale.y); - } - } - } - /** 检查是否存在敌人 */ private checkEnemiesExist(entity: ecs.Entity): boolean { const team = entity.get(HeroAttrsComp).fac;