diff options
Diffstat (limited to 'src/controls.ts')
-rw-r--r-- | src/controls.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/controls.ts b/src/controls.ts new file mode 100644 index 0000000..b575b3f --- /dev/null +++ b/src/controls.ts @@ -0,0 +1,38 @@ +export interface Controls { + up: boolean, + right: boolean, + down: boolean, + left: boolean, + space: boolean, +} + +export let current = { + up: false, + right: false, + down: false, + left: false, + space: false, +} + +document.addEventListener('keydown', event => { + current = update(current, event.key, true) +}) + +document.addEventListener('keyup', event => { + current = update(current, event.key, false) +}) + +function update(current: Controls, key: string, isDown: boolean): Controls { + if (key === 'ArrowUp') + return { ...current, up: isDown } + else if (key === 'ArrowRight') + return { ...current, right: isDown } + else if (key === 'ArrowDown') + return { ...current, down: isDown } + else if (key === 'ArrowLeft') + return { ...current, left: isDown } + else if (key === ' ') + return { ...current, space: isDown } + else + return current +} |