-
Notifications
You must be signed in to change notification settings - Fork 10
/
input.ts
68 lines (61 loc) · 1.45 KB
/
input.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
type TInput = {
// using numbers so that I can multiply easily without casts
left: number;
right: number;
thrust: number;
skill1: number;
skill2: number;
skill3: number;
};
const input: TInput = {
left: 0,
right: 0,
thrust: 0,
skill1: 0,
skill2: 0,
skill3: 0,
};
type TInputProp = keyof TInput;
const keymap: Record<string, TInputProp> = {
ArrowLeft: "left",
ArrowRight: "right",
ArrowUp: "thrust",
KeyQ: "skill1",
KeyW: "skill2",
KeyE: "skill3",
};
window.addEventListener("keydown", ({ code }) => {
input[keymap[code]] = 1;
});
window.addEventListener("keyup", ({ code }) => {
input[keymap[code]] = 0;
});
// window.addEventListener("touchstart", (e) => {
// e.preventDefault();
// input.skill1 = 1;
// });
// window.addEventListener("touchmove", (e) => {
// e.preventDefault();
// input.skill1 = 0;
// for (let i = 0; i < e.touches.length; ++i) {
// const t = e.touches[i];
// const lq = window.innerWidth / 4,
// rq = window.innerWidth - lq,
// h = 1 - t.clientY / window.innerHeight;
// if (t.clientX < lq) {
// // left
// input.left = 1 - h;
// input.right = 0;
// input.thrust = h;
// }
// if (t.clientX > rq) {
// input.right = 1 - h;
// input.left = 0;
// input.thrust = h;
// }
// }
// });
// window.addEventListener("touchend", (e) => {
// input.left = input.right = input.thrust = input.skill1 = 0;
// });
export default input;