diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:10 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:28 -0400 |
| commit | 607f69c67a5b4dc72d2754192e3cdf67d0ad11d0 (patch) | |
| tree | 6556e7922c5bedb274bb1650e5dd100643a7895d /client/assets/javascripts/rectangles/models/room.js | |
| parent | d31259291d807c851de4396921e0c26b6dd8dce2 (diff) | |
partitioning client and serveR
Diffstat (limited to 'client/assets/javascripts/rectangles/models/room.js')
| -rw-r--r-- | client/assets/javascripts/rectangles/models/room.js | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/client/assets/javascripts/rectangles/models/room.js b/client/assets/javascripts/rectangles/models/room.js new file mode 100644 index 0000000..731411c --- /dev/null +++ b/client/assets/javascripts/rectangles/models/room.js @@ -0,0 +1,213 @@ +window.Room = (function(){ + + var Room = function(opt){ + this.id = opt.id || Rooms.list.length + this.rect = opt.rect + this.regions = [] + this.walls = [] + this.floor = [] + this.ceiling = [] + + this.height = opt.height || 200 + this.focused = false + } + + Room.prototype.toString = function(){ + return this.rect.toString() + } + + Room.prototype.reset = function(){ + var copy = this.rect.clone() + copy.id = this.id + copy.sides = FRONT | BACK | LEFT | RIGHT + this.regions = [ copy ] + + this.intersects = [] + this.constructed = false + + this.walls = [] + + this.mx_walls = [] + this.mx_floor = [] + this.mx_ceiling = [] + } + + Room.prototype.bind = function(){ + var base = this + base.mx_walls.forEach(function(wall){ + $(wall.el).bind({ + mouseover: function(){ + }, + mousemove: function(e){ + var color = choice(window.palettes.colors) + base.mx_walls.forEach(function(wall){ + $(wall.el).css("background-color", color) + }) + }, + mousedown: function(){ + } + }) + }) + } + + Room.prototype.group_mx_walls = function(){ + var base = this + var side_groups = {}, walls = [] + + // group the walls by side + base.mx_walls.forEach(function(wall){ + + // ignore half-walls for now + var side = wall.side || wall.half_side + + if (side_groups[side]) { + side_groups[side].push(wall) + } + else { + side_groups[side] = [wall] + } + }) + + // sort the subgroups, and then combine mx objects under wall objects + pairs(side_groups).forEach(function(pair){ + var side = pair[0], els = pair[1] + + if (side & LEFT_RIGHT) { + els.sort(compare_x) + } + else if (side & FRONT_BACK) { + els.sort(compare_z) + } + + var wall + + els.forEach(function(el){ + if (el.half_side) { + wall = new_wall(el) + walls.push(wall) + wall = null + } + else if (! wall) { + wall = new_wall(el) + walls.push(wall) + } + else if (side & FRONT_BACK && wall.rect.x.b == el.rect.x.a) { + wall.rect.x.b = el.rect.x.b + wall.mx.push(el) + } + else if (side & LEFT_RIGHT && wall.rect.y.b == el.rect.y.a) { + wall.rect.y.b = el.rect.y.b + wall.mx.push(el) + } + else { + wall = new_wall(el) + walls.push(wall) + } + }) + }) + + function new_wall (el) { + return new Wall ({ + room: base.id, + side: el.side | el.half_side, + half_side: el.half_side, + rect: el.rect.clone(), + el: el, + }) + } + + return walls + } + + Room.prototype.clipTo = function(r){ + // for each of this rect's regions split the region if necessary + var regions = this.regions + var splits + for (var i = 0, len = regions.length; i < len; i++) { + if (regions[i] && regions[i].intersects(r)) { + splits = regions[i].split(r) + regions = regions.concat(splits) + regions[i] = null + } + } + this.regions = regions + } + + Room.prototype.collides = function(x,y){ + var collision = 0, wall_collision, contains_x, contains_y + this.regions.forEach(function(r){ + if (! r.sides) return + + wall_collision = 0 + + if ((r.sides & FRONT) && y < r.y.a) { + wall_collision |= FRONT + } + if ((r.sides & BACK) && r.y.b < y) { + wall_collision |= BACK + } + if ((r.sides & LEFT) && x < r.x.a) { + wall_collision |= LEFT + } + if ((r.sides & RIGHT) && r.x.b < x) { + wall_collision |= RIGHT + } + if (! wall_collision) return + + contains_y = r.y.contains(y) + contains_x = r.x.contains(x) + + if (contains_x) { + collision |= wall_collision & FRONT_BACK + } + else if (contains_y) { + collision |= wall_collision & LEFT_RIGHT + } + else if (bitcount(wall_collision) > 1) { + collision |= wall_collision + } + }) + return collision + } + + Room.prototype.collidesDisc = function(x,y,radius){ + var collision = 0, wall_collision, contains_x, contains_y + this.regions.forEach(function(r){ + if (! r.sides) return + + wall_collision = 0 + + if ((r.sides & FRONT) && y-radius < r.y.a) { + wall_collision |= FRONT + } + if ((r.sides & BACK) && r.y.b < y+radius) { + wall_collision |= BACK + } + if ((r.sides & LEFT) && x-radius < r.x.a) { + wall_collision |= LEFT + } + if ((r.sides & RIGHT) && r.x.b < x+radius) { + wall_collision |= RIGHT + } + if (! wall_collision) return + + contains_x = r.x.contains(x, radius) + contains_y = r.y.contains(y, radius) + + if (contains_x) { + collision |= wall_collision & FRONT_BACK + } + else if (contains_y) { + collision |= wall_collision & LEFT_RIGHT + } + else if (bitcount(wall_collision) > 1) { + collision |= wall_collision + } + }) + return collision + } + + return Room + +})() + |
