Last active
August 29, 2015 14:20
-
-
Save BaldarSilveraxe/1fedbb8cf53584d80e03 to your computer and use it in GitHub Desktop.
Roll20 API feature script based on polygon path on the GM layer
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
var featurePath = featurePath || (function(){ | |
'use strict'; | |
var version = '0.1.1', | |
lastUpdate = 1430550687, | |
schemaVersion = '0.1.1', | |
objExtractKeysGraphic = ['id','pageid','layer','width','height','top','left'], | |
objExtractKeysPath = ['id','pageid','layer','width','height','top','left','path','stroke'], | |
activeTrap = function(hazzard, token) { | |
var objValues = getObjValue(hazzard, objExtractKeysPath), | |
featureMacro = findObjs({ | |
_type: 'macro', | |
name: objValues.stroke | |
})[0]; | |
//DO SOMETHING WITH 'featureMacro' | |
log(hazzard) | |
log(token) | |
log(featureMacro) | |
}, | |
getObjValue = function(obj, keys) { | |
return _.reduce( keys || objExtractKeys, function(m,prop){ | |
m[prop] = obj.get(prop); | |
return m; | |
}, {}); | |
}, | |
isPointInPoly = function (poly, pt){ | |
var c, i, l, j; | |
for(c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) { | |
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) | |
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x) | |
&& (c = !c); | |
} | |
return c; | |
}, | |
pathChecker = function (obj){ | |
var ObjValues = getObjValue(obj, ['layer', 'path']), | |
check = true, | |
firstX = false, | |
firstY = false, | |
lastX, lastY; | |
_.each(JSON.parse(ObjValues.path), function(eachPoint) { | |
if( false === firstX ) { | |
firstX = eachPoint[1]; | |
firstY = eachPoint[2]; | |
} | |
lastX = eachPoint[1]; | |
lastY = eachPoint[2]; | |
if( 3 !== eachPoint.length ) { | |
check = false; | |
return false; | |
} | |
}); | |
if ( (lastX !== firstX) || (lastY !== firstY) ) { | |
check = false; | |
} | |
if ( ('gmlayer' !== ObjValues.layer) ) { | |
check = false; | |
} | |
return check; | |
}, | |
registerPath = function(obj) { | |
var objValues = getObjValue(obj, objExtractKeysPath), | |
goodPath = pathChecker(obj); | |
if(goodPath){ | |
obj.set({ | |
controlledby: 'supportedPath', | |
fill: 'transparent' | |
}); | |
} | |
}, | |
checkTrap = function(obj) { | |
var objValues = getObjValue(obj, objExtractKeysGraphic), | |
point = [{x: objValues.left, y: objValues.top}], | |
inPoly = false, | |
trapId = '', | |
currentPageTraps = findObjs({ | |
_pageid: objValues.pageid, | |
_type: 'path', | |
controlledby: 'supportedPath' | |
}), | |
checkPath; | |
_.each(currentPageTraps, function(obj) { | |
checkPath = []; | |
_.each(JSON.parse(obj.get('path')), function(eachPoint) { | |
checkPath.push({ | |
x: parseInt(eachPoint[1] + (obj.get('left') - (obj.get('width') / 2))), | |
y: parseInt(eachPoint[2] + (obj.get('top') - (obj.get('height') / 2))) | |
}); | |
}); | |
inPoly = isPointInPoly(checkPath, point[0]); | |
if(true === inPoly) { | |
trapId = obj.get('id') | |
} | |
}); | |
if(true === inPoly) { | |
activeTrap(getObj('path', trapId),obj); | |
} | |
}, | |
checkInstall = function() { | |
if( ! _.has(state,'featurePath') || state.featurePath.version !== schemaVersion) { | |
log('featurePath: Resetting state'); | |
state.featurePath = { | |
version: schemaVersion | |
}; | |
} | |
}, | |
handleGraphicChange = function(obj) { | |
var objValues = getObjValue(obj, objExtractKeysGraphic), | |
playerBookMark = Campaign().get('playerpageid'); | |
if(('objects' === objValues.layer) && playerBookMark === objValues.pageid) { | |
checkTrap(obj); | |
} | |
}, | |
handlePathAdd = function(obj) { | |
var objValues = getObjValue(obj, objExtractKeysPath), | |
playerBookMark = Campaign().get('playerpageid'); | |
if(('gmlayer' === objValues.layer) && playerBookMark === objValues.pageid) { | |
registerPath(obj); | |
} | |
}, | |
registerEventHandlers = function() { | |
on('change:graphic', handleGraphicChange); | |
on('add:path', handlePathAdd); | |
checkInstall(); | |
}; | |
return { | |
CheckInstall: checkInstall, | |
RegisterEventHandlers: registerEventHandlers | |
}; | |
}()); | |
on('ready',function(){ | |
'use strict'; | |
featurePath.RegisterEventHandlers(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment