-
Notifications
You must be signed in to change notification settings - Fork 22
/
keyboard.js
196 lines (176 loc) · 4.35 KB
/
keyboard.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
/** @const */ var P1_KEY_UP = 0x01;
/** @const */ var P1_KEY_DOWN = 0x02;
/** @const */ var P1_KEY_LEFT = 0x04;
/** @const */ var P1_KEY_RIGHT = 0x08;
/** @const */ var P1_KEY_FIRE1 = 0x10;
/** @const */ var P1_KEY_FIRE2 = 0x20;
/** @const */ var P2_KEY_UP = 0x40;
/** @const */ var P2_KEY_DOWN = 0x80;
/** @const */ var P2_KEY_LEFT = 0x01;
/** @const */ var P2_KEY_RIGHT = 0x02;
/** @const */ var P2_KEY_FIRE1 = 0x04;
/** @const */ var P2_KEY_FIRE2 = 0x08;
/** @const */ var KEY_START = 0x40;
/** @const */ var GG_KEY_START = 0x80;
/**
* @constructor
* @param {JSSMS} sms
*/
JSSMS.Keyboard = function(sms) {
this.main = sms;
/**
* Controller 1.
* @type {number}
*/
this.controller1 = 0;
/**
* Controller 2.
* @type {number}
*/
this.controller2 = 0;
/**
* Game Gear start button.
* @type {number}
*/
this.ggstart = 0;
/** Lightgun position. */
/** @type {number} */ this.lightgunX = 0;
/** @type {number} */ this.lightgunY = 0;
/**
* Lightgun button pressed.
* @type {boolean}
*/
this.lightgunClick = false;
/**
* Lightgun is enabled.
* @type {boolean}
*/
this.lightgunEnabled = false;
};
JSSMS.Keyboard.prototype = {
/**
* Reset controllers to default state.
*/
reset: function() {
// Default 0xFF = No Keys Pressed
this.controller1 = 0xff;
this.controller2 = 0xff;
this.ggstart = 0xff;
// Turn lightgun off
if (LIGHTGUN) {
this.lightgunClick = false;
}
this.pause_button = false;
},
/**
* @param {Event} evt A keydown event.
*/
keydown: function(evt) {
switch (evt.keyCode) {
case 38:
this.controller1 &= ~P1_KEY_UP;
break; // Up
case 40:
this.controller1 &= ~P1_KEY_DOWN;
break; // Down
case 37:
this.controller1 &= ~P1_KEY_LEFT;
break; // Left
case 39:
this.controller1 &= ~P1_KEY_RIGHT;
break; // Right
case 88:
this.controller1 &= ~P1_KEY_FIRE1;
break; // X
case 90:
this.controller1 &= ~P1_KEY_FIRE2;
break; // Z
case 13:
//this.controller1 &= ~P1_KEY_START;
if (this.main.is_sms) {
//this.controller2 &= ~0x10; // Reset
this.main.pause_button = true; // Pause
} else {
this.ggstart &= ~GG_KEY_START; // Start
}
break; // Enter
case 104:
this.controller2 &= ~P2_KEY_UP;
break; // Num-8
case 98:
this.controller2 &= ~P2_KEY_DOWN;
break; // Num-2
case 100:
this.controller2 &= ~P2_KEY_LEFT;
break; // Num-4
case 102:
this.controller2 &= ~P2_KEY_RIGHT;
break; // Num-6
case 103:
this.controller2 &= ~P2_KEY_FIRE1;
break; // Num-7
case 105:
this.controller2 &= ~P2_KEY_FIRE2;
break; // Num-9
default:
return; //browser should handle key event
}
evt.preventDefault();
},
/**
* @param {Event} evt A keyup event.
*/
keyup: function(evt) {
switch (evt.keyCode) {
case 38:
this.controller1 |= P1_KEY_UP;
break; // Up
case 40:
this.controller1 |= P1_KEY_DOWN;
break; // Down
case 37:
this.controller1 |= P1_KEY_LEFT;
break; // Left
case 39:
this.controller1 |= P1_KEY_RIGHT;
break; // Right
case 88:
this.controller1 |= P1_KEY_FIRE1;
break; // X
case 90:
this.controller1 |= P1_KEY_FIRE2;
break; // Z
case 13:
//this.controller1 |= P1_KEY_START;
if (!this.main.is_sms) {
// controller2 |= 0x10; // Reset/Start
//else
this.ggstart |= GG_KEY_START; // Start
}
break; // Enter
case 104:
this.controller2 |= P2_KEY_UP;
break; // Num-8
case 98:
this.controller2 |= P2_KEY_DOWN;
break; // Num-2
case 100:
this.controller2 |= P2_KEY_LEFT;
break; // Num-4
case 102:
this.controller2 |= P2_KEY_RIGHT;
break; // Num-6
case 103:
this.controller2 |= P2_KEY_FIRE1;
break; // Num-7
case 105:
this.controller2 |= P2_KEY_FIRE2;
break; // Num-9
default:
return; //browser should handle key event
}
evt.preventDefault();
},
// @todo Add setLightGunPos().
};