diff options
| -rw-r--r-- | public/assets/javascripts/rectangles/engine/rooms/builder.js | 18 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/room.js | 76 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/wall.js | 37 | ||||
| -rwxr-xr-x | public/assets/stylesheets/app.css | 7 | ||||
| -rw-r--r-- | test/06-test-grouper.js | 121 | ||||
| -rw-r--r-- | test/mocks/mx.js | 2 |
6 files changed, 153 insertions, 108 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index dfabc86..1f21636 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -7,7 +7,7 @@ } else { MX = require('../../../../../../test/mocks/mx.js') - scene = MX.scene + scene = MX.Scene Rooms = require('./_rooms') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 @@ -43,7 +43,7 @@ if (window.scene) { base.clear() base.build() - base.bind_walls() + Rooms.grouper.group() } } @@ -62,18 +62,7 @@ }) }.bind(this)) } - - 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() - }) - }) - } - + base.clear = function (){ els.forEach(function(el){ scene.remove(el) @@ -84,7 +73,6 @@ this.build_walls = function (region) { var room = Rooms.list[ region.id ] - var list = [], el = null var width = region.x.length() diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index c2850ba..937c928 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -85,83 +85,7 @@ }) }) } - - 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(sort.compare_rect_x) - } - else if (side & FRONT_BACK) { - els.sort(sort.compare_rect_y) - } - - // wall holds state for the last wall we created/saw.. - // right now we keep walls subdivided where there are half-walls to simplify - // calculations involving placing media on walls, calculating wall boundaries, etc.. - // one consequence of this is we can't place things on the walls above "doorways". - // in the future, we should have more robust placement algorithm, which takes - // this into account, and only use one wall "object" per plane - 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 ({ - id: base.id + "_" + (el.side | el.half_side) + "_" + walls.length, - 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 diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 027d5f5..f2b8bca 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -1,21 +1,31 @@ -window.Wall = (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') + } + var wall_uid = new UidGenerator({}) + var Wall = function(opt){ this.id = opt.id - this.uid = Uid() - this.room = opt.room - this.rect = opt.rect || new Rect (0,0,0,0) - this.rect.sides = opt.side + this.uid = wall_uid + this.vec = opt.vec this.side = opt.side this.mx = [] - this.els = [] if (opt.el) { this.mx.push(opt.el) } } Wall.prototype.toString = function(){ - return this.rect.toString() + return this.vec.toString() } Wall.prototype.reset = function(){ @@ -25,7 +35,7 @@ window.Wall = (function(){ this.mx.forEach(function(mx){ mx.destroy && mx.destroy() }) - this.room = this.rect = this.mx = this.els = null + this.room = this.vec = this.mx = null } Wall.prototype.bind = function(){ @@ -146,8 +156,6 @@ window.Wall = (function(){ if (shouldFlip) { sortedWalls = sortedWalls.reverse() } - -console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) var len = sortedWalls.length @@ -184,7 +192,6 @@ console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) mx.el.style.backgroundImage = "url(" + dataUrl + ")" }) - window.zz += 1 } Wall.prototype.siblings = function(){ @@ -222,6 +229,12 @@ console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) }) } - return Wall + if ('window' in this) { + window.Wall = Wall + } + else { + module.exports = Wall + } + })() diff --git a/public/assets/stylesheets/app.css b/public/assets/stylesheets/app.css index a5b1733..de9cce6 100755 --- a/public/assets/stylesheets/app.css +++ b/public/assets/stylesheets/app.css @@ -581,11 +581,10 @@ iframe.embed { .back { background-color: #fff; } .left { background-color: #fff; } .right { background-color: #fff; } -.floor { background-color: #eee; } -.ceiling { background-color: rgba(230,230,255,0.3); } +.floor { background-color: #f6f6f6; } +.ceiling { background-color: rgba(255,255,255,0.9); } -.active.floor { background-color: #ffe; } -.active.ceiling { background-color: rgba(230,230,255,0.3); } +.active.floor { background-color: #f8f8f8; } .dragging .mx-object3d.image { pointer-events: none; diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js new file mode 100644 index 0000000..1707e4c --- /dev/null +++ b/test/06-test-grouper.js @@ -0,0 +1,121 @@ +var assert = require("assert") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Room = require("../public/assets/javascripts/rectangles/models/room.js") +var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js") +var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clipper.js") +var Builder = require("../public/assets/javascripts/rectangles/engine/rooms/builder.js") +var Grouper = require("../public/assets/javascripts/rectangles/engine/rooms/grouper.js") +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | 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 +} +function bitcount(v) { + v = v - ((v >>> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); + return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; +} + +var rect = new Rect( new vec(1,5), new vec(1,5) ) +var east = new Rect( new vec(2,6), new vec(1,5) ) +var corner = new Rect( new vec(3,7), new vec(3,7) ) +var peninsula = new Rect( new vec(4,6), new vec(6,8) ) + +var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) +var east_room = new Room({ id: "east", rect: east, height: 2 }) +var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) +var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) + +var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) + +function report(a) { + console.log( a.join("\n") ) +} +function reportSides(walls) { + console.log(walls.map(function(w){ return sidesToString(w.side) }).join("\n")) +} +function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) + Rooms.list = {} + Rooms.regions = [] +} +function rebuild(){ + Rooms.clipper.solve_rects() + Rooms.builder.build() +} + +describe('grouper(rect)', function(){ + reset() + Rooms.add( rect_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect)', function(){ + it("should return 4 sets of 1 wall each", function(){ + assert.equal(1, collections[FRONT].length) + assert.equal(1, collections[BACK].length) + assert.equal(1, collections[LEFT].length) + assert.equal(1, collections[RIGHT].length) + }) + }) +}) + +describe('grouper(rect,east)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( east_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect, east)', function(){ + it("should find 3 walls on front/back, 1 wall on left/right", function(){ + assert.equal(3, collections[FRONT].length) + assert.equal(3, collections[BACK].length) + assert.equal(1, collections[LEFT].length) + assert.equal(1, collections[RIGHT].length) + }) + }) + + describe('#group(rect, east)', function(){ + var front_walls = Rooms.grouper.group([], collections, FRONT) + var back_walls = Rooms.grouper.group([], collections, BACK) + var left_walls = Rooms.grouper.group([], collections, LEFT) + var right_walls = Rooms.grouper.group([], collections, RIGHT) + + it("each side now has one wall", function(){ + assert.equal(1, left_walls.length) + assert.equal(1, right_walls.length) + assert.equal(1, front_walls.length) + assert.equal(1, back_walls.length) + }) + it("front wall is now 5 units long, contains 3 mx elements", function(){ + var wall = front_walls[0] + assert.equal(5, wall.vec.length()) + assert.equal(3, wall.mx.length) + }) + it("back wall is also 5 units long, contains 3 mx elements", function(){ + var wall = back_walls[0] + assert.equal(5, wall.vec.length()) + assert.equal(3, wall.mx.length) + }) + it("left wall is still 4 units long, contains 1 mx element", function(){ + var wall = left_walls[0] + assert.equal(4, wall.vec.length()) + assert.equal(1, wall.mx.length) + }) + }) +}) + + diff --git a/test/mocks/mx.js b/test/mocks/mx.js index 2a76ffc..889f4bc 100644 --- a/test/mocks/mx.js +++ b/test/mocks/mx.js @@ -16,7 +16,7 @@ MX.Object3D = function (klass) { MX.Scene = { els: [], add: function (el) { - els.push(el) + MX.Scene.els.push(el) } } |
