19 lines
716 B
JavaScript
19 lines
716 B
JavaScript
import { Action } from './model.js'
|
|
|
|
// Key name (as in KeyboardEvent.key) -> direction. A lookup table keeps keyToAction a one-liner.
|
|
const directions = {
|
|
ArrowUp: 'Up', w: 'Up', W: 'Up',
|
|
ArrowDown: 'Down', s: 'Down', S: 'Down',
|
|
ArrowLeft: 'Left', a: 'Left', A: 'Left',
|
|
ArrowRight: 'Right', d: 'Right', D: 'Right',
|
|
}
|
|
|
|
// Keys that are not steering (m10).
|
|
const commands = { ' ': Action.TogglePause, r: Action.Restart, R: Action.Restart }
|
|
|
|
// hasOwn, not `key in table`: "constructor" and "toString" are inherited properties, not keys we mapped.
|
|
export const keyToAction = (key) =>
|
|
Object.hasOwn(directions, key) ? Action.Turn(directions[key])
|
|
: Object.hasOwn(commands, key) ? commands[key]
|
|
: null
|