fix: 后退 & 移动速度

This commit is contained in:
unanmed 2024-10-03 20:16:25 +08:00
parent c6ec4971ee
commit 0b734762b0
3 changed files with 72 additions and 13 deletions

View File

@ -222,6 +222,7 @@ export class HeroRenderer
*
*/
private step() {
this.stepDir = this.moveDir;
this.lastStepTime = Date.now();
this.stepDelta = core.utils.scan2[this.stepDir];
this.turn(this.stepDir);

View File

@ -61,6 +61,8 @@ export abstract class ObjectMoverBase extends EventEmitter<EObjectMovingEvent> {
moveDir: Dir2 = 'down';
/** 当前是否正在移动 */
moving: boolean = false;
/** 面朝方向 */
faceDir: Dir2 = 'down';
/** 当前的控制对象 */
controller?: IMoveController;
@ -101,12 +103,43 @@ export abstract class ObjectMoverBase extends EventEmitter<EObjectMovingEvent> {
controller: IMoveController
): Promise<void>;
/**
*
* @param speed
* @param controller
*/
protected abstract onSetMoveSpeed(
speed: number,
controller: IMoveController
): void;
private getMoveDir(move: Move2): Dir2 {
if (move === 'forward') return this.moveDir;
if (move === 'backward') return backDir(this.moveDir);
if (move === 'backward') return backDir(this.faceDir);
return move;
}
private getFaceDir(move: Move2): Dir2 {
if (move === 'forward' || move === 'backward') return this.faceDir;
return move;
}
/**
*
* @param dir
*/
setFaceDir(dir: Dir2) {
if (!this.moving) this.faceDir = dir;
}
/**
*
* @param dir
*/
setMoveDir(dir: Dir2) {
if (!this.moving) this.moveDir = dir;
}
/**
* {@link insertMove}
* {@link IMoveController.push}
@ -130,12 +163,14 @@ export abstract class ObjectMoverBase extends EventEmitter<EObjectMovingEvent> {
if (!step) break;
if (step.type === 'dir') {
this.moveDir = this.getMoveDir(step.value);
this.faceDir = this.getFaceDir(step.value);
const code = await this.onStepStart(step, controller);
await this.onStepEnd(step, code, controller);
} else {
const replay = core.status.replay.speed;
const speed = replay === 24 ? 1 : step.value / replay;
this.moveSpeed = speed;
this.onSetMoveSpeed(speed, controller);
}
this.emit('stepEnd', step);
}
@ -329,6 +364,11 @@ export class BlockMover extends ObjectMoverBase {
controller: IMoveController
): Promise<void> {}
protected onSetMoveSpeed(
speed: number,
controller: IMoveController
): void {}
private moveAnimate(step: MoveStepDir) {
const layer = this.layerItems[0];
if (!layer) return;
@ -451,15 +491,15 @@ export class HeroMover extends ObjectMoverBase {
step: MoveStepDir,
controller: IMoveController
): Promise<HeroMoveCode> {
const showDir = toDir(this.moveDir);
const showDir = toDir(this.faceDir);
core.setHeroLoc('direction', showDir);
const adapter = HeroMover.adapter;
adapter?.sync('setAnimateDir', showDir);
const { x, y } = core.status.hero.loc;
const { x: nx, y: ny } = this.nextLoc(x, y, this.moveDir);
if (this.autoSave) this.checkAutoSave(x, y, nx, ny);
if (this.autoSave && !this.ignoreTerrain) {
this.checkAutoSave(x, y, nx, ny);
}
if (!this.inLockControl && core.status.lockControl) {
controller.stop();
@ -537,6 +577,7 @@ export class HeroMover extends ObjectMoverBase {
core.setHeroLoc('y', ny, true);
}
if (!this.ignoreTerrain) {
const direction = core.getHeroLoc('direction');
core.control._moveAction_popAutomaticRoute();
if (!this.noRoute) core.status.route.push(direction);
@ -545,6 +586,13 @@ export class HeroMover extends ObjectMoverBase {
core.checkRouteFolding();
}
}
}
protected onSetMoveSpeed(speed: number, controller: IMoveController): void {
const adapter = HeroMover.adapter;
if (!adapter) return;
adapter.sync('setMoveSpeed', speed);
}
/**
*
@ -563,6 +611,7 @@ export class HeroMover extends ObjectMoverBase {
const viewport = HeroMover.viewport;
if (!adapter || !viewport) return;
viewport.all('moveTo', x, y);
adapter.sync('setAnimateDir', showDir);
await adapter.all('move', moveDir);
}

View File

@ -57,7 +57,7 @@ export function init() {
else {
if (type === 'speed') moveSteps.push(v);
else {
moveSteps.push(...Array(number).fill(type));
moveSteps.push(...Array(Number(number)).fill(type));
}
}
});
@ -149,10 +149,19 @@ export function init() {
const start: MoveStep = { type: 'speed', value: time };
heroMover.insertMove(...[start, ...resolved]);
const controller = heroMover.startMove();
controller?.onEnd.then(() => {
const controller = heroMover.startMove(true, true, true, false);
if (!controller) {
callback?.();
return;
}
controller.onEnd.then(() => {
callback?.();
});
const animate = fallbackIds++;
core.animateFrame.lastAsyncId = animate;
core.animateFrame.asyncId[animate] = controller.stop;
};
control.prototype.setHeroLoc = function (