Sure. To use it, all you need to do is put it in a tab on the API Scripts page for your Game. Then any token on the objects layer with the "overdrive" symbol on it will be randomly repositioned whenever they are moved. If you want to understand what the script is doing, here's an annotated version: // run this function when the api is fully loaded. light going when the light turns green, instead of one of the yellow ones.
on('ready',function(){
// tells the javascript engine to make certain sloppy coding practices into errors
"use strict";
// starts defining things. first is a distance function
var distance=function(p1,p2) {
// pythagorean theorem for distance between two points
return Math.sqrt(Math.pow(p2[0]-p1[0],2)+Math.pow(p2[1]-p2[1],2));
},
// function to determine how long a move chain was
moveDistance=function(lastMove){
return _.chain(lastMove.split(/,/)) //< break the move string up by commas
.map(parseFloat) //< turn each thing into a number
.groupBy(function(v,k){ //< break up the array into points
return Math.round((k-1)/2);
})
.reduce(function(m,p){ //< walk the points adding the distance between them.
if(m.pp){
m.sum+=distance(m.pp,p);
}
m.pp=p;
return m;
},{
pp:null,
sum:0
})
.value()
.sum; //< return just the sum distance
};
// run this function when graphics (tokens) are changed
on('change:graphic',function(obj,prev){
// setup some variables for later use
var page, move, theta,tprime,lprime;
// determine if we want to affect this object
if( 'objects' === obj.get('layer') /* on the objects layer */
&& -1 !== obj.get('statusmarkers').indexOf('overdrive') /* has the overdrive symbol */
&& (obj.get('top') !== prev.top || obj.get('left') !== prev.left) /* has changed position */
) {
// grab the page object based on the page the token is on (for later use bounding)
page=getObj('page',obj.get('pageid'));
// figure out how far the token moved
move = moveDistance(obj.get('lastmove'));
if(!move){
// if it was just dragged directly, the lastmove field won't give us a distance,
// use the distance between where it is now and where it was instead
move=distance([obj.get('left'),obj.get('top')],[prev.left,prev.top]);
}
// cut move in half. move/=2 is the same as move = move/2
move/=2;
// get a random angle in radians
theta=(randomInteger(360) * (math.pi/180));
// calculate the new top, bounded by half a unit from the top and bottom edge of the map
tprime = Math.max(35,Math.min((page.get('height')*70)-35,obj.get('top')+move*Math.sin(theta)));
// calculate the new left, bounded by half a unit from the left and right edge of the map
lprime = Math.max(35,Math.min((page.get('width')*70)-35,obj.get('left')+move*Math.cos(theta)));
// set the object's new position
obj.set({
top: tprime,
left: lprime
});
}
});
});