Last active
October 8, 2017 17:47
-
-
Save Bastlifa/ea4aaa9d4f605b090be904e675d4a5fd to your computer and use it in GitHub Desktop.
rooftop removal and restore script for roll20. WIP, probably awful in lots of ways. Didn't test much.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a script to remove rooftops if a player enters a building | |
on("ready", function() { | |
"use strict"; | |
var allPlayerTokens = []; | |
var allRoofTokens = []; | |
function getPlayerTokens() | |
{ | |
var campaign = findObjs({type: "campaign"})[0]; | |
var allTokens = findObjs({type: 'graphic', pageid: campaign.get("playerpageid"), layer: "objects"}); | |
allPlayerTokens = []; | |
for (var i = 0; i<allTokens.length; i++) | |
{ | |
if (allTokens[i].get("controlledby") != "" || allTokens[i].get("represents")) | |
{ | |
if (allTokens[i].get("represents")) | |
{ | |
if (getObj('character', allTokens[i].get("represents")).get("controlledby")) | |
{ | |
allPlayerTokens.push(allTokens[i]); | |
} | |
} | |
else | |
{ | |
allPlayerTokens.push(allTokens[i]); | |
} | |
} | |
} | |
} | |
function getRoofTokens() | |
{ | |
var campaign = findObjs({type: "campaign"})[0]; | |
var allMapTokens = findObjs({type: 'graphic', pageid: campaign.get("playerpageid")}); | |
allRoofTokens = []; | |
for (var i = 0; i<allMapTokens.length; i++) | |
{ | |
if (allMapTokens[i].get("name") == "roof") | |
{ | |
allRoofTokens.push(allMapTokens[i]); | |
} | |
} | |
} | |
function roofInsideCheck(roofToken) | |
{ | |
var anyoneInside = false; | |
for (var i = 0; i < allPlayerTokens.length; i++) | |
{ | |
if (roofToken.get("left") - roofToken.get("width")/2 < allPlayerTokens[i].get("left") && | |
roofToken.get("left") + roofToken.get("width")/2 > allPlayerTokens[i].get("left") && | |
roofToken.get("top") - roofToken.get("height")/2 < allPlayerTokens[i].get("top") && | |
roofToken.get("top") + roofToken.get("height")/2 > allPlayerTokens[i].get("top")) | |
{ | |
anyoneInside = true; | |
} | |
} | |
return anyoneInside; | |
} | |
function removeRoof(roofID) | |
{ | |
getObj('graphic', roofID).set("layer", "walls"); | |
} | |
function restoreRoof(roofID) | |
{ | |
getObj('graphic', roofID).set("layer", "map"); | |
} | |
on("change:campaign:playerpageid", function() { | |
getPlayerTokens(); | |
getRoofTokens(); | |
sendChat("", "/w gm &{template:default} {{name=Rooftops Ready}} {{Player Tokens=" + allPlayerTokens.length + | |
"}} {{Roof Tokens=" + allRoofTokens.length + "}}") | |
}); | |
on("chat:message", function (msg) { | |
if (msg.type === 'api' && msg.content === "!RoofReady"){ | |
getPlayerTokens(); | |
getRoofTokens(); | |
sendChat("", "/w gm &{template:default} {{name=Rooftops Ready}} {{Player Tokens=" + allPlayerTokens.length + | |
"}} {{Roof Tokens=" + allRoofTokens.length + "}}") | |
} | |
}); | |
on("change:graphic", function(obj){ | |
if (allPlayerTokens.includes(obj)) | |
{ | |
for (var i = 0; i < allRoofTokens.length; i++) | |
{ | |
if (roofInsideCheck(allRoofTokens[i])) { removeRoof(allRoofTokens[i].id); } | |
else { restoreRoof(allRoofTokens[i].id); } | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment