Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*global on, _, getObj, createObj, findObjs, state */
/*jslint white: true, bitwise: true, for: true, multivar: true, browser: true */
var deluxe_dungeon_designer = deluxe_dungeon_designer || (function() {
'use strict';
var script = 'deluxe_dungeon_designer',
version = 0.10,
last_update = 1459468801, //Unix timestamp
schema_version = 0.10,
percentChange = 0.3,
maxColor = 168,
http = 'https://s3.amazonaws.com/files.d20.io/images/',
file = 'thumb.png?',
deleteSource = http + '16499766/kYvZHn25orNHvu0CbX4Qpw/' + file + '1456084159',
createSource = http + '16686548/SZa2lFqdvt6PaWWI4RkHKA/' + file + '1456606560',
bkgrndSource = http + '16352171/kK8h6OH-zLXEf0-kQBtUkw/' + file + '1455671236',
knobLRSource = http + '16435047/0xrxAYHly_m8b5M2E9-xIw/' + file + '1234567890',
knobUDSource = http + '16435047/0xrxAYHly_m8b5M2E9-xIw/' + file + '0987654321',
shakerSource = http + '16446393/coPNDoCKjLylrxsim8pKWg/' + file + '1455958737',
featureArray = [
{lt: 875, tp: 700, wh: 1750, ht: 1400, ic: bkgrndSource, lr: 'map', rn: 0},
{lt: 210, tp: 1265, wh: 150, ht: 150, ic: knobLRSource, lr: 'objects', rn: 0},
{lt: 1545, tp: 1265, wh: 150, ht: 150, ic: knobUDSource, lr: 'objects', rn: 90},
{lt: 1545, tp: 700, wh: 150, ht: 150, ic: shakerSource, lr: 'objects', rn: 0}
],
hexToRgb = function (hex) {
var result, shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
},
step_color = function (color, percent) {
var f = parseInt(color.slice(1),16),
t = percent<0 ? 0 : 255,
p = percent<0 ? percent*-1 : percent,
R = f>>16,
G = f>>8&0x00FF,
B = f&0x0000FF;
return "#"+(0x1000000+(Math.round((t-R)*p)+R)*0x10000+(Math.round((t-G)*p)+G)*0x100+(Math.round((t-B)*p)+B)).toString(16).slice(1);
},
reset_map = function (o){
_.each(findObjs({pageid: o.get('pageid'), type: 'path', controlledby: script}), function(e) {
if ( hexToRgb(e.get('stroke')).r >= maxColor ) {
e.set('controlledby', 'remove');
e.remove();
}else{
e.set('stroke', step_color(e.get('stroke'),percentChange) );
}
});
},
control_change = function (cMove,pageid) {
var control = state[script].data[pageid],delta,loc,l,t,w,h;
delta = ((cMove.oldLoc-cMove.newLoc)< -100)?-100:((cMove.oldLoc-cMove.newLoc)>100)?100:(cMove.oldLoc-cMove.newLoc);
if( 'up/down' === cMove.which ) {
loc = ((control.currentTop - delta) < 242) ? 242 : ((control.currentTop - delta) > 1106) ? 1106 : (control.currentTop - delta);
l = control.currentLeft;
t = Math.round((control.currentTop + loc) /2);
w = 1;
h = Math.round(Math.abs(control.currentTop - loc)) + 1 ;
state[script].data[pageid] = {
currentLeft: l,
currentTop: loc
};
}
if( 'left/right' === cMove.which ) {
loc = ((control.currentLeft - delta) < 309) ? 309 : ((control.currentLeft - delta) > 1438) ? 1438 : (control.currentLeft - delta);
l = Math.round((control.currentLeft + loc) /2);
t = control.currentTop;
w = Math.round(Math.abs(control.currentLeft - loc)) + 1;
h = 1;
state[script].data[pageid] = {
currentLeft: loc,
currentTop: t
};
}
_(2).times(function(n){
createObj('path', {
left: l, top: t, width: w, height: h, pageid: pageid, layer: n===0?'objects':'walls', stroke_width: 2, stroke: '#000000',
path: '[["M", 0, 0],["L", '+ w+', 0],["L", '+ w+', '+ h+'],["L", 0, '+ h+'],["L", 0, 0]]', controlledby: 'add'
});
});
},
orphan_token = function(pgid){
_.each(findObjs({pageid: pgid, type: 'graphic', imgsrc: createSource}), function(e) {
e.remove();
});
},
on_add_graphic = function(o) {
var pg,g,u=o.get('imgsrc'),eu;
if( u !== createSource ){ return; }
pg = o.get('pageid');
g = findObjs({pageid: pg, type: 'graphic', imgsrc: bkgrndSource});
getObj('page', pg).set({width: 25, height: 25, gridcolor: 'transparent', background_color: '#FFFFFF'});
if( 0 === g.length ) {
if( !_.has(state[script].data, pg) ) { state[script].data[pg] = {currentLeft: 700, currentTop: 700 }; }
_.each(featureArray, function(f) {
createObj('graphic', {left: f.lt, top: f.tp, width: f.wh, height: f.ht, imgsrc: f.ic,
pageid: pg, isdrawing: true, layer: f.lr, rotation: f.rn, controlledby: 'add'
});
});
}else{
_.each(findObjs({pageid: pg, type: 'graphic' }), function(e) {
eu = e.get('imgsrc');
if( (eu === knobLRSource) || (eu === knobUDSource) || (eu === bkgrndSource) || (eu === shakerSource) ) {
e.set('imgsrc', deleteSource);
e.remove();
}
});
_.each(findObjs({pageid: pg, type: 'path', controlledby: script}), function(e) {
e.set('controlledby', 'remove');
e.remove();
});
}
setTimeout(function() { orphan_token(pg); }, 10);
},
on_change_graphic = function (o,p) {
var f,u=o.get('imgsrc'),pg = o.get('pageid');
if ( 'add' === o.get('controlledby') ) {
o.set('controlledby', '');
return;
}
if( u !== knobLRSource && u !== knobUDSource && u !== shakerSource ) { return; }
if( !_.has(state, script) && !_.has(state[script].data, pg) ) { return; }
f = _.findWhere(featureArray, {ic: u });
if( u === shakerSource ){ reset_map(o); }
if( u === knobLRSource && !_.isUndefined(p) ) { control_change({which: 'left/right', newLoc: o.get('left'), oldLoc: p.left},pg); }
if( u === knobUDSource && !_.isUndefined(p) ) { control_change({which: 'up/down', newLoc: o.get('top'), oldLoc: p.top},pg); }
if( !_.isUndefined(f.wh) ) {
o.set({width: f.wh, height: f.ht, left: f.lt, top: f.tp, isdrawing: true, layer: f.lr, fliph: false, flipv: false, rotation: f.rn });
}
},
on_destroy_graphic = function(o) {
var u = o.get('imgsrc');
if( u === knobLRSource || u === knobUDSource || u === bkgrndSource || u === shakerSource ){
createObj('graphic', {
left: o.get('left'), top: o.get('top'), width: o.get('width'), height: o.get('height'), imgsrc: u, pageid: o.get('pageid'),
isdrawing: true, layer: o.get('layer'), rotation: o.get('rotation'), controlledby: 'add'
});
}
},
on_change_path = function (o,p) {
if( o.get('controlledby') === 'add' ) {
o.set('controlledby', script);
return;
}
if( o.get('controlledby') === script || o.get('stroke') === p.stroke ){
createObj('path', {
left: p.left, top: p.top, width: p.width, height: p.height, pageid: p._pageid, layer: p.layer,
stroke_width: p.stroke_width, stroke: p.stroke, path: o.get('path'), controlledby: 'add'
});
o.set('controlledby', 'remove');
o.remove();
}
},
on_destroy_path = function (o) {
if( o.get('controlledby') === script ) {
createObj('path', {
left: o.get('left'), top: o.get('top'), width: o.get('width'), height: o.get('height'), pageid: o.get('pageid'), layer: o.get('layer'),
stroke_width: o.get('stroke_width'), stroke: o.get('stroke'), path: o.get('path'), controlledby: o.get('controlledby')
});
}
},
registerEventHandlers = function() {
on('add:graphic', on_add_graphic);
on('change:graphic', on_change_graphic);
on('destroy:graphic', on_destroy_graphic);
on('change:path', on_change_path);
on('destroy:path', on_destroy_path);
},
find_create_rollabletable = function() {
var tables = findObjs({type: 'rollabletable',name: script}),
table = tables[0] || createObj('rollabletable', {name: script,showplayers: false}),
items = findObjs({rollabletableid: table.id}),
item = items[0]||createObj('tableitem', {rollabletableid: table.id, avatar: createSource});
_.each(tables, function( eachItem, index ) {
if( 0 !== index ) {
eachItem.remove();
}
});
_.each(items, function( eachItem, index ) {
if( 0 !== index ) {
eachItem.remove();
}
});
if( item.get('avatar') !== createSource ) {
item.set({avatar: createSource});
}
},
ready_state = function(){
if( !_.has(state, script) || state[script].version !== schema_version ) {
state[script] = {
version: schema_version,
data: {}
};
}
},
ready_module = function () {
ready_state();
find_create_rollabletable();
registerEventHandlers();
};
return {
ready_module: ready_module
};
}());
on('ready',function(){
'use strict';
deluxe_dungeon_designer.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.