Skip to content

Instantly share code, notes, and snippets.

@Bastlifa
Last active May 13, 2020 00:08
Show Gist options
  • Save Bastlifa/50439188f09fff6a88e18da77e7ef0c1 to your computer and use it in GitHub Desktop.
Save Bastlifa/50439188f09fff6a88e18da77e7ef0c1 to your computer and use it in GitHub Desktop.
Roll20 API script to shake the screen, maybe for earthquake
/* Screen Shake (really just maps, tokens, and paths)
make a macro with the folloing body
/w gm &{template:default} {{name=Screen Shaker}} {{[Start](!ScreenShake)}} {{[Shake With Time](!ScreenShake ?{time})}} {{[Stop](!ScreenShakeStop)}}
*/
on("ready", function() {
var ScreenShakeEnabled = true;
on("chat:message", function (msg) {
if (msg.type === "api" && msg.content.split(' ')[0] === "!ScreenShake" && playerIsGM(msg.playerid))
{
var shakeTime = 0;
var startTime = Date.now();
if (msg.content.split(' ')[1]) { shakeTime = parseInt(msg.content.split(' ')[1]) * 1000; }
ScreenShakeEnabled = true;
var page = getObj('player', msg.playerid).get('_lastpage');
var tokens = findObjs({type: 'graphic', pageid: page});
var paths = findObjs({type: 'path', pageid: page});
var tokensLeft = [];
var tokensTop = [];
var pathsLeft = [];
var pathsTop = [];
for (var i = 0; i < tokens.length; i++)
{
tokensLeft.push(tokens[i].get('left'));
tokensTop.push(tokens[i].get('top'));
}
for (var j = 0; j < paths.length; j++)
{
pathsLeft.push(paths[j].get('left'));
pathsTop.push(paths[j].get('top'));
}
var oInterval = setInterval(function() {
if (ScreenShakeEnabled && (shakeTime == 0 || shakeTime > Date.now() - startTime))
{
let randomWalkLeft = randomInteger(70);
let randomWalkTop = randomInteger(70);
for (var k = 0; k< tokens.length; k++)
{
tokens[k].set('left', tokensLeft[k] - 35 + randomWalkLeft);
tokens[k].set('top', tokensTop[k] - 35 + randomWalkTop);
}
for (var l = 0; l< paths.length; l++)
{
paths[l].set('left', pathsLeft[l] - 35 + randomWalkLeft);
paths[l].set('top', pathsTop[l] - 35 + randomWalkTop);
}
}
else
{
clearInterval(oInterval);
for (var k = 0; k< tokens.length; k++)
{
tokens[k].set('left', tokensLeft[k]);
tokens[k].set('top', tokensTop[k]);
}
for (var l = 0; l< paths.length; l++)
{
paths[l].set('left', pathsLeft[l]);
paths[l].set('top', pathsTop[l]);
}
}
}, 150);
}
if (msg.type === "api" && msg.content === "!ScreenShakeStop" && playerIsGM(msg.playerid))
{
ScreenShakeEnabled = false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment