Skip to content

Instantly share code, notes, and snippets.

@BaldarSilveraxe
Last active January 30, 2016 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaldarSilveraxe/af41414d3cc30f0c6b90 to your computer and use it in GitHub Desktop.
Save BaldarSilveraxe/af41414d3cc30f0c6b90 to your computer and use it in GitHub Desktop.
var SampleDoor = SampleDoor || (function(){
'use strict';
var Rollable_Table_Avatar = 'https://s3.amazonaws.com/files.d20.io/images/15774224/TylRyZ0nvG4ChhmEApDOPw/thumb.png?1454115889',
find_create_rollabletable = function() {
var tables = findObjs({type: 'rollabletable',name: 'hit token button'}),
table = tables[0] || createObj('rollabletable', {name: 'hit token button',showplayers: false}),
items = findObjs({rollabletableid: table.id}),
item = items[0]||createObj('tableitem', {rollabletableid: table.id, avatar: Rollable_Table_Avatar});
_.each(tables, function( eachItem, index ) {
if( 0 !== index ) {
eachItem.remove();
}
});
_.each(items, function( eachItem, index ) {
if( 0 !== index ) {
eachItem.remove();
}
});
if( item.get('avatar') !== Rollable_Table_Avatar ) {
item.set({avatar: Rollable_Table_Avatar});
}
},
pathingRotation = function(angle, point,width,height) {
var pointX = point[0], pointY = point[1], originX = (width/2), originY = (height/2);
angle = angle * Math.PI / 180.0;
return [
Math.cos(angle) * (pointX-originX) - Math.sin(angle) * (pointY-originY) + originX,
Math.sin(angle) * (pointX-originX) + Math.cos(angle) * (pointY-originY) + originY
];
},
placeRotatedFlipPaths = function(givenPathData) {
var temp, i, newX, newY, inputPath, angle, Xoffset, Yoffset, PathArray, maxX, minX, maxY, minY, objectWidth, objectHeight,
objectTop, objectLeft, pathString, graphicID, newPath;
_.each(givenPathData, function(given) {
temp = [];
for(i = 0; i < given.path.length; i++) {
newX = given.path[i][0];
newY = given.path[i][1];
if(given.fliph){newX = given.width - given.path[i][0]; }
if(given.flipv){newY = given.height - given.path[i][1]; }
temp.push([newX, newY]);
}
given.path = temp;
graphicID = given.forID;
inputPath = given.path;
angle = given.rotation;
Xoffset = given.left - (given.width/2);
Yoffset = given.top - (given.height/2);
PathArray = [];
if(!angle) angle = 0;
if(!Xoffset) Xoffset = 0;
if(!Yoffset) Yoffset = 0;
maxX = 0;
minX = false;
maxY = 0;
minY = false;
for(i = 0; i < inputPath.length; i++) {
PathArray.push([inputPath[i][0], inputPath[i][1]]);
PathArray[i] = pathingRotation(angle, PathArray[i],given.width,given.height);
if(PathArray[i][0] > maxX) maxX = PathArray[i][0];
if(minX === false || Number(PathArray[i][0]) < Number(minX)) minX = PathArray[i][0];
if(PathArray[i][1] > maxY) maxY = PathArray[i][1];
if(minY === false || PathArray[i][1] < minY) minY = PathArray[i][1];
}
objectWidth = maxX - minX;
objectHeight = maxY - minY;
objectTop = minY + (objectHeight/2);
objectLeft = minX + (objectWidth/2);
for(i = 0; i < PathArray.length; i++) {
PathArray[i][0] = PathArray[i][0] - minX;
PathArray[i][1] = PathArray[i][1] - minY;
}
pathString = "";
for(i = 0; i < PathArray.length; i++) {
if(i != 0) {
pathString += ",[\"L\"," + PathArray[i][0] + "," + PathArray[i][1] + "]";
} else {
pathString = "[\[\"M\"," + PathArray[i][0] + "," + PathArray[i][1] + "]";
}
}
pathString += "\]";
objectTop = objectTop + Yoffset;
objectLeft = objectLeft + Xoffset;
given.path = pathString;
given.left = objectLeft;
given.top = objectTop;
newPath = createObj('path',{
pageid: Campaign().get('playerpageid'),
layer: 'walls',
path: given.path,
left: given.left,
top: given.top,
width: objectWidth,
height: objectHeight,
rotation: 0,
fliph: false,
flipv: false,
stroke: given.stroke,
stroke_width: given.strokewidth,
controlledby: graphicID
});
});
},
set_door = function(obj) {
_.each(findObjs({type: 'path', controlledby: obj.id }), function(wall) {
wall.remove();
});
obj.set({width:280, height:70});
placeRotatedFlipPaths([{
left: obj.get('left'),
top: obj.get('top'),
width: obj.get('width'),
height: obj.get('height'),
rotation: obj.get('rotation'),
fliph: obj.get('fliph'),
flipv: obj.get('flipv'),
path: [[0,40],[140,40]],
stroke: '#FF0000',
strokewidth: 3,
forID: obj.get('_id')
}]);
},
remove_door = function(obj) {
_.each(findObjs({type: 'path', controlledby: obj.id }), function(wall) {
wall.remove();
});
},
ready_events = function () {
on('add:graphic', function(obj){
if( obj.get('imgsrc') === Rollable_Table_Avatar ){
set_door(obj);
return;
}
});
on('change:graphic', function(obj){
if( obj.get('imgsrc') === Rollable_Table_Avatar ){
set_door(obj);
return;
}
});
on('destroy:graphic', function(obj){
if( obj.get('imgsrc') === Rollable_Table_Avatar ){
remove_door(obj);
return;
}
});
},
ready_module = function(){
find_create_rollabletable();
ready_events();
};
return {
ready_module: ready_module
};
}());
on('ready',function(){
'use strict';
SampleDoor.ready_module();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment