Skip to content

Instantly share code, notes, and snippets.

@shdwjk
Created September 5, 2014 14:49
Show Gist options
  • Select an option

  • Save shdwjk/054574d39c166d50865c to your computer and use it in GitHub Desktop.

Select an option

Save shdwjk/054574d39c166d50865c to your computer and use it in GitHub Desktop.
Roll20 API: LokiTrapMan -- reoganization of Loki's trap script.
// GIST: https://gist.github.com/shdwjk/054574d39c166d50865c
var LokiTrapMan = LokiTrapMan || ( function() {
'use strict';
// Here is the bulk of where you define a trap and cusomize the events. This is an array of all traps on your campaign (I only have 1 defined).
// Trap coordinates are defined in squares, not pixals. A trap location is defined as a rectange having two points (x1,y1 to x2,y2)
// Set the pageID and region of the trap and when a token enters this area the defined callback in the array will be triggered.
var traps = [
// In this example when a charater lands next to (or on) a placed npc he (and his campfire) disappears in a firey explosion.
{
pageID: "7F73B956-71C3-4C5B-8C72-576EE4F15EA4",
xP1: 16,
yP1: 2,
xP2: 18,
yP2: 4,
fired: false,
fireEvent: function(firedBy) {
//Find objects I want to manipulate
var explode = findObjs({_type: "graphic", name: "MysteryExplode"})[0],
man = findObjs({_type: "graphic", name: "Mystery Man"})[0],
campfire = findObjs({_type: "graphic", name: "MysteryCampfire"})[0],
flashCount,
flashObj;
// GM emote a notification of the trap trigger and by whom.
sendChat('', "/emas The camper laughs at "+ firedBy.get("name") +" and disappears in a firey explosion. You take 2 points of damage");
// Hide the npc and the campfire
man.set({
'layer': 'gmlayer'
});
campfire.set({
'layer': 'gmlayer'
});
// Animate a quick flashing explosion
flashCount = 0;
flashObj = setInterval(function(){
var nextLayer = (explode.get("layer") === "gmlayer") ? "objects" : "gmlayer";
explode.set({
'layer': nextLayer
});
flashCount++;
// ensure the explosion is hidden before we kill the animation.
if (flashCount >= 20 && explode.get("layer") === "gmlayer") {
clearInterval(flashObj);
}
}, 50);
}
}
],
checkTrapTrigger = function(firedBy) {
// Get the page ID and location of the changed token
var pageID = firedBy.get("_pageid"),
xLoc = Math.ceil(firedBy.get("left")/70),
yLoc = Math.ceil(firedBy.get("top")/70);
log("Checking for trap triggers at " +pageID+":"+ xLoc + ","+yLoc);
// For each defined trap check to see if the token landed on the trap.
// Future work will have options to control triggering on crossing over and\or landing on it to trigger it
traps.forEach(function(opts){
log("Checking trap "+opts.pageID+":"+parseInt(opts.xP1,10)+","+parseInt(opts.yP1,10)+";"+parseInt(opts.xP2,10)+","+parseInt(opts.yP2,10),10);
if ( !opts.fired
&& opts.pageID === pageID
&& xLoc >= parseInt(opts.xP1,10)
&& xLoc <= parseInt(opts.xP2,10)
&& yLoc >= parseInt(opts.yP1,10)
&& yLoc <= parseInt(opts.yP2,10)
) {
opts.fired = 1; //Only fire a trap event once
opts.fireEvent(firedBy); // Fire the trap callback taking some action
}
});
},
onChangeGraphic = function (obj, prev) {
//Only token linked to a char sheet will trigger traps.
if (obj.get("represents") !== "") {
checkTrapTrigger(obj);
}
},
registerEventHandlers = function() {
on('change:graphic', onChangeGraphic);
}
;
return {
RegisterEventHandlers: registerEventHandlers
};
}() );
on("ready", function() {
'use strict';
LokiTrapMan.RegisterEventHandlers();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment