-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlHandler.js
113 lines (103 loc) · 2.96 KB
/
ControlHandler.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import PlayerHandler from './PlayerHandler.js';
export default {
a: false,
d: false,
s: false,
space: false,
mouseLeft: false,
mouseRight: false,
mouseX: 0,
mouseY: 0,
init(game) {
this.game = game;
this.canvas = game.canvas;
this.kdelistener = this.keyDownEvent.bind(this);
this.kuelistener = this.keyUpEvent.bind(this);
this.mdelistener = this.mouseDownEvent.bind(this);
this.muelistener = this.mouseUpEvent.bind(this);
this.mmelistener = this.mouseMoveEvent.bind(this);
this.scrolllistener = this.scrollEvent.bind(this);
},
enterFrame() {},
scrollEvent(e) {
if (e.deltaY > 0) {
PlayerHandler.wheel(-100);
} else if (e.deltaY < 0) {
PlayerHandler.wheel(100);
} else {
}
},
keyDownEvent(e) {
if (e.keyCode == 32) {
this.space = true;
} else if (e.keyCode == 65) {
this.a = true;
} else if (e.keyCode == 68) {
this.d = true;
} else if (e.keyCode == 83) {
this.s = true;
} else if (e.keyCode == 87) {
this.space = true;
} else if (e.keyCode == 88) {
this.mouseLeft = true;
} else if (e.keyCode == 27) {
if (this.game.state == 'game') {
this.game.state = 'paused';
} else if (this.game.state == 'paused') {
this.game.state = 'game';
}
} else if (
(e.keyCode >= 48 || e.keyCode <= 57) &&
this.game.state == 'game'
) {
PlayerHandler.hotKey(e.keyCode);
}
if (e.ctrlKey) {
return true;
} else {
e.preventDefault();
return false;
}
},
keyUpEvent(e) {
if (e.keyCode == 32) {
this.space = false;
} else if (e.keyCode == 65) {
this.a = false;
} else if (e.keyCode == 68) {
this.d = false;
} else if (e.keyCode == 87) {
this.space = false;
} else if (e.keyCode == 88) {
this.mouseLeft = false;
} else if (e.keyCode == 83) {
this.s = false;
} else if (e.keyCode == 40) {
PlayerHandler.wheel(-100);
} else if (e.keyCode == 38) {
PlayerHandler.wheel(100);
}
if (e.ctrlKey) {
return true;
} else {
e.preventDefault();
return false;
}
},
mouseDownEvent(e) {
this.mouseLeft = e.button == 0;
this.mouseRight = e.button == 2;
},
mouseUpEvent(e) {
if (e.button == 0) {
this.mouseLeft = false;
} else if (e.button == 2) {
this.mouseRight = false;
}
},
mouseMoveEvent(e) {
var rect = this.canvas.getBoundingClientRect();
this.mouseX = e.clientX - rect.left;
this.mouseY = e.clientY - rect.top;
},
};