-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
64 lines (61 loc) · 2.15 KB
/
input.js
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
export class Input{
constructor(game){
this.game = game;
this.keys = [];
this.lastKey = "";
this.touchX = 0;
this.touchY = 0;
this.touchThreshold = 50;
//developer
//button
this.keypressed = false;
this.buttons = document.querySelectorAll("button");
this.buttons.forEach(btn=>{
btn.addEventListener('touchstart', (e)=>{
if(this.keys.indexOf(e.target.id) === -1){
this.keys.unshift(e.target.id);
this.lastKey = e.target.id;
this.keypressed = true;
}
});
btn.addEventListener ('touchend', (e)=>{
if(this.keys.indexOf(e.target.id) > -1){
this.keys.splice(this.keys.indexOf(e.target.id), 1);
this.lastKey = "";
this.keypressed = false;
}
})
btn.addEventListener('mousedown', (e)=>{
if(this.keys.indexOf(e.target.id) === -1){
this.keys.unshift(e.target.id);
this.lastKey = e.target.id;
this.keypressed = true;
}
});
btn.addEventListener ('mouseup', (e)=>{
if(this.keys.indexOf(e.target.id) > -1){
this.keys.splice(this.keys.indexOf(e.target.id), 1);
this.lastKey = "";
this.keypressed = false;
}
})
})
//arrows
window.addEventListener('keydown', (e)=>{
if(this.keys.indexOf(e.key) === -1){
this.keys.unshift(e.key);
this.lastKey = e.key;
this.keypressed = true;
}
})
window.addEventListener('keyup', (e)=>{
if(this.keys.indexOf(e.key) > -1){
this.keys.splice(this.keys.indexOf(e.key), 1);
this.lastKey = "";
this.keypressed = false;
}
//debug settings
if(e.key=== "d") this.game.debug = !this.game.debug;
})
}
}