-
Notifications
You must be signed in to change notification settings - Fork 1
/
c-input-handler.js
123 lines (105 loc) · 3 KB
/
c-input-handler.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
114
115
116
117
118
119
120
class InputHandler
{
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Setup
constructor(game, power_up, left_key = 'ArrowLeft', right_key = 'ArrowRight', start_key = 'Space', border_key='KeyB')
{
// Members
this.left_key_ = left_key;
this.right_key_ = right_key;
this.start_key_ = start_key;
this.left_active_ = false;
this.right_active_ = false;
this.game_ = game;
this.power_up_ = this.power_up;
this.start_pressed_ = false;
this.border_key_ = border_key;
// Event listeners
let event_target = this;
document.addEventListener('keydown', function(e) {
event_target.keyDownHandler.call(event_target, e);
});
document.addEventListener('keyup', function(e) {
event_target.keyUpHandler.call(event_target, e);
});
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods for handling keypresses
consumeEvent(e) {
e.preventDefault();
e.stopPropagation();
}
keyDownHandler(e)
{
if(e.code == this.left_key_) {
this.left_active_ = true;
this.consumeEvent(e);
}
else if(e.code == this.right_key_) {
this.right_active_ = true;
this.consumeEvent(e);
}
else if(e.code == this.start_key_)
{
console.log('debug start')
if(this.game_.state_ == 'Lobby')
{
this.game_.requestStartGame();
}
else if(this.game_.state_ =='LobbyGameOver')
{
console.log('debug reset')
this.game_.requestResetGame();
}
this.consumeEvent(e);
}
else if(e.code == this.border_key_)
{
game.power_up_.sendMessageWallInactive();
}
}
keyUpHandler(e)
{
if(e.code == this.left_key_) {
this.left_active_ = false;
this.consumeEvent(e);
}
if(e.code == this.right_key_) {
this.right_active_ = false;
this.consumeEvent(e);
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods for exporting state of keypresses
getDirection()
{
if(this.left_active_ == true && this.right_active_ == false)
return 'left';
if(this.right_active_ == true && this.left_active_ == false)
return 'right';
return 'straight';
}
pollController()
{
var gamepads = navigator.getGamepads();
var controller = gamepads[0];
if(controller != undefined)
{
var axis = controller.axes[2].toFixed(4);
//Move left if axis is lower 0
if(axis < -0.2)
{
this.right_active_ = false;
this.left_active_ = true;
}//Move right if axis is higher 0
else if(axis > 0.2)
{
this.left_active_ = false;
this.right_active_ = true;
} else { // reset for moving in a straight line
this.left_active_ = false;
this.right_active_ = false;
}
}
}
}