fix: Mover 可以直接设置位置和朝向

This commit is contained in:
unanmed 2026-06-28 16:00:59 +08:00
parent abaa5206c5
commit 13e778d072
3 changed files with 75 additions and 8 deletions

View File

@ -132,12 +132,12 @@ export class HeroFollowersController
} }
gatherFollowersSync(): void { gatherFollowersSync(): void {
const targetX = this.heroLocation.x; const { x, y } = this.heroLocation;
const targetY = this.heroLocation.y; const dir = this.heroLocation.mover.faceDirection;
const targetDir = this.heroLocation.mover.faceDirection;
for (const follower of this.followers) { for (const follower of this.followers) {
follower.location.setPos(targetX, targetY); const mover = follower.location.mover;
follower.location.mover.face(targetDir).start(); mover.setPos(x, y);
mover.setFaceDir(dir);
} }
this.forEachHook(hook => { this.forEachHook(hook => {
hook.onGatherFollowers?.(true); hook.onGatherFollowers?.(true);

View File

@ -43,6 +43,6 @@ export class HeroLocation implements IHeroLocation {
loadState(state: IHeroLocationSave): void { loadState(state: IHeroLocationSave): void {
this.x = state.x; this.x = state.x;
this.y = state.y; this.y = state.y;
this.mover.face(state.direction).start(); this.mover.setFaceDir(state.direction);
} }
} }

View File

@ -205,6 +205,31 @@ export interface IObjectMoverHooks<T extends IObjectMovable> extends IHookBase {
tile: T, tile: T,
mover: IObjectMover<T> mover: IObjectMover<T>
): Promise<void>; ): Promise<void>;
/**
* `setPos`
* @param x
* @param y
* @param tile
* @param mover
*/
onSetPos?(x: number, y: number, tile: T, mover: IObjectMover<T>): void;
/**
* `setFaceDir`
* @param dir
* @param tile
* @param mover
*/
onSetFaceDir?(dir: FaceDirection, tile: T, mover: IObjectMover<T>): void;
/**
* `setMoveDir`
* @param dir
* @param tile
* @param mover
*/
onSetMoveDir?(dir: FaceDirection, tile: T, mover: IObjectMover<T>): void;
} }
export interface IObjectMover<T extends IObjectMovable> extends IHookable< export interface IObjectMover<T extends IObjectMovable> extends IHookable<
@ -223,6 +248,25 @@ export interface IObjectMover<T extends IObjectMovable> extends IHookable<
/** 当前移动速度,单位毫秒 */ /** 当前移动速度,单位毫秒 */
readonly currentSpeed: number; 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 * @param x
@ -424,7 +468,7 @@ export abstract class ObjectMover<T extends IObjectMovable>
if (step.direction === ObjectSpecialStep.Backward) { if (step.direction === ObjectSpecialStep.Backward) {
const opposite = this.faceHandler.opposite(dir); const opposite = this.faceHandler.opposite(dir);
this.moveDirection = opposite; this.moveDirection = opposite;
this.faceDirection = opposite; this.faceDirection = dir;
} else { } else {
this.moveDirection = dir; this.moveDirection = dir;
this.faceDirection = dir; this.faceDirection = dir;
@ -440,6 +484,27 @@ export abstract class ObjectMover<T extends IObjectMovable>
} }
} }
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 { tp(x: number, y: number, rel: boolean = false): this {
this.pushStep({ this.pushStep({
type: ObjectMoveStepType.Teleport, type: ObjectMoveStepType.Teleport,
@ -561,7 +626,9 @@ export abstract class ObjectMover<T extends IObjectMovable>
); );
await Promise.all(stepStartHooks); await Promise.all(stepStartHooks);
const loc = await this.onStepEnd(code, step, this.tile, controller); 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 => const stepEndHooks = this.forEachHook(hook =>
hook.onStepEnd?.(code, step, this.tile, this) hook.onStepEnd?.(code, step, this.tile, this)
); );