summaryrefslogtreecommitdiffstats
path: root/src/view
diff options
context:
space:
mode:
Diffstat (limited to 'src/view')
-rw-r--r--src/view/input/directionhandler.ts37
-rw-r--r--src/view/input/gameinput.ts78
-rw-r--r--src/view/input/inputhandler.ts24
3 files changed, 90 insertions, 49 deletions
diff --git a/src/view/input/directionhandler.ts b/src/view/input/directionhandler.ts
deleted file mode 100644
index 0a3687e..0000000
--- a/src/view/input/directionhandler.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { InputHandler } from './inputhandler';
-
-import { Listenable } from '../../util';
-
-import { vec2 } from 'gl-matrix';
-
-export const enum Keycode {
- Left = 37,
- Up = 38,
- Right = 39,
- Down = 40,
-}
-
-export class DirectionHandler extends Listenable<[vec2]> {
- private readonly input: InputHandler;
-
- constructor() {
- super();
-
- this.input = new InputHandler(new Set([Keycode.Left, Keycode.Up, Keycode.Right, Keycode.Down]));
-
- this.input.addListener(() => {
- const dir = vec2.create();
-
- if (this.input.has(Keycode.Left))
- vec2.add(dir, dir, [-1, 0]);
- if (this.input.has(Keycode.Up))
- vec2.add(dir, dir, [0, -1]);
- if (this.input.has(Keycode.Right))
- vec2.add(dir, dir, [1, 0]);
- if (this.input.has(Keycode.Down))
- vec2.add(dir, dir, [0, 1]);
-
- this.runListeners(dir);
- });
- }
-}
diff --git a/src/view/input/gameinput.ts b/src/view/input/gameinput.ts
new file mode 100644
index 0000000..f3066e3
--- /dev/null
+++ b/src/view/input/gameinput.ts
@@ -0,0 +1,78 @@
+import { InputHandler } from './inputhandler';
+
+import { Listenable } from '../../util';
+
+import { vec2 } from 'gl-matrix';
+
+export enum ButtonCode {
+ Action,
+ Back,
+ Menu,
+}
+
+const buttonMapping: {[key: string]: ButtonCode} = {
+ KeyZ: ButtonCode.Action,
+ KeyX: ButtonCode.Back,
+ KeyC: ButtonCode.Menu,
+};
+
+export interface DirectionInput {
+ type: 'direction';
+ direction: vec2;
+}
+
+export interface ButtonInput {
+ type: 'button';
+ button: ButtonCode;
+}
+
+export type GameInput = DirectionInput | ButtonInput;
+
+export class GameInputHandler extends Listenable<[GameInput]> {
+ private readonly input: InputHandler;
+
+ constructor() {
+ super();
+
+ this.input = new InputHandler(
+ new Set([
+ 'ArrowLeft',
+ 'ArrowUp',
+ 'ArrowRight',
+ 'ArrowDown',
+ ...Object.keys(buttonMapping),
+ ]));
+
+ this.input.addListener((key: string, pressed: boolean) => {
+ const button = buttonMapping[key];
+ if (button !== undefined) {
+ if (pressed)
+ this.runListeners({
+ type: 'button',
+ button,
+ });
+
+ return;
+ }
+
+ const dir = vec2.create();
+
+ if (this.input.has('ArrowLeft'))
+ vec2.add(dir, dir, [-1, 0]);
+ if (this.input.has('ArrowUp'))
+ vec2.add(dir, dir, [0, -1]);
+ if (this.input.has('ArrowRight'))
+ vec2.add(dir, dir, [1, 0]);
+ if (this.input.has('ArrowDown'))
+ vec2.add(dir, dir, [0, 1]);
+
+ if (vec2.sqrLen(dir) > 0)
+ vec2.normalize(dir, dir);
+
+ this.runListeners({
+ type: 'direction',
+ direction: dir,
+ });
+ });
+ }
+}
diff --git a/src/view/input/inputhandler.ts b/src/view/input/inputhandler.ts
index 96f15dd..22fdc28 100644
--- a/src/view/input/inputhandler.ts
+++ b/src/view/input/inputhandler.ts
@@ -1,39 +1,39 @@
import { Listenable } from '../../util';
-export class InputHandler extends Listenable<[]> {
- private readonly keys: Set<number> = new Set();
+export class InputHandler extends Listenable<[string, boolean]> {
+ private readonly keys: Set<string> = new Set();
- constructor(relevantKeys: Set<number>) {
+ constructor(relevantKeys: Set<string>) {
super();
window.addEventListener('keydown', (ev) => {
- if (!relevantKeys.has(ev.keyCode))
+ if (!relevantKeys.has(ev.code))
return;
ev.preventDefault();
- if (this.keys.has(ev.keyCode))
+ if (ev.repeat)
return;
- this.keys.add(ev.keyCode);
- this.runListeners();
+ this.keys.add(ev.code);
+ this.runListeners(ev.code, true);
});
window.addEventListener('keyup', (ev) => {
- if (!relevantKeys.has(ev.keyCode))
+ if (!relevantKeys.has(ev.code))
return;
ev.preventDefault();
- if (!this.keys.has(ev.keyCode))
+ if (!this.keys.has(ev.code))
return;
- this.keys.delete(ev.keyCode);
- this.runListeners();
+ this.keys.delete(ev.code);
+ this.runListeners(ev.code, false);
});
}
- public has(key: number): boolean {
+ public has(key: string): boolean {
return this.keys.has(key);
}
}