diff options
Diffstat (limited to 'public/assets/javascripts/rectangles')
12 files changed, 878 insertions, 615 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js index e0033e3..29dee41 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js +++ b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js @@ -1,99 +1,139 @@ -var Rooms = new function(){ - - var base = this - - base.list = {} - base.walls = {} - base.regions = [] +(function(){ - base.init = function(){ - Rooms.builder.init() - Rooms.clipper.init() - Rooms.mover.init() + var vec2, Rect, Room, sort, UidGenerator, _ + if ('window' in this) { + vec2 = window.vec2 + Rect = window.Rect + Room = window.Room + sort = window.sort + UidGenerator = window.UidGenerator + _ = window._ } - - base.filter = function(f){ - return _.values(base.list).filter(f) + else { + vec2 = require('../../models/vec2') + Rect = require('../../models/rect') + Room = require('../../models/room') + UidGenerator = require('../../util/uid') + sort = require('../../util/sort') + _ = require('lodash') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + TOP = CEILING, BOTTOM = FLOOR + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } } + + var Rooms = new function(){ + + var base = this - base.add = function(room){ - base.list[room.id] = room - } + base.list = {} + base.walls = {} + base.regions = [] + + base.uid = new UidGenerator(base.list) + + base.init = function(){ + Rooms.builder.init() + Rooms.clipper.init() + Rooms.mover.init() + } - base.add_with_rect = function(rect){ - var room = new Room({ - rect: rect, - height: 500 - }) - base.add(room) - return room - } + base.filter = function(f){ + return _.values(base.list).filter(f) + } - base.remove = function(room){ - delete base.list[room.id] - Rooms.clipper.update() - } + base.add = function(room){ + base.list[room.id] = room + } - base.removeAll = function(){ - base.list = {} - base.regions = [] - Rooms.clipper.update() - } + base.add_with_rect = function(rect){ + var room = new Room({ + rect: rect, + height: 500 + }) + base.add(room) + return room + } - base.count = function(){ - return this.values().length - } + base.remove = function(room){ + delete base.list[room.id] + Rooms.clipper.update() + } - base.forEach = function(f){ - return base.values().forEach(f) - } + base.removeAll = function(){ + base.list = {} + base.regions = [] + Rooms.clipper.update() + } - base.map = function(f){ - return base.values().map(f) - } + base.count = function(){ + return this.values().length + } - base.values = function(){ - return _.values(base.list) - } + base.forEach = function(f){ + return base.values().forEach(f) + } - base.serialize = function(){ - var rooms = base.map(function(room){ - return room.serialize() - }) - return rooms - } + base.map = function(f){ + return base.values().map(f) + } - base.deserialize = function(rooms_data){ - rooms_data.forEach(function(data){ - var rect = new Rect(data.rect.x[0], data.rect.y[0], data.rect.x[1], data.rect.y[1]) - var room = new Room({ - id: data.id, - rect: rect, - height: data.height + base.values = function(){ + return _.values(base.list) + } + + base.serialize = function(){ + var rooms = base.map(function(room){ + return room.serialize() }) - base.add(room) - }) - Rooms.clipper.update() - } + return rooms + } + + base.deserialize = function(rooms_data){ + rooms_data.forEach(function(data){ + var rect = new Rect(data.rect.x[0], data.rect.y[0], data.rect.x[1], data.rect.y[1]) + var room = new Room({ + id: data.id, + rect: rect, + height: data.height + }) + base.add(room) + }) + Rooms.clipper.update() + } - base.serializeWalls = function(){ - return [] - } + base.serializeWalls = function(){ + return [] + } - base.deserializeWalls = function(walls_data){ - return [] - } + base.deserializeWalls = function(walls_data){ + return [] + } - base.uid = UidGenerator(base.list) + base.sorted_by_position = function(){ + return sort.rooms_by_position( base.values() ) + } + base.sorted_by_height = function(){ + return sort.rooms_by_height( base.values() ) + } + base.sorted_by_area = function(){ + return sort.rooms_by_area( base.values() ) + } - base.sorted_by_position = function(){ - return sort_rooms_by_position( base.values() ) } - base.sorted_by_height = function(){ - return sort_rooms_by_height( base.values() ) + + if ('window' in this) { + window.Rooms = Rooms } - base.sorted_by_area = function(){ - return sort_rooms_by_area( base.values() ) + else { + module.exports = Rooms } - -} +})() diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 49e55dc..dfabc86 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -1,295 +1,319 @@ +(function(){ -Rooms.builder = new function(){ - var base = this + var Rooms, sort + if ('window' in this) { + Rooms = window.Rooms + sort = window.sort + } + else { + MX = require('../../../../../../test/mocks/mx.js') + scene = MX.scene + Rooms = require('./_rooms') + sort = require('../../util/sort') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + PI = Math.PI + HALF_PI = PI/2 + TOP = CEILING, BOTTOM = FLOOR + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } + } + + Rooms.builder = new function(){ + var base = this - var els = [] + var els = [] - base.init = function(){ - base.bind() - } + base.init = function(){ + base.bind() + } - base.bind = function(){ - app.on("clip", rebuild) - } + base.bind = function(){ + app.on("clip", base.rebuild.bind(base)) + } - function rebuild(){ - if (window.scene) { - clear() - build() - bind() + base.rebuild = function(){ + if (window.scene) { + base.clear() + base.build() + base.bind_walls() + } } - } - function build (){ - Rooms.regions.forEach(function(region){ - build_walls(region).forEach(function(el){ - els.push(el) - scene.add(el) - }) - }) + + base.build = function (){ + Rooms.regions.forEach(function(region){ + this.build_walls(region).forEach(function(el){ + els.push(el) + scene.add(el) + }) + }.bind(this)) - Rooms.sorted_by_height().forEach(function(room){ - build_floors(room).forEach(function(el){ - els.push(el) - scene.add(el) - }) - }) - } + Rooms.sorted_by_height().forEach(function(room){ + this.build_floors(room).forEach(function(el){ + els.push(el) + scene.add(el) + }) + }.bind(this)) + } - function bind (){ - Rooms.forEach(function(room){ - room.walls = room.group_mx_walls() - room.walls.forEach(function(wall){ - Rooms.walls[ wall.id ] = wall - wall.bind() - wall.randomize_colors() + base.bind_walls = function (){ + Rooms.forEach(function(room){ + room.walls = room.group_mx_walls() + room.walls.forEach(function(wall){ + Rooms.walls[ wall.id ] = wall + wall.bind() + wall.randomize_colors() + }) }) - }) - } + } - function clear (){ - els.forEach(function(el){ - scene.remove(el) - el.destroy && el.destroy() - }) - els = [] - } + base.clear = function (){ + els.forEach(function(el){ + scene.remove(el) + el.destroy && el.destroy() + }) + els = [] + } - function build_walls (region){ - var room = Rooms.list[ region.id ] + this.build_walls = function (region) { + var room = Rooms.list[ region.id ] - var list = [], el = null + var list = [], el = null - var width = region.x.length() - var depth = region.y.length() - var height = room.height + var width = region.x.length() + var depth = region.y.length() + var height = room.height - if (region.sides & FRONT) { - el = make_wall('.front') - el.width = width - el.height = height - el.rotationY = PI - el.x = region.x.a + width/2 - el.y = height/2 - el.z = region.y.a - el.rect = region - el.side = FRONT - room.mx_walls.push(el) - list.push(el) - } - if (region.sides & BACK) { - var el = make_wall('.back') - el.width = width - el.height = height - el.rotationY = 0 - el.x = region.x.b - width/2 - el.y = height/2 - el.z = region.y.b - el.rect = region - el.side = BACK - room.mx_walls.push(el) - list.push(el) - } - if (region.sides & LEFT) { - el = make_wall('.left') - el.rotationY = HALF_PI - el.height = height - el.width = depth - el.x = region.x.a - el.y = height/2 - el.z = region.y.a + depth/2 - el.rect = region - el.side = LEFT - room.mx_walls.push(el) - list.push(el) - } - if (region.sides & RIGHT) { - el = make_wall('.right') - el.rotationY = -HALF_PI - el.height = height - el.width = depth - el.x = region.x.b - el.y = height/2 - el.z = region.y.b - depth/2 - el.rect = region - el.side = RIGHT - room.mx_walls.push(el) - list.push(el) + if (region.sides & FRONT) { + el = this.make_wall('.front') + el.width = width + el.height = height + el.rotationY = PI + el.x = region.x.a + width/2 + el.y = height/2 + el.z = region.y.a + el.rect = region + el.side = FRONT + room.mx_walls.push(el) + list.push(el) + } + if (region.sides & BACK) { + var el = this.make_wall('.back') + el.width = width + el.height = height + el.rotationY = 0 + el.x = region.x.b - width/2 + el.y = height/2 + el.z = region.y.b + el.rect = region + el.side = BACK + room.mx_walls.push(el) + list.push(el) + } + if (region.sides & LEFT) { + el = this.make_wall('.left') + el.rotationY = HALF_PI + el.height = height + el.width = depth + el.x = region.x.a + el.y = height/2 + el.z = region.y.a + depth/2 + el.rect = region + el.side = LEFT + room.mx_walls.push(el) + list.push(el) + } + if (region.sides & RIGHT) { + el = this.make_wall('.right') + el.rotationY = -HALF_PI + el.height = height + el.width = depth + el.x = region.x.b + el.y = height/2 + el.z = region.y.b - depth/2 + el.rect = region + el.side = RIGHT + room.mx_walls.push(el) + list.push(el) + } + + return list } - return list - } + this.build_floors = function (room){ + var list = [], el = null - function build_floors(room){ - var list = [], el = null - - var constructed = room.intersects.filter(function(room){ return room.constructed }) - sort_rooms_by_height(constructed) - - if (constructed.length > 0) { - // render the regions that don't intersect with anything we've already rendered - // if the height is different, calculate the overlapping sides and render half-walls - room.regions.forEach(function(region){ - var intersected = false - for (var i = 0; i < constructed.length; i++) { - if (constructed[i].rect.contains(region)) { - intersected = true - // r.sides = 0xf - // half_sides - } - else if (constructed[i].rect.intersects(region)) { - intersected = true - if (room.height < constructed[i].height) { - var ceiling_walls = make_ceiling_walls( room, constructed[i], region ) - list = list.concat(ceiling_walls) + var constructed = room.intersects.filter(function(room){ return room.constructed }) + sort.rooms_by_height(constructed) + + if (constructed.length > 0) { + // render the regions that don't intersect with anything we've already rendered + // if the height is different, calculate the overlapping sides and render half-walls + room.regions.forEach(function(region){ + var intersected = false + for (var i = 0; i < constructed.length; i++) { + if (constructed[i].rect.overlaps(region)) { + intersected = true + if (room.height < constructed[i].height) { + var ceiling_walls = this.make_ceiling_walls( room, constructed[i], region ) + list = list.concat(ceiling_walls) + } } } - } - if (! intersected) { - el = make_floor(room, region) - list.push( el ) - room.mx_floor.push(el) + if (! intersected) { + el = this.make_floor(room, region) + list.push( el ) + room.mx_floor.push(el) - el = make_ceiling(room, region) - list.push( el ) - room.mx_ceiling.push(el) - } - }) + el = this.make_ceiling(room, region) + list.push( el ) + room.mx_ceiling.push(el) + } + }.bind(this)) - } - else { - // render floor and ceiling for the entire rectangle - el = make_floor(room, room.rect) - list.push( el ) - room.mx_floor.push(el) + } + else { + // render floor and ceiling for the entire rectangle + el = this.make_floor(room, room.rect) + list.push( el ) + room.mx_floor.push(el) - el = make_ceiling(room, room.rect) - list.push( el ) - room.mx_ceiling.push(el) - } + el = this.make_ceiling(room, room.rect) + list.push( el ) + room.mx_ceiling.push(el) + } - room.constructed = true - return list - } + room.constructed = true + return list + } - function make_ceiling_walls( lo, hi, region ){ - var list = [] + this.make_ceiling_walls = function ( lo, hi, region ){ + var list = [] - var width = region.x.length() - var depth = region.y.length() - var height = hi.height - lo.height + var width = region.x.length() + var depth = region.y.length() + var height = hi.height - lo.height - if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) { - el = make_wall('.left') - el.rotationY = HALF_PI - el.height = height - el.width = depth - el.x = region.x.a - el.y = lo.height + height/2 - el.z = region.y.a + depth/2 - el.rect = region - list.push(el) - hi.mx_walls.push(el) - region.half_sides |= LEFT - el.half_side = LEFT - } + if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) { + el = this.make_wall('.left') + el.rotationY = HALF_PI + el.height = height + el.width = depth + el.x = region.x.a + el.y = lo.height + height/2 + el.z = region.y.a + depth/2 + el.rect = region + list.push(el) + hi.mx_walls.push(el) + region.half_sides |= LEFT + el.half_side = LEFT + } - if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) { - el = make_wall('.right') - el.rotationY = -HALF_PI - el.height = height - el.width = depth - el.x = region.x.b - el.y = lo.height + height/2 - el.z = region.y.b - depth/2 - el.rect = region - list.push(el) - hi.mx_walls.push(el) - region.half_sides |= RIGHT - el.half_side = RIGHT + if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) { + el = this.make_wall('.right') + el.rotationY = -HALF_PI + el.height = height + el.width = depth + el.x = region.x.b + el.y = lo.height + height/2 + el.z = region.y.b - depth/2 + el.rect = region + list.push(el) + hi.mx_walls.push(el) + region.half_sides |= RIGHT + el.half_side = RIGHT + } + + if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) { + el = this.make_wall('.front') + el.width = width + el.height = height + el.rotationY = PI + el.x = region.x.a + width/2 + el.y = lo.height + height/2 + el.z = region.y.a + el.rect = region + list.push(el) + hi.mx_walls.push(el) + region.half_sides |= FRONT + el.half_side = FRONT + } + + if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) { + el = this.make_wall('.back') + el.width = width + el.height = height + el.rotationY = 0 + el.x = region.x.b - width/2 + el.y = lo.height + height/2 + el.z = region.y.b + el.rect = region + list.push(el) + hi.mx_walls.push(el) + region.half_sides |= BACK + el.half_side = BACK + } + return list } - if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) { - el = make_wall('.front') + this.make_floor = function (room, region) { + var width = region.x.length() + var depth = region.y.length() + + var el = this.make_wall('.floor') + el.height = depth el.width = width - el.height = height - el.rotationY = PI el.x = region.x.a + width/2 - el.y = lo.height + height/2 - el.z = region.y.a + el.y = 0 + el.z = region.y.a + depth/2 + el.rotationX = PI/2 el.rect = region - list.push(el) - hi.mx_walls.push(el) - region.half_sides |= FRONT - el.half_side = FRONT + el.side = FLOOR + return el } + this.make_ceiling = function (room, region) { + var width = region.x.length() + var depth = region.y.length() + var height = room.height - if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) { - el = make_wall('.back') + var el = this.make_wall('.ceiling') + el.height = depth el.width = width - el.height = height - el.rotationY = 0 - el.x = region.x.b - width/2 - el.y = lo.height + height/2 - el.z = region.y.b + el.x = region.x.a + width/2 + el.y = height + el.z = region.y.a + depth/2 + el.rotationX = -PI/2 el.rect = region - list.push(el) - hi.mx_walls.push(el) - region.half_sides |= BACK - el.half_side = BACK + el.side = CEILING + return el } - return list - } - function make_floor(room, region){ - var width = region.x.length() - var depth = region.y.length() - - var el = make_wall('.floor') - el.height = depth - el.width = width - el.x = region.x.a + width/2 - el.y = 0 - el.z = region.y.a + depth/2 - el.rotationX = PI/2 - el.rect = region - el.side = FLOOR - return el - } - function make_ceiling(room, region){ - var width = region.x.length() - var depth = region.y.length() - var height = room.height + this.make_wall = function (klass) { + var el = new MX.Object3D(".face" + (klass || "")) + el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1 + el.z = el.y = el.x = 0 + el.side = 0 + el.type = "Face" + el.el.style.opacity = 1.0 + el.side = 0 + el.rect = null + el.destroy = function(){ + this.el = this.rect = null + } - var el = make_wall('.ceiling') - el.height = depth - el.width = width - el.x = region.x.a + width/2 - el.y = height - el.z = region.y.a + depth/2 - el.rotationX = -PI/2 - el.rect = region - el.side = CEILING - return el - } + // possible if walls are opaque + // el.el.classList.add("backface-hidden") - function make_wall(klass){ - var el = new MX.Object3D(".face" + (klass || "")) - el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1 - el.z = el.y = el.x = 0 - el.side = 0 - el.type = "Face" - el.el.style.opacity = 1.0 - el.side = 0 - el.rect = null - el.destroy = function(){ - this.el = this.rect = null + return el } - // possible if walls are opaque - // el.el.classList.add("backface-hidden") - - return el } -} - +})() diff --git a/public/assets/javascripts/rectangles/engine/rooms/clipper.js b/public/assets/javascripts/rectangles/engine/rooms/clipper.js index e2bb894..33e3a84 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/clipper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/clipper.js @@ -1,107 +1,158 @@ +(function(){ -Rooms.clipper = new function(){ - var base = this - - base.init = function(){ - base.bind() - base.update() + var Rooms, Tree, sort + if ('window' in this) { + Rooms = window.Rooms + Tree = window.Tree + sort = window.sort } - - base.bind = function(){ - map.ui && map.ui.mouse.tube.on("up", function(){ base.update() }) + else { + Rooms = require('./_rooms') + Tree = require('../../models/tree') + sort = require('../../util/sort') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + TOP = CEILING, BOTTOM = FLOOR + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } } - base.update = function(){ - base.solve_rects() - app.tube("clip") - } - - var rooms, regions + Rooms.clipper = new function(){ + var base = this + + base.init = function(){ + base.bind() + base.update() + } - // Given a set of overlapping rooms, clip any intersections, then cull any duplicate polygons - base.solve_rects = function(){ - if (Rooms.count() == 0) { - Rooms.regions = regions = [] - return + base.bind = function(){ + map.ui && map.ui.mouse.tube.on("up", function(){ base.update() }) } + + base.update = function(){ + base.solve_rects() + app.tube("clip") + } + + var regions + + // Given a set of overlapping rooms, clip any intersections, then cull any duplicate polygons + base.solve_rects = function(){ + if (Rooms.count() == 0) { + Rooms.regions = regions = [] + return + } - base.reset_rects() - base.clip_rects() - base.cull_rects() + base.reset_rects() + base.clip_rects() + var culled = base.cull_rects_iterative() - Rooms.regions = sort_rects_by_position(regions) - } + Rooms.regions = sort.rects_by_position(culled) + } - // Reset the clipping/culling states of each of the rooms - base.reset_rects = function(){ - Rooms.forEach(function(room){ - room.reset() - }) - } + // Reset the clipping/culling states of each of the rooms + base.reset_rects = function(){ + regions = [] + Rooms.forEach(function(room){ + room.reset() + }) + } - // Compare each room to the rooms it overlaps, and subdivide - base.clip_rects = function(){ - var rooms = Rooms.sorted_by_position() - regions = [] + // Compare each room to the rooms it overlaps, and subdivide + base.clip_rects = function(){ + var rooms = Rooms.sorted_by_position() - var left, right - for (var i = 0; i < rooms.length; i++) { - left = rooms[i] - for (var j = i+1; j < rooms.length; j++) { - right = rooms[j] - if (left.rect.intersects(right.rect)) { - left.clipTo(right.rect) - right.clipTo(left.rect) - left.intersects.push(right) - right.intersects.push(left) - } - if (left.rect.x.b < right.rect.x.a) { - break + var left, right + for (var i = 0; i < rooms.length; i++) { + left = rooms[i] + for (var j = i+1; j < rooms.length; j++) { + right = rooms[j] + if (left.rect.intersects(right.rect)) { + left.clipTo(right.rect) + right.clipTo(left.rect) + left.intersects.push(right) + right.intersects.push(left) + } + if (left.rect.x.b < right.rect.x.a) { + break + } } } + for (var i = 0; i < rooms.length; i++) { + rooms[i].regions = rooms[i].regions.filter(function(r){ return !!r }) + regions = regions.concat(rooms[i].regions) + } + + return regions } - for (var i = 0; i < rooms.length; i++) { - rooms[i].regions = rooms[i].regions.filter(function(r){ return !!r }) - regions = regions.concat(rooms[i].regions) - } - } - // Find overlapping regions (of the same size) and dedupe - base.cull_rects = function(){ - regions = sort_rects_by_area( regions ) + // Find overlapping regions (of the same size) and dedupe + base.cull_rects = function(){ + regions = sort.rects_by_area( regions ) - var ty = new Tree (regions[0].y.a, [regions[0]]) - var tx = new Tree (regions[0].x.a, ty) - var ttx, tty + var ty = new Tree (regions[0].y.a, [regions[0]]) + var tx = new Tree (regions[0].x.a, ty) + var ttx, tty - for (var i = 1; i < regions.length; i++) { - ttx = tx.add (regions[i].x.a, null) - if (ttx.data) { - tty = ttx.data.add (regions[i].y.a, null) - // duplicate polygon? - if (tty.data) { - tty.data.forEach(function(yy, ii){ - if (yy.intersects(regions[i])) { - if (yy.area() > regions[i].area()) { - regions[i].dupe = true + for (var i = 1; i < regions.length; i++) { + ttx = tx.add (regions[i].x.a, null) + if (ttx.data) { + tty = ttx.data.add (regions[i].y.a, null) + // duplicate polygon? + if (tty.data) { + tty.data.forEach(function(yy, ii){ + if (yy.intersects(regions[i])) { + if (yy.area() > regions[i].area()) { + regions[i].dupe = true + } + else { + yy.dupe = true + tty.data[ii] = regions[i] + } } - else { - yy.dupe = true - tty.data[ii] = regions[i] - } - } - }) + }) + } + else { + tty.data = [regions[i]] + } } else { - tty.data = [regions[i]] + ttx.data = new Tree (regions[i].y.a, [regions[i]]) } } - else { - ttx.data = new Tree (regions[i].y.a, [regions[i]]) + + return regions + } + + // Find overlapping regions and dedupe the smaller ones + base.cull_rects_iterative = function(){ + regions = sort.rects_by_area( regions ) + + var region_i, region_j, i, j, _len + + for (i = 0, _len = regions.length; i < _len-1; i++) { + region_i = regions[i] + for (j = i+1; j < _len; j++) { + region_j = regions[j] + if (region_j.dupe) continue; + if (region_i.overlaps(region_j)) { + region_i.dupe = true + } + } } + + return regions.filter(function(r){ return ! r.dupe }) } + + return base } - return base -} +})() diff --git a/public/assets/javascripts/rectangles/engine/rooms/mover.js b/public/assets/javascripts/rectangles/engine/rooms/mover.js index e67d9bc..7195fcc 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/mover.js +++ b/public/assets/javascripts/rectangles/engine/rooms/mover.js @@ -43,15 +43,8 @@ Rooms.mover = new function(){ var collision = base.room.collidesDisc(pos.x, pos.z, radius) if (collision) { - if (! (collision & LEFT_RIGHT)) { - cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - else { - // cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - if (! (collision & FRONT_BACK)) { - cam.z = base.room.rect.y.clampDisc(pos.z, radius) - } + cam.x = (collision & LEFT_RIGHT) ? base.room.rect.x.clampDisc(pos.x, radius) : pos.x + cam.z = (collision & FRONT_BACK) ? base.room.rect.y.clampDisc(pos.z, radius) : pos.z return } diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index abd14ba..fe5f037 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -40,7 +40,7 @@ var Scenery = new function(){ media && media.destroy() } - base.uid = UidGenerator(base.list) + base.uid = new UidGenerator(base.list) base.forEach = function(f){ return base.values().forEach(f) diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 315adef..590440a 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -1,5 +1,25 @@ (function(){ + var vec2 + if ('window' in this) { + vec2 = window.vec2 + } + else { + vec2 = require('./vec2') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + TOP = CEILING, BOTTOM = FLOOR + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } + } + var Rect = function (x0,y0,x1,y1){ if (x0 instanceof vec2) { this.x = x0 @@ -74,8 +94,18 @@ Rect.prototype.containsDisc = function(x,y,r){ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } + Rect.prototype.overlaps = function(rect){ + return this.x.overlaps(rect.x) && this.y.overlaps(rect.y) + } Rect.prototype.intersects = function(r){ - return this.x.intersects(r.x) && this.y.intersects(r.y) + var corner_intersect = (this.x.b === r.x.a && this.y.b === r.y.a) + return this.x.intersects(r.x) && this.y.intersects(r.y) && ! corner_intersect + } + Rect.prototype.adjacent = function(r){ + return this.x.adjacent(r.x) && this.y.adjacent(r.y) + } + Rect.prototype.eq = function(r){ + return this.x.eq(r.x) && this.y.eq(r.y) } Rect.prototype.nearEdge = function (x, y, r) { var edges = 0 @@ -111,79 +141,51 @@ Rect.prototype.split = function(r){ var rz = this var splits = [] - var split_contains = 0 - var x_intervals = [], y_intervals = [] var sides = this.sides - // Split vertically - if (this.x.contains(r.x.a) && r.x.contains(this.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.a ), LEFT ]) - x_intervals.push([ new vec2( r.x.a, this.x.b ), RIGHT ]) - split_contains |= RIGHT - } - - else if (r.x.contains(this.x.a) && this.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.b ), LEFT ]) - x_intervals.push([ new vec2( r.x.b, this.x.b ), RIGHT ]) - split_contains |= LEFT - } - - else if (this.x.contains(r.x.a) && this.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, r.x.a ), LEFT ]) - x_intervals.push([ new vec2( r.x.a, r.x.b ), 0 ]) - x_intervals.push([ new vec2( r.x.b, this.x.b ), RIGHT ]) - split_contains |= LEFT | RIGHT - } - - else { // if (r.x.contains(this.x.a) && r.x.contains(r.x.b)) { - x_intervals.push([ new vec2( this.x.a, this.x.b ), LEFT | RIGHT ]) - split_contains |= LEFT | RIGHT - } - - // Split horizontally - if (this.y.contains(r.y.a) && r.y.contains(this.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.a ), FRONT ]) - y_intervals.push([ new vec2( r.y.a, this.y.b ), BACK ]) - split_contains |= BACK - } - - else if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.b ), FRONT ]) - y_intervals.push([ new vec2( r.y.b, this.y.b ), BACK ]) - split_contains |= FRONT - } - - else if (this.y.contains(r.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, r.y.a ), FRONT ]) - y_intervals.push([ new vec2( r.y.a, r.y.b ), 0 ]) - y_intervals.push([ new vec2( r.y.b, this.y.b ), BACK ]) - split_contains |= FRONT | BACK - } - - else { // if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) { - y_intervals.push([ new vec2( this.y.a, this.y.b ), FRONT | BACK ]) - split_contains |= FRONT | BACK - } + // bisect (or trisect) two overlapping rectangles + var x_intervals = this.x.split( r.x, LEFT, RIGHT ) + var y_intervals = this.y.split( r.y, FRONT, BACK ) - x_intervals.forEach(function(x){ - y_intervals.forEach(function(y){ + // generate rectangular regions by crossing the two sets of vectors + x_intervals.forEach(function(x, i){ + y_intervals.forEach(function(y, i){ var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) - if (r.intersects(rn)) { - rn.sides = 0 - } rn.focused = rz.focused splits.push(rn) + + // cull extra walls from overlapping regions + if (r.x.contains(rn.x.a) && r.x.contains(rn.x.b)) { + if (rz.y.a == rn.y.a && r.y.containsCenter(rn.y.a)) { // top edges + rn.sides &= ~ FRONT + } + if (rz.y.b == rn.y.b && r.y.containsCenter(rn.y.b)) { // bottom edges + rn.sides &= ~ BACK + } + } + + if (r.y.contains(rn.y.a) && r.y.contains(rn.y.b)) { + if (rz.x.a == rn.x.a && r.x.containsCenter(rn.x.a)) { // left edges + rn.sides &= ~ LEFT + } + + if (rz.x.b == rn.x.b && r.x.containsCenter(rn.x.b) ) { // right edges + rn.sides &= ~ RIGHT + } + } + }) }) + return splits } if ('window' in this) { window.Rect = Rect } - else if ('module' in this) { + else { module.exports = Rect } diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index 61a7447..d19ca2f 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -1,4 +1,32 @@ -window.Room = (function(){ + +(function(){ + var vec2, Rect, sort + if ('window' in this) { + vec2 = window.vec2 + Rect = window.Rect + sort = window.sort + } + else { + vec2 = require('./vec2') + Rect = require('./rect') + UidGenerator = require('../util/uid') + Rooms = { uid: new UidGenerator({}) } + sort = require('../util/sort') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + TOP = CEILING, BOTTOM = FLOOR + FRONT_BACK = FRONT | BACK + LEFT_RIGHT = LEFT | RIGHT + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } + } var Room = function(opt){ this.id = opt.id || Rooms.uid("room_") @@ -81,10 +109,10 @@ window.Room = (function(){ var side = pair[0], els = pair[1] if (side & LEFT_RIGHT) { - els.sort(compare_x) + els.sort(sort.compare_x) } else if (side & FRONT_BACK) { - els.sort(compare_z) + els.sort(sort.compare_z) } // wall holds state for the last wall we created/saw.. @@ -212,17 +240,22 @@ window.Room = (function(){ if (contains_x) { collision |= wall_collision & FRONT_BACK } - else if (contains_y) { + if (contains_y) { collision |= wall_collision & LEFT_RIGHT } - else if (bitcount(wall_collision) > 1) { - collision |= wall_collision - } +// if (bitcount(wall_collision) > 1) { +// collision |= wall_collision +// } }) return collision } - return Room + if ('window' in this) { + window.Room = Room + } + else { + module.exports = Room + } })() diff --git a/public/assets/javascripts/rectangles/models/tree.js b/public/assets/javascripts/rectangles/models/tree.js index 8193988..7c698fe 100644 --- a/public/assets/javascripts/rectangles/models/tree.js +++ b/public/assets/javascripts/rectangles/models/tree.js @@ -1,37 +1,48 @@ -var Tree = function(n, data){ - this.lo = null - this.hi = null - this.value = n - this.data = data -} -Tree.prototype.find = function(n){ - if (n == this.value) return this - if (n < this.value) return this.lo ? this.lo.find(n) : this - if (n > this.value) return this.hi ? this.hi.find(n) : this -} -Tree.prototype.add = function(n, data){ - var closest = this.find(n) - if (n == closest.value) return closest - if (n < closest.value) return closest.lo = new Tree(n, data) - if (n > closest.value) return closest.hi = new Tree(n, data) -} -Tree.prototype.toArray = function(){ - var a = [] - if (this.lo) a = a.concat(this.lo.toArray()) - a.push(this.data) - if (this.hi) a = a.concat(this.hi.toArray()) - return a -} -Tree.prototype.toString = function(){ - var s = ""; - if (this.lo) s += this.lo.toString() - s += this.value + "," - if (this.hi) s += this.hi.toString() - return s -} -Tree.prototype.depth = function(){ - if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth()) - else if (this.lo) return 1 + this.lo.depth() - else if (this.hi) return 1 + this.hi.depth() - else return 0 -} +(function(){ + + var Tree = function(n, data){ + this.lo = null + this.hi = null + this.value = n + this.data = data + } + Tree.prototype.find = function(n){ + if (n == this.value) return this + if (n < this.value) return this.lo ? this.lo.find(n) : this + if (n > this.value) return this.hi ? this.hi.find(n) : this + } + Tree.prototype.add = function(n, data){ + var closest = this.find(n) + if (n == closest.value) return closest + if (n < closest.value) return closest.lo = new Tree(n, data) + if (n > closest.value) return closest.hi = new Tree(n, data) + } + Tree.prototype.toArray = function(){ + var a = [] + if (this.lo) a = a.concat(this.lo.toArray()) + a.push(this.data) + if (this.hi) a = a.concat(this.hi.toArray()) + return a + } + Tree.prototype.toString = function(){ + var s = ""; + if (this.lo) s += this.lo.toString() + s += this.value + "," + if (this.hi) s += this.hi.toString() + return s + } + Tree.prototype.depth = function(){ + if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth()) + else if (this.lo) return 1 + this.lo.depth() + else if (this.hi) return 1 + this.hi.depth() + else return 0 + } + + if ('window' in this) { + window.Tree = Tree + } + else { + module.exports = Tree + } + +})() diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 9233aec..2bf286b 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -1,4 +1,6 @@ (function(){ + function clamp(n,a,b){ return n<a?a:n<b?n:b } + var vec2 = function (a,b){ this.a = a this.b = b @@ -7,7 +9,7 @@ return this.b-this.a } vec2.prototype.length = function(){ - return abs(this.b-this.a) + return Math.abs(this.b-this.a) } vec2.prototype.dist = function(){ return dist(0,this.a,0,this.b) @@ -70,6 +72,9 @@ vec2.prototype.contains = function(n){ return this.a <= n && n <= this.b } + vec2.prototype.containsCenter = function(n){ + return this.a < n && n < this.b + } vec2.prototype.containsDisc = function(n,r){ return this.a <= n-r && n+r <= this.b } @@ -80,16 +85,35 @@ return clamp(n, this.a+r, this.b-r) } vec2.prototype.intersects = function(v){ - if (this.a < v.a) { - return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b) - } - else if (this.a == v.a) { + if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) { return true } + else if (this.a < v.a) { + return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b) + } else if (this.a > v.a) { return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b) } } + vec2.prototype.overlaps = function(v){ + if (this.a == v.a || this.b == v.b) { + return true + } + if (this.a < v.a) { + return (v.a < this.b && this.b < v.b) || (this.a < v.b && v.b < this.b) + } + else if (v.a < this.a) { + return (this.a < v.b && v.b < this.b) || (v.a < this.b && this.b < v.b) + } + } + + + vec2.prototype.adjacent = function(v){ + if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) { + return true + } + return false + } vec2.prototype.union = function(v){ if (this.intersects(v)) { return new vec2( min(this.a,v.a), max(this.b, v.b) ) @@ -100,6 +124,56 @@ return new vec2( max(this.a,v.a), min(this.b, v.b) ) } } + + // given two vectors, test how they overlap + // return the set of overlapping segments in the initial vector, labelled with sides + vec2.prototype.split = function(v, left, right){ + var intervals = [], _len + + if (this.eq(v)) { + intervals.push([ new vec2( this.a, this.b ), left | right ]) + } + + // a---A===b---B (rightways overlap) + else if (this.contains(v.a) && v.contains(this.b)) { + intervals.push([ new vec2( this.a, v.a ), left ]) + intervals.push([ new vec2( v.a, this.b ), right ]) + } + + // A---a===B---b (leftways overlap) + else if (v.contains(this.a) && this.contains(v.b)) { + intervals.push([ new vec2( this.a, v.b ), left ]) + intervals.push([ new vec2( v.b, this.b ), right ]) + } + + // a---A===B---b (contains v) + else if (this.contains(v.a) && this.contains(v.b)) { + intervals.push([ new vec2( this.a, v.a ), left ]) + intervals.push([ new vec2( v.a, v.b ), 0 ]) + intervals.push([ new vec2( v.b, this.b ), right ]) + } + + // A---a===b---B (contained in v) + else { // if (v.contains(this.a) && v.contains(v.b)) { + intervals.push([ new vec2( this.a, this.b ), left | right ]) + } + + // cull empty vectors + _len = intervals.length + if (_len > 1) { + if (intervals[0][0].magnitude() == 0) { + intervals[1][1] |= intervals[0][1] + intervals.shift() + } + else if (intervals[_len-1][0].magnitude() == 0) { + intervals[_len-2][1] |= intervals[_len-1][1] + intervals.pop() + } + } + + return intervals + } + vec2.prototype.toString = function(){ return "[" + ~~this.a + " " + ~~this.b + "]" } @@ -115,8 +189,7 @@ if ('window' in this) { window.vec2 = vec2 } - else if ('module' in this) { + else { module.exports = vec2 } - -})()
\ No newline at end of file +})() diff --git a/public/assets/javascripts/rectangles/util/colors.js b/public/assets/javascripts/rectangles/util/colors.js index c590072..95827cc 100644 --- a/public/assets/javascripts/rectangles/util/colors.js +++ b/public/assets/javascripts/rectangles/util/colors.js @@ -3,6 +3,12 @@ alpha: [ "rgba(0,0,0,0.1)", ], + alphaQuad: [ + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + "rgba(0,0,0,0.1)", + ], redblue: [ "rgba(0,0,0,0.2)", "rgba(255,0,0,0.2)", @@ -52,9 +58,9 @@ select.blur() }) - window.colors = color_palettes[select ? select.value : 'bone'] + window.colors = color_palettes[select ? select.value : 'alphaQuad'] window.grayColors = {} - _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.bone).map(function(pair){ + _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.alphaQuad).map(function(pair){ window.grayColors[pair[0]] = pair[1] }) window.palettes = color_palettes diff --git a/public/assets/javascripts/rectangles/util/sort.js b/public/assets/javascripts/rectangles/util/sort.js index a0665ae..3b4771c 100644 --- a/public/assets/javascripts/rectangles/util/sort.js +++ b/public/assets/javascripts/rectangles/util/sort.js @@ -1,86 +1,101 @@ +(function(){ + function compare_rect_position(a,b){ + if (a[0].x.a < b[0].x.a) { + return -1 + } + if (a[0].x.a > b[0].x.a) { + return 1 + } + if (a[0].y.a < b[0].y.a) { + return -1 + } + if (a[0].y.a > b[0].y.a) { + return 1 + } + return 0 + } -function compare_rect_position(a,b){ - if (a[0].x.a < b[0].x.a) { - return -1 + function compare_car_reversed (a,b){ + if (a[0] < b[0]) { + return 1 + } + if (a[0] > b[0]) { + return -1 + } + return 0 } - if (a[0].x.a > b[0].x.a) { - return 1 + function compare_car (a,b){ + if (a[0] < b[0]) { + return -1 + } + if (a[0] > b[0]) { + return 1 + } + return 0 } - if (a[0].y.a < b[0].y.a) { - return -1 + + function room_id_tuple (r){ return [r.id, r] } + function room_height_tuple (r){ return [r.height, r] } + function room_area_tuple (r){ return [r.rect.area(), r] } + function rect_area_tuple (r){ return [r.area(), r] } + function rect_area_tuple_larger (r){ return [-r.area(), r] } + + function room_rect_tuple (r){ return [r.rect, r] } + function identity_tuple (r){ return [r, r] } + function car (r){ return r[0] } + function cdr (r){ return r[1] } + + var sort = {} + + sort.rooms_by_id = function (list){ + return list.map(room_id_tuple) + .sort(compare_car) + .map(cdr) } - if (a[0].y.a > b[0].y.a) { - return 1 + sort.rooms_by_height = function (list){ + return list.map(room_height_tuple) + .sort(compare_car_reversed) + .map(cdr) + } + sort.rooms_by_position = function (list){ + return list.map(room_rect_tuple) + .sort(compare_rect_position) + .map(cdr) + } + sort.rooms_by_area = function (list){ + return list.map(room_area_tuple) + .sort(compare_car) + .map(cdr) } - return 0 -} -function compare_car_reversed (a,b){ - if (a[0] < b[0]) { - return 1 + sort.rects_by_position = function (list){ + return list.map(identity_tuple) + .sort(compare_rect_position) + .map(cdr) } - if (a[0] > b[0]) { - return -1 + sort.rects_by_area = function (list){ + return list.map(rect_area_tuple) + .sort(compare_car) + .map(cdr) } - return 0 -} -function compare_car (a,b){ - if (a[0] < b[0]) { - return -1 + sort.rects_by_larger_area = function (list){ + return list.map(rect_area_tuple_larger) + .sort(compare_car) + .map(cdr) } - if (a[0] > b[0]) { - return 1 + sort.compare_z = function (a,b){ + return a.rect.y.a < b.rect.y.a ? -1 : a.rect.y.a == b.rect.y.a ? 0 : 1 + } + sort.compare_x = function (a,b){ + return a.rect.x.a > b.rect.x.a ? -1 : a.rect.x.a == b.rect.x.a ? 0 : 1 } - return 0 -} - -function room_id_tuple (r){ return [r.id, r] } -function room_height_tuple (r){ return [r.height, r] } -function room_area_tuple (r){ return [r.rect.area(), r] } -function rect_area_tuple (r){ return [r.area(), r] } - -function room_rect_tuple (r){ return [r.rect, r] } -function identity_tuple (r){ return [r, r] } -function car (r){ return r[0] } -function cdr (r){ return r[1] } - - -function sort_rooms_by_id(list){ - return list.map(room_id_tuple) - .sort(compare_car) - .map(cdr) -} -function sort_rooms_by_height(list){ - return list.map(room_height_tuple) - .sort(compare_car_reversed) - .map(cdr) -} -function sort_rooms_by_position(list){ - return list.map(room_rect_tuple) - .sort(compare_rect_position) - .map(cdr) -} -function sort_rooms_by_area(list){ - return list.map(room_area_tuple) - .sort(compare_car) - .map(cdr) -} -function sort_rects_by_position(list){ - return list.map(identity_tuple) - .sort(compare_rect_position) - .map(cdr) -} -function sort_rects_by_area(list){ - return list.map(rect_area_tuple) - .sort(compare_car) - .map(cdr) -} + if ("window" in this) { + window.sort = sort + } + else { + module.exports = sort + } -function compare_z(a,b){ - return a.rect.y.a < b.rect.y.a ? -1 : a.rect.y.a == b.rect.y.a ? 0 : 1 -} -function compare_x(a,b){ - return a.rect.x.a > b.rect.x.a ? -1 : a.rect.x.a == b.rect.x.a ? 0 : 1 -} +})() diff --git a/public/assets/javascripts/rectangles/util/uid.js b/public/assets/javascripts/rectangles/util/uid.js index ca22fb3..0c0b176 100644 --- a/public/assets/javascripts/rectangles/util/uid.js +++ b/public/assets/javascripts/rectangles/util/uid.js @@ -1,14 +1,29 @@ +(function(){ -var UidGenerator = function(list){ - var id = 0 - return function(s){ - s = s || "" - var ss - while (1) { - ss = s + (id++) - if (! (ss in list)) { - return ss + var UidGenerator = function(list){ + var id = 0 + var generator = function(s){ + s = s || "" + var ss + while (1) { + ss = s + (id++) + if (! (ss in list)) { + return ss + } } } + generator.setList = function(newList){ + list = newList + } + return generator + } + + if ('window' in this) { + window.UidGenerator = UidGenerator } -}
\ No newline at end of file + else { + module.exports = UidGenerator + } + +})() + |
