From 13e778d072070c5c861115d84117ce42ad14949d Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sun, 28 Jun 2026 16:00:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Mover=20=E5=8F=AF=E4=BB=A5=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=AE=BE=E7=BD=AE=E4=BD=8D=E7=BD=AE=E5=92=8C=E6=9C=9D?= =?UTF-8?q?=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data-base/src/hero/followersController.ts | 10 +-- packages-user/data-base/src/hero/location.ts | 2 +- packages-user/data-common/src/common/mover.ts | 71 ++++++++++++++++++- 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/packages-user/data-base/src/hero/followersController.ts b/packages-user/data-base/src/hero/followersController.ts index 5b00340..e784be1 100644 --- a/packages-user/data-base/src/hero/followersController.ts +++ b/packages-user/data-base/src/hero/followersController.ts @@ -132,12 +132,12 @@ export class HeroFollowersController } gatherFollowersSync(): void { - const targetX = this.heroLocation.x; - const targetY = this.heroLocation.y; - const targetDir = this.heroLocation.mover.faceDirection; + const { x, y } = this.heroLocation; + const dir = this.heroLocation.mover.faceDirection; for (const follower of this.followers) { - follower.location.setPos(targetX, targetY); - follower.location.mover.face(targetDir).start(); + const mover = follower.location.mover; + mover.setPos(x, y); + mover.setFaceDir(dir); } this.forEachHook(hook => { hook.onGatherFollowers?.(true); diff --git a/packages-user/data-base/src/hero/location.ts b/packages-user/data-base/src/hero/location.ts index 206788d..48f666c 100644 --- a/packages-user/data-base/src/hero/location.ts +++ b/packages-user/data-base/src/hero/location.ts @@ -43,6 +43,6 @@ export class HeroLocation implements IHeroLocation { loadState(state: IHeroLocationSave): void { this.x = state.x; this.y = state.y; - this.mover.face(state.direction).start(); + this.mover.setFaceDir(state.direction); } } diff --git a/packages-user/data-common/src/common/mover.ts b/packages-user/data-common/src/common/mover.ts index c062351..d514c18 100644 --- a/packages-user/data-common/src/common/mover.ts +++ b/packages-user/data-common/src/common/mover.ts @@ -205,6 +205,31 @@ export interface IObjectMoverHooks extends IHookBase { tile: T, mover: IObjectMover ): Promise; + + /** + * 当设置移动对象的位置时触发,仅 `setPos` 可以触发 + * @param x 横坐标 + * @param y 纵坐标 + * @param tile 移动器绑定的图块 + * @param mover 当前移动器 + */ + onSetPos?(x: number, y: number, tile: T, mover: IObjectMover): void; + + /** + * 当设置移动对象的朝向时触发,仅 `setFaceDir` 可以触发 + * @param dir 面朝朝向 + * @param tile 移动器绑定的图块 + * @param mover 当前移动器 + */ + onSetFaceDir?(dir: FaceDirection, tile: T, mover: IObjectMover): void; + + /** + * 当设置移动对象的朝向时触发,仅 `setMoveDir` 可以触发 + * @param dir 移动朝向 + * @param tile 移动器绑定的图块 + * @param mover 当前移动器 + */ + onSetMoveDir?(dir: FaceDirection, tile: T, mover: IObjectMover): void; } export interface IObjectMover extends IHookable< @@ -223,6 +248,25 @@ export interface IObjectMover extends IHookable< /** 当前移动速度,单位毫秒 */ readonly currentSpeed: number; + /** + * 立刻设置移动对象的位置 + * @param x 横坐标 + * @param y 纵坐标 + */ + setPos(x: number, y: number): void; + + /** + * 立刻设置移动对象的朝向 + * @param dir 朝向 + */ + setFaceDir(dir: FaceDirection): void; + + /** + * 立刻设置移动对象的移动方向 + * @param dir 朝向 + */ + setMoveDir(dir: FaceDirection): void; + /** * 添加一个瞬移步,到这一步时目标将会瞬移至指定位置 * @param x 瞬移目标横坐标 @@ -424,7 +468,7 @@ export abstract class ObjectMover if (step.direction === ObjectSpecialStep.Backward) { const opposite = this.faceHandler.opposite(dir); this.moveDirection = opposite; - this.faceDirection = opposite; + this.faceDirection = dir; } else { this.moveDirection = dir; this.faceDirection = dir; @@ -440,6 +484,27 @@ export abstract class ObjectMover } } + setPos(x: number, y: number): void { + this.tile.setPos(x, y); + this.forEachHook(hook => { + hook.onSetPos?.(x, y, this.tile, this); + }); + } + + setFaceDir(dir: FaceDirection): void { + this.faceDirection = dir; + this.forEachHook(hook => { + hook.onSetFaceDir?.(dir, this.tile, this); + }); + } + + setMoveDir(dir: FaceDirection): void { + this.moveDirection = dir; + this.forEachHook(hook => { + hook.onSetMoveDir?.(dir, this.tile, this); + }); + } + tp(x: number, y: number, rel: boolean = false): this { this.pushStep({ type: ObjectMoveStepType.Teleport, @@ -561,7 +626,9 @@ export abstract class ObjectMover ); await Promise.all(stepStartHooks); const loc = await this.onStepEnd(code, step, this.tile, controller); - this.tile.setPos(loc.x, loc.y); + if (this.tile.x !== loc.x && this.tile.y !== loc.y) { + this.tile.setPos(loc.x, loc.y); + } const stepEndHooks = this.forEachHook(hook => hook.onStepEnd?.(code, step, this.tile, this) );