Last active
August 29, 2015 14:11
-
-
Save BaldarSilveraxe/3b09ede4260a39ab491c to your computer and use it in GitHub Desktop.
Roll20 API to be used with Dungeon Ready Tiles and scripts
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
// GIST: https://gist.github.com/BaldarSilveraxe/3b09ede4260a39ab491c | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
!readydoors | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
var DungeonReadyDoors = DungeonReadyDoors || (function(){ | |
'use strict'; | |
var version = 0.1, | |
opened = "https://s3.amazonaws.com/files.d20.io/images/6743577/YZp1vxxhRWgVMqySSZ6GpQ/thumb.png?1418743597", | |
swing = "https://s3.amazonaws.com/files.d20.io/images/6743730/OTbt2Yc7Srp2Pgm859H6JA/thumb.png?1418744374", | |
doorpath, | |
doorpathID, | |
filterDoorPath, | |
toggle = function(obj) { | |
var token = getObj('graphic', obj._id), path; | |
if (token.get("name") === "dungeonreadytorch") { | |
if (token.get("bar1_value") === "1") { | |
token.set({ | |
"light_radius": "", | |
"light_dimradius": "", | |
"light_otherplayers": false, | |
"bar1_value": "0", | |
"imgsrc": state.DungeonReadyDoorsURLs.torchoff | |
}); | |
} else { | |
token.set({ | |
"light_radius": 60, | |
"light_dimradius": 20, | |
"light_otherplayers": true, | |
"bar1_value": "1", | |
"imgsrc": state.DungeonReadyDoorsURLs.torchon | |
}); | |
} | |
} else { | |
path = getObj("path", token.get("name")); | |
if (token.get("bar1_value") === "1") { | |
path.set("layer", "gmlayer"); | |
token.set({ | |
"bar1_value": "0", | |
"width": 210, | |
"imgsrc": opened | |
}); | |
} else { | |
path.set("layer", "walls"); | |
if (token.get("bar2_value") === "1") { | |
token.set({ | |
"bar1_value": "1", | |
"width": 70, | |
"imgsrc": state.DungeonReadyDoorsURLs.barred | |
}); | |
} else { | |
token.set({ | |
"bar1_value": "1", | |
"width": 70, | |
"imgsrc": state.DungeonReadyDoorsURLs.door | |
}); | |
} | |
} | |
} | |
}, | |
readydoors = function() { | |
var doorControlName = "DungeonReadyDoorControl", | |
doorControlSheet = findObjs({ _type: "character", name: doorControlName}), doorControlSheetId, | |
torchControlName = "DungeonReadyTorchControl", | |
doorToggleName = "Toggle-Feature", doorToggleAbility, | |
allObjectImages, doorImages, torchImages; | |
if (doorControlSheet.length === 0) { | |
createObj("character", {name: doorControlName}); | |
} | |
doorControlSheet = findObjs({ _type: "character", name: doorControlName}); | |
doorControlSheetId = doorControlSheet[0].get("_id"); | |
doorToggleAbility = findObjs({ _type: "ability", name: doorToggleName, characterid: doorControlSheetId}); | |
if (doorToggleAbility.length === 0) { | |
log("doorToggleAbility") | |
createObj("ability", { | |
name: doorToggleName, | |
characterid: doorControlSheetId, | |
action: "!DungeonReadyDoorsToggle", | |
istokenaction: true | |
}); | |
} | |
allObjectImages = findObjs({ _type: "graphic", layer: "objects"}); | |
_.each(allObjectImages, function(obj) { | |
if (obj.get("name") === "dungeonreadydoor") { | |
doorpath = findObjs({ _type: "path", | |
layer: "walls", | |
stroke:"#00ff00" | |
}); | |
filterDoorPath = _.chain(doorpath) | |
.filter(function( o ) { | |
return (o.get('left') > obj.get("left") - 2) && (o.get('left') < obj.get("left") + 2) && (o.get('top') > obj.get("top") - 2) && (o.get('top') < obj.get("top") + 2); | |
}) | |
.value(); | |
if (filterDoorPath.length > 0) { | |
doorpathID = filterDoorPath[0].get("id"); | |
} else { | |
doorpathID = "dungeonreadydoor"; | |
} | |
if (obj.get("bar1_value") === "1") { | |
state.DungeonReadyDoorsURLs.door = obj.get("imgsrc"); | |
obj.set("name", doorpathID); | |
obj.set("represents", doorControlSheetId); | |
} else { | |
state.DungeonReadyDoorsURLs.barred = obj.get("imgsrc"); | |
obj.set("name", doorpathID); | |
obj.set("represents", doorControlSheetId); | |
obj.set("bar1_value", "1"); | |
obj.set("bar2_value", "1"); | |
} | |
createObj("graphic", { | |
_type: "graphic", | |
_subtype: "token", | |
_pageid: obj.get("_pageid") , | |
layer: "gmlayer", | |
width: 210, | |
height: 280, | |
left: obj.get("left"), | |
top: obj.get("top"), | |
imgsrc: swing, | |
rotation: obj.get("rotation"), | |
flipv: obj.get("flipv"), | |
fliph: obj.get("fliph") | |
}); | |
} | |
if (obj.get("name") === "dungeonreadytorch") { | |
obj.set("represents", doorControlSheetId); | |
if (obj.get("bar1_value") === "1") { | |
state.DungeonReadyDoorsURLs.torchon = obj.get("imgsrc"); | |
} else { | |
state.DungeonReadyDoorsURLs.torchoff = obj.get("imgsrc"); | |
} | |
} | |
}); | |
}, | |
handleInput = function(msg) { | |
var args, obj, playerid, dungeonReadyPage, dungeonReadyID, objCheck | |
msg = _.clone(msg); | |
if ( "api" !== msg.type ) {return; } | |
args = msg.content.split(/\s+/); | |
obj = _.first(msg.selected); | |
playerid = msg.playerid | |
switch(args[0]) { | |
case "!readydoors": | |
readydoors(); | |
return; | |
case "!DungeonReadyDoorsToggle": | |
objCheck = checkSelect(obj); | |
if ( objCheck == false) {return; } | |
toggle(obj); | |
break; | |
} | |
}, | |
checkSelect = function(obj,type) { | |
var token; | |
if (obj == undefined || obj.length < 1) { | |
return false; | |
} | |
if (obj._type != "graphic") { | |
return false; | |
} | |
token = getObj('graphic', obj._id); | |
if (token.get("layer") != "objects") { | |
return false; | |
} | |
return true; | |
}, | |
checkInstall = function() { | |
if( ! _.has(state,'DungeonReadyDoors')) { | |
log('DungeonReadyDoors: Resetting state'); | |
/* Default Settings stored in the state. */ | |
state.DungeonReadyDoors = {}; | |
} | |
if( ! _.has(state,'DungeonReadyDoorsURLs')) { | |
log('DungeonReadyDoorsURLs: Resetting state'); | |
/* Default Settings stored in the state. */ | |
state.DungeonReadyDoorsURLs = { | |
door: "https://s3.amazonaws.com/files.d20.io/images/6438063/X10nUgJNuThdJ6GdhUAi0A/med.png?1416620236", | |
barred: "https://s3.amazonaws.com/files.d20.io/images/6438067/oyzvQp98uq_2MPCYNZO9PQ/med.png?1416620239", | |
torchon: "https://s3.amazonaws.com/files.d20.io/images/6438075/TbPSZ2ZA42yDNy3cduEAtA/med.png?1416620243", | |
torchoff: "https://s3.amazonaws.com/files.d20.io/images/6438073/bB25Z0CTxcUhloxX3H-b9Q/med.png?1416620242", | |
}; | |
} | |
}, | |
registerEventHandlers = function() { | |
on('chat:message', handleInput); | |
}; | |
return { | |
CheckInstall: checkInstall, | |
RegisterEventHandlers: registerEventHandlers | |
}; | |
}()); | |
on("ready",function(){ | |
'use strict'; | |
DungeonReadyDoors.RegisterEventHandlers(); | |
DungeonReadyDoors.CheckInstall(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment