window.room = (function(){ var room = function(opt){ this.id = opt.id || clipper.rooms.length this.rect = opt.rect this.regions = [] this.height = opt.height || 200 this.focused = false this.$walls = $([]) this.$floor = $([]) this.$ceiling = $([]) } 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.$floor = $([]) this.$ceiling = $([]) } 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 this.regions.forEach(function(r){ if (! r.sides) return if ((r.sides & FRONT) && y < r.y.a && r.x.contains(x)) { collision |= FRONT } if ((r.sides & BACK) && r.y.b < y && r.x.contains(x)) { collision |= BACK } if ((r.sides & LEFT) && x < r.x.a && r.y.contains(y)) { collision |= LEFT } if ((r.sides & RIGHT) && r.x.b < x && r.y.contains(y)) { collision |= RIGHT } }) return collision } return room })() function bitcount(v) { v = v - ((v >>> 1) & 0x55555555); v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; }