fix: 优化移动手感

This commit is contained in:
unanmed 2024-08-26 21:55:42 +08:00
parent 1c369251b1
commit e5afdb73fd
3 changed files with 46 additions and 41 deletions

View File

@ -86,7 +86,8 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
enabled: boolean = false; enabled: boolean = false;
conditionMap: Map<symbol, () => boolean> = new Map(); conditionMap: Map<symbol, () => boolean> = new Map();
private scope: symbol = Symbol(); /** 当前作用域 */
scope: symbol = Symbol();
private scopeStack: symbol[] = []; private scopeStack: symbol[] = [];
private grouping: string = 'none'; private grouping: string = 'none';
@ -226,6 +227,8 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
// 检查全局启用情况 // 检查全局启用情况
if (!this.enabled) return false; if (!this.enabled) return false;
const when = this.conditionMap.get(this.scope)!; const when = this.conditionMap.get(this.scope)!;
if (type === 'down') this.checkPress(key);
else this.checkPressEnd(key);
if (!when()) return false; if (!when()) return false;
const toEmit = this.keyMap.get(key); const toEmit = this.keyMap.get(key);
if (!toEmit) return false; if (!toEmit) return false;
@ -253,9 +256,6 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
}); });
this.emit('emit', key, assist, type); this.emit('emit', key, assist, type);
if (type === 'down') this.checkPress(key);
else this.checkPressEnd(key);
return emitted; return emitted;
} }

View File

@ -289,4 +289,8 @@ export class LayerOpenDoorAnimate implements ILayerRenderExtends {
awake(layer: Layer) { awake(layer: Layer) {
this.layer = layer; this.layer = layer;
} }
openDoor(x: number, y: number) {}
closeDoor(x: number, y: number) {}
} }

View File

@ -25,45 +25,47 @@ export function init() {
const pressedArrow: Set<Dir> = new Set(); const pressedArrow: Set<Dir> = new Set();
Mota.r(() => { Mota.r(() => {
const gameKey = Mota.require('var', 'gameKey'); const gameKey = Mota.require('var', 'gameKey');
gameKey const { moveUp, moveDown, moveLeft, moveRight } = gameKey.data;
.realize('moveUp', onMoveKeyDown('up'), { type: 'down' }) const symbol = Symbol.for('@key_main');
.realize('moveUp', onMoveKeyUp('up')) gameKey.on('press', code => {
.realize('moveDown', onMoveKeyDown('down'), { type: 'down' }) if (core.status.lockControl || gameKey.scope !== symbol) return;
.realize('moveDown', onMoveKeyUp('down')) if (code === moveUp.key) onMoveKeyDown('up');
.realize('moveLeft', onMoveKeyDown('left'), { type: 'down' }) if (code === moveDown.key) onMoveKeyDown('down');
.realize('moveLeft', onMoveKeyUp('left')) if (code === moveLeft.key) onMoveKeyDown('left');
.realize('moveRight', onMoveKeyDown('right'), { type: 'down' }) if (code === moveRight.key) onMoveKeyDown('right');
.realize('moveRight', onMoveKeyUp('right')); });
gameKey.on('release', code => {
if (code === moveUp.key) onMoveKeyUp('up');
if (code === moveDown.key) onMoveKeyUp('down');
if (code === moveLeft.key) onMoveKeyUp('left');
if (code === moveRight.key) onMoveKeyUp('right');
});
}); });
function onMoveKeyDown(type: Dir) { function onMoveKeyDown(type: Dir) {
return () => { pressedArrow.add(type);
pressedArrow.add(type); moveDir = type;
moveDir = type; if (!moving) {
stepDir = moveDir;
readyMove();
}
if (moving && stopChian) {
stopChian = false;
}
}
function onMoveKeyUp(type: Dir) {
pressedArrow.delete(type);
if (pressedArrow.size === 0) {
stopChian = true;
} else {
const arr = [...pressedArrow];
moveDir = arr[0];
if (!moving) { if (!moving) {
stepDir = moveDir; stepDir = moveDir;
readyMove(); readyMove();
} }
if (moving && stopChian) { }
stopChian = false;
}
};
}
function onMoveKeyUp(type: Dir) {
return () => {
pressedArrow.delete(type);
if (pressedArrow.size === 0) {
stopChian = true;
} else {
const arr = [...pressedArrow];
moveDir = arr[0];
if (!moving) {
stepDir = moveDir;
readyMove();
}
}
};
} }
async function readyMove() { async function readyMove() {
@ -108,11 +110,6 @@ export function init() {
stopChian = true; stopChian = true;
return; return;
} }
if (core.status.lockControl) {
pressedArrow.clear();
stopChian = true;
return;
}
if (checkCanMoveStatus()) { if (checkCanMoveStatus()) {
if (!moving) { if (!moving) {
stepDir = moveDir; stepDir = moveDir;
@ -275,6 +272,10 @@ export function init() {
adapters['hero-adapter']?.all('setImage', img); adapters['hero-adapter']?.all('setImage', img);
}; };
control.prototype.isMoving = function () {
return moving;
};
hook.on('reset', () => { hook.on('reset', () => {
moveDir = core.status.hero.loc.direction; moveDir = core.status.hero.loc.direction;
stepDir = moveDir; stepDir = moveDir;