Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*global on, _, log, createObj, findObjs */
/*jslint white: true, multivar: true, single: true, bitwise: true, browser: true */
var RotatingMadness = RotatingMadness || (function(){
"use strict";
var script = {name: "RotatingMadness", version: 1.0, last_update: 1476924569, schema_version: 1.0},
your_library = [
{imgsrc: "https://s3.amazonaws.com/files.d20.io/images/24366259/sCxV-8v6FO9x9khDM-XPDQ/thumb.png?1476919656", tileName: "mapmaze"},
{imgsrc: "https://s3.amazonaws.com/files.d20.io/images/24366260/OgnC_UlRwWEBaRA1AoFV1w/thumb.png?1476919660", tileName: "spinner"}
],
tile_settings = [
{tileName: "mapmaze", width: 840, height: 840},
{tileName: "spinner", width: 560, height: 560}
],
path_data = [
{tileName: "spinner", path: [[208, 70], [210, 78], [210, 482], [209, 482]]},
{tileName: "spinner", path: [[350, 78], [350, 482]]},
{tileName: "mapmaze", path: [[350, 0], [350, 225], [330, 230], [100, 0]]},
{tileName: "mapmaze", path: [[490, 0], [490, 225], [510, 230], [740, 0]]},
{tileName: "mapmaze", path: [[350, 840], [350, 615], [330, 610], [100, 840]]},
{tileName: "mapmaze", path: [[490, 840], [490, 615], [510, 610], [740, 840]]},
{tileName: "mapmaze", path: [[0, 100], [230, 330], [222, 350], [0, 350]]},
{tileName: "mapmaze", path: [[0, 740], [230, 510], [222, 490], [0, 490]]},
{tileName: "mapmaze", path: [[840, 350], [615, 350], [607, 330], [840, 100]]},
{tileName: "mapmaze", path: [[840, 490], [615, 490], [607, 510], [840, 740]]}
],
tiles = [],
path_remove = function (obj) {
_.each(findObjs({ type: "path", controlledby: script.name + "|" + obj.id }), function (e) {
e.remove();
});
},
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
];
},
pathingString = function (PathArray) {
var pathString;
_(PathArray.length).times(function(n){
pathString = (n !== 0) ? pathString + ",[\"L\"," + PathArray[n][0] + "," + PathArray[n][1] + "]"
: "[[\"M\"," + PathArray[n][0] + "," + PathArray[n][1] + "]";
});
return pathString + "]";
},
placeRotatedFlipPaths = function(givenPathData) {
var inputPath, PathArray, maxX, minX, maxY, minY;
_.each(givenPathData, function(given) {
inputPath = [];
_.each(given.path, function(eachPoint) {
inputPath.push([
given.fliph ? given.width - eachPoint[0] : eachPoint[0],
given.flipv ? given.height - eachPoint[1] : eachPoint[1]
]);
});
PathArray = [];
maxX = 0;
minX = false;
maxY = 0;
minY = false;
_(inputPath.length).times(function(n){
PathArray.push([inputPath[n][0], inputPath[n][1]]);
PathArray[n] = pathingRotation((given.rotation||0), PathArray[n],given.width,given.height);
maxX = ( PathArray[n][0] > maxX ) ? PathArray[n][0] : maxX;
minX = ( minX === false || (Number(PathArray[n][0]) < Number(minX)) ) ? PathArray[n][0] : minX;
maxY = ( PathArray[n][1] > maxY ) ? PathArray[n][1] : maxY;
minY = ( minY === false || (Number(PathArray[n][1]) < Number(minY)) ) ? PathArray[n][1] : minY;
});
_(PathArray.length).times(function(n){
PathArray[n][0] = PathArray[n][0] - minX;
PathArray[n][1] = PathArray[n][1] - minY;
});
createObj("path",{
pageid: given.pageid,
layer: "walls",
path: pathingString(PathArray),
left: (minX + ((maxX - minX)/2)) + (given.left - (given.width/2)||0),
top: (minY + ((maxY - minY)/2)) + (given.top - (given.height/2)||0),
width: (maxX - minX),
height: (maxY - minY),
stroke: given.stroke,
stroke_width: given.strokewidth,
controlledby: given.forID
});
});
},
getCleanImgsrc = function (imgsrc) {
var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)(.*)\?.*$/);
if (parts) {
return parts[1] + "thumb" + parts[3];
}
return;
},
handle_tile = function (obj, tileName) {
var tile_info = _.findWhere(tile_settings, {tileName: tileName}),
pathData = [];
if (tile_info) {
obj.set({
width: tile_info.width,
height: tile_info.height
});
path_remove(obj);
_.each(_.where(path_data, {tileName: tileName}), function (eachPath) {
pathData.push({
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"),
pageid: obj.get("pageid"),
path: eachPath.path,
stroke: "#FF0000",
strokewidth: 1,
forID: script.name + "|" + obj.id
});
});
if (pathData) {
placeRotatedFlipPaths(pathData);
}
}
},
doorSpinner = function(obj,start,end,plusMinus,name) {
(function(){
var angle = start,
delta = plusMinus,
threshold = end,
rate = 100,
pathData = [],
interval = setInterval(function(){
obj.set('rotation', angle);
path_remove(obj);
handle_tile(obj, name);
angle += delta;
if(angle >= threshold) {
clearInterval(interval);
obj.set({rotation: end, light_angle: end});
handle_tile(obj, name);
}
}, rate);
}());
},
on_change_graphic = function (obj) {
var cleanUrl = getCleanImgsrc(obj.get("imgsrc")),
tile;
tile = _.findWhere(tiles, {imgsrc: cleanUrl});
if (tile) {
handle_tile(obj, tile.tileName);
}
},
on_destroy_graphic = function (obj) {
path_remove(obj);
},
on_chat_message = function (msg) {
var message = _.clone(msg),
graphic,
tile;
if (message.type !== "api" ) {
return;
}
graphic = getObj("graphic", "-KUUK1Ax40755hpc_U6j");
tile = _.findWhere(tiles, {imgsrc: getCleanImgsrc(graphic.get("imgsrc"))});
if (tile) {
doorSpinner(graphic,graphic.get("rotation"),graphic.get("rotation") + 45,5, tile.tileName);
}
},
register_event_handlers = function() {
on("change:graphic", on_change_graphic);
on("destroy:graphic", on_destroy_graphic);
on('chat:message', on_chat_message);
},
ready_module = function () {
log(script.name + " " + (new Date(script.last_update * 1000)) + " version: " + script.version + " Schema version: " + script.schema_version, script);
_.each(your_library, function (u) {
tiles.push({
imgsrc: getCleanImgsrc(u.imgsrc),
tileName: u.tileName
});
});
register_event_handlers();
};
return {
ready_module: ready_module
};
}());
on("ready",function(){
"use strict";
RotatingMadness.ready_module();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.