User:RSChetFaker/common.js

rotate_factor = 0; rooms = { garden: 'four', parlour: 'three', kitchen: 'two', dining: 'three', workshop: 'workshop', bedroom: 'two', skill: 'four', games: 'three', combat: 'four', quest: 'four', menagerie: 'four', study: 'three', costume: 'one', chapel: 'two', portals: 'one', formal: 'four', throne: 'one', aquarium: 'four', oubliette: 'four', dungeon: 'four', pit: 'four', treasure: 'four' }; grid = [];

function prepContainers { $('#canvas').html('  '); }

function addLinks { for (var key in rooms) { $('.heap ul').append('' + cap(key) + '') } };

function generateGrid { for (var i = 0; i < 7; i++) { grid[i] = []; for (var p = 0; p < 7; p++) { grid[i][p] = null; } }  console.table(grid); }

function makeRoom(room_name) { var el = $('' + cap(room_name) + ' ').appendTo('.grid').draggable({   containment: '.grid',    grid: [80, 80],    start: function {      startx = $(this).position.left / 80;      starty = $(this).position.top / 80;    },    stop: function {      endx = $(this).position.left / 80;      endy = $(this).position.top / 80;      if (grid[endy][endx] === null) {        grid[starty][startx] = null;        grid[endy][endx] = $(this)[0];        $(el[0]).data("location", { x: endx, y: endy });     } else {        $(this).css({ top: starty * 80, left: startx * 80 });       grid[starty][startx] = $(this)[0];        $(el[0]).data("location", { x: startx, y: starty });     }    }  });

if (grid[0][0] !== null) { for (var key in grid) { for (var subkey in grid[key]) { var subslot_value = grid[key][subkey]; if (subslot_value === null) { grid[key][subkey] = el[0]; $(el[0]).css({           top: key * 80,            left: subkey * 80          }); $(el[0]).data("location", {           x: subkey,            y: key          }); return false; }     }    }  } else { grid[0][0] = el[0]; $(el[0]).data("location", {     x: 0,      y: 0    }); } };

function rotateRoom(element) { rotate_factor += 1; var rotate_angle = (90 * rotate_factor) % 360; $(element).css({   '-moz-transform': 'rotate(' + rotate_angle + 'deg)',    '-webkit-transform': 'rotate(' + rotate_angle + 'deg)',    '-o-transform': 'rotate(' + rotate_angle + 'deg)',    '-ms-transform': 'rotate(' + rotate_angle + 'deg)',    'transform': 'rotate(' + rotate_angle + 'deg)'  }); }

function cap(string) { return string.charAt(0).toUpperCase + string.slice(1); }

//add room on menu link click $('body').on('click', '.heap a', function(e) { e.preventDefault;  var room_name = $(this).attr('href');  makeRoom(room_name); });

//rotate room on click $('body').on('click', '.block', function(e) { e.preventDefault;  rotateRoom(this);  console.table(grid); });

//remove room on middle mouse click $('body').on('mousedown', '.block', function(e) { e.preventDefault;  if (e.which == 2) {    var x = $(this).data("location").x;    var y = $(this).data("location").y;    grid[y][x] = null;    $(this).remove;  } });

//init prepContainers; addLinks; generateGrid;