forked from JoshuaKGoldberg/Old-Deleted-FullScreenMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.js
224 lines (204 loc) · 6.12 KB
/
triggers.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Triggers.js */
// Keeps track of triggers, which mainly consist of key presses
function resetTriggers() {
// Make the controls object
window.controls = new Controls({
left: [37, 65], // a, left
right: [39, 68], // d, right
up: [38, 87, 32], // w, up
down: [40, 83], // s, down
sprint: [16, 17], // shift, ctrl
pause: [80], // p
mute: [77], // m
qcount: [81] // q
});
// Set the key events on the body
proliferate(body, {
onkeydown: ControlsPipe("keydown", true),
onkeyup: ControlsPipe("keyup", false),
oncontextmenu: contextmenu,
onmousedown: mousedown
});
}
// Hash table for onkeydown and onkeyup
function Controls(pipes) {
// Pipes is a listing of which actions are piped to by which character codes
this.pipes = pipes;
// Actions are piped to the corresponding keydown or keyup via the corresponding ControlsPipe
var keydown = this.keydown = {
// Left
left: function(keys) {
keys.run = -1;
keys.left_down = true;
},
// Right
right: function(keys) {
keys.run = 1;
keys.right_down = true; // independent of changes to mario.keys.run
},
// Up / Jump
up: function(keys) {
keys.up = true;
if(mario.canjump &&/* !mario.crouching &&*/ (mario.resting || map.underwater)) {
keys.jump = 1;
mario.canjump = keys.jumplev = 0;
// To do: can mario make a jumping sound during the spring, and during the pipe cutscenes?
if(mario.power > 1) play("Jump Super");
else play("Jump Small");
if(map.underwater) setTimeout(function() {
mario.jumping = keys.jump = false;
}, timer * 14);
}
},
// Down / Crouch
down: function(keys) {
keys.crouch = true;
},
// Sprint / Fire
sprint: function(keys) {
if(mario.power == 3 && keys.sprint == 0 && !keys.crouch)
mario.fire();
keys.sprint = 1;
},
// Pause
pause: function(keys) {
if(!paused && !(window.editing && !editor.playing))
setTimeout(function() { pause(true); }, 140);
},
// Mute / Unmute
mute: function(keys) {
toggleMute();
},
// qqqqqqq
q: function(keys) {
if(++qcount > 28) maxlulz();
switch(qcount) {
case 7: lulz(); break;
case 14: superlulz(); break;
case 21: hyperlulz(); break;
}
}
};
var keyup = this.keyup = {
// Left
left: function(keys) {
keys.run = 0;
keys.left_down = false;
},
// Right
right: function(keys) {
keys.run = 0;
keys.right_down = false;
},
// Up
up: function(keys) {
if(!map.underwater) keys.jump = keys.up = 0;
mario.canjump = true;
},
// Down
down: function(keys) {
keys.crouch = 0;
removeCrouch();
},
// Spring
sprint: function(keys) {
keys.sprint = 0;
},
// Pause (if held down)
pause: function(keys) {
unpause(true);
},
}
var tag, codes, code, i;
// Map each character code in pipes to the corresponding key event
// For each tag ("up", "down"...)
for(tag in pipes) {
// For each array of character codes, like 38 (up) or 40 (down)
codes = pipes[tag];
for(i in codes) {
code = codes[i];
// That code redirects to the equivalent tag (38 -> "up")
keydown[code] = keydown[tag];
keyup[code] = keyup[tag];
}
}
}
// Generates a pipe to the given name
// For example, ControlsPipe("keydown") pipes to Controls.keydown
function ControlsPipe(name, strict) {
var responses = controls[name];
return function(event) {
if((strict && ((mario && mario.dead) || window.paused)) || window.nokeys) return;
// Allow this to be used as keyup(37) or keyup({which: 37})
if(typeof(event) != "number" || event.which)
event = event.which;
// If there is a known response to this character code, do it
if(responses[event])
responses[event](mario.keys);
// Otherwise only complain if verbosity[name] is true
else mlog(name, "Could not", name, event);
// Record this in the history
window.gamehistory[gamecount] = [keydown, event];
}
}
function keydown(event) {
if((mario && mario.dead) || window.paused || window.nokeys) return;
// Allow this to be used as keyup(37) or keyup({which: 37})
if(typeof(event) != "number" || event.which)
event = event.which;
window.gamehistory[gamecount] = [keydown, event];
}
function keyup(event) {
if(window.nokeys) return;
// Allow this to be used as keyup(37) or keyup({which: 37})
if(typeof(event) != "number" || event.which)
event = event.which;
window.gamehistory[gamecount] = [keyup, event];
}
function contextmenu(event) {
if(event.preventDefault)
event.preventDefault();
}
function mousedown(event) {
// Right click
if(event.which == 3) {
if(paused) unpause();
else if((!window.editor) || (!editing && !editor.playing)) pause(true);
if(event.preventDefault)
event.preventDefault()
}
}
function scriptKeys(oldhistory) {
var i, entry;
for(i in oldhistory) {
entry = oldhistory[i];
addEvent(entry[0], i, entry[1]);
addEvent(function() { alert(entry[0].name + ", " + entry[1]) }, i);
}
}
function lulz(options, timer) {
mario.star = true;
options = options || [Goomba];
timer = timer || 7;
addEventInterval(function() {
if(characters.length > 210) return;
var lul = new Thing(options[randInt(options.length)], randBoolJS(), randBoolJS());
lul.yvel = random() * -unitsizet4;
lul.xvel = lul.speed = random() * unitsizet2 * randSign();
addThing(lul, (32 * random() + 128) * unitsize, (88 * random()) * unitsize);
}, timer, Infinity);
}
function superlulz() {
lulz([Goomba, Koopa, Beetle, HammerBro, Lakitu, Podoboo, Blooper]);
}
function hyperlulz() {
lulz([Bowser], 21);
}
function maxlulz() {
// Sigh....
// window.palette = arrayShuffle(window.palette, 1);
// clearAllSprites(true);
addEventInterval(function(arr) {
setAreaSetting(arr[randInt(arr.length)]);
}, 7, Infinity, ["Overworld", "Underworld", "Underwater", "Sky", "Castle"]);
}