Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
handleInventoryEvent(evt) {
let { keyCode } = evt
// evt.preventDefault()
const exit = [ROT.VK_ESCAPE, ROT.VK_I]
const confirm = [ROT.VK_RETURN, ROT.VK_E]
const drop = [ROT.VK_D]
const up = [ROT.VK_UP, ROT.VK_NUMPAD8, ROT.VK_W, ROT.VK_K]
const down = [ROT.VK_DOWN, ROT.VK_NUMPAD2, ROT.VK_S, ROT.VK_J]
const left = [ROT.VK_LEFT, ROT.VK_NUMPAD4, ROT.VK_H]
const right = [ROT.VK_RIGHT, ROT.VK_NUMPAD6, ROT.VK_L]
const ul = [ROT.VK_NUMPAD7, ROT.VK_Y]
const lr = [ROT.VK_NUMPAD3, ROT.VK_N]
const ur = [ROT.VK_NUMPAD9, ROT.VK_U]
const ll = [ROT.VK_NUMPAD1, ROT.VK_B]
/* If the key event isn't repeated within the last 160 milliseconds (too soon), then we proceed but we keep track of this
key movement time */
if (evt.type === 'keydown' && up.concat(down).includes(keyCode)) {
if (this.keyTimer === null) {
this.keyTimer = new Date()
} else {
let start = this.keyTimer.getTime()
let now = new Date().getTime()
if (!(now - start >= 100)) {
return
} else {
// we've waited long enough, but add another timer just in case
this.keyTimer = new Date()
}
document.body.addEventListener('keydown', (e) => {
const offsets = {
[ROT.VK_W]: { x: 0, y: -1 },
[ROT.VK_S]: { x: 0, y: 1 },
[ROT.VK_A]: { x: -1, y: 0 },
[ROT.VK_D]: { x: 1, y: 0 },
[ROT.VK_NUMPAD8]: { x: 0, y: -1 },
[ROT.VK_NUMPAD2]: { x: 0, y: 1 },
[ROT.VK_NUMPAD4]: { x: -1, y: 0 },
[ROT.VK_NUMPAD6]: { x: 1, y: 0 },
[ROT.VK_NUMPAD7]: { x: -1, y: -1 },
[ROT.VK_NUMPAD3]: { x: 1, y: 1 },
[ROT.VK_NUMPAD1]: { x: -1, y: 1 },
[ROT.VK_NUMPAD9]: { x: 1, y: -1 }
};
if(!offsets[e.keyCode] || !GameState.manualMove) return;
const player = GameState.players[0];
player.moveTo(player.x+offsets[e.keyCode].x, player.y+offsets[e.keyCode].y);
GameState.game.engine.unlock();
});
};