summaryrefslogtreecommitdiffstats
path: root/src/view/input/DirectionHandler.ts
blob: 691ba01d393e6e49800a94ba74dc37c7a322ef34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {Listenable} from '../../util';
import InputHandler from './InputHandler';

import {vec2} from 'gl-matrix';

export const enum Keycode {
	Left = 37,
	Up = 38,
	Right = 39,
	Down = 40,
}

export default 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);
		});
	}
}