-
Notifications
You must be signed in to change notification settings - Fork 9
/
rumble.js
46 lines (37 loc) · 1.65 KB
/
rumble.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
(function rumbleMacro() {
// makes your devices vibrate.
// usage: <<rumble 100>> <<rumble 100, 200, 100, 500>> <<rumble stop>>
// Please remember that Vibration API support is somewhat limited, so don't rely on it in critical parts.
// Very long sequences may be chopped or completely skipped, it may not work when putting game in iframe,
// and it often requires user input to start.
// https://caniuse.com/#feat=vibration
// https://developer.mozilla.org/docs/Web/API/Navigator/vibrate
'use strict';
/* globals version, Macro, Config */
const macroName = 'rumble';
if (!version || !version.title || 'SugarCube' !== version.title || !version.major || version.major < 2) {
throw new Error(`<<${macroName}>> macro requires SugarCube 2.0 or greater, aborting load`);
}
version.extensions[macroName] = {major: 1, minor: 0, revision: 0};
Macro.add(macroName, {
handler() {
if (!navigator.vibrate) {
if (Config.debug) {
console.warn(`Vibration not supported, <<${macroName}>> will do nothing.`);
}
return;
}
const args = this.args;
if (!args.length && Config.debug) {
this.error(`<<${macroName}>> needs arguments: 'stop' or comma-delimited positive integers.`);
return;
}
if (args[0] === 'stop' || args[0] === '0') {
navigator.vibrate(0);
} else {
const sequence = args.map((chunk) => parseInt(chunk.trim()));
navigator.vibrate(sequence);
}
},
});
}());