From be7412073a5e0b6641f125f29d254315f50bc95c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 1 Aug 2014 18:03:20 -0400 Subject: tests for grouper --- test/06-test-grouper.js | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 test/06-test-grouper.js (limited to 'test/06-test-grouper.js') 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) + }) + }) +}) + + -- cgit v1.2.3-70-g09d2 From e2e1565f5cd3154c0f515dbe3d88c9943dd0580e Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 4 Aug 2014 17:41:04 -0400 Subject: group walls and render using 'wireframe' look --- .../javascripts/rectangles/engine/rooms/builder.js | 2 +- .../javascripts/rectangles/engine/rooms/grouper.js | 147 +++++++++++++++++++++ .../assets/javascripts/rectangles/models/wall.js | 39 +++--- .../assets/javascripts/rectangles/util/colors.js | 3 +- public/assets/javascripts/rectangles/util/sort.js | 33 ++++- server/index.js | 2 +- test/06-test-grouper.js | 129 +++++++++++++++++- views/partials/scripts.ejs | 1 + 8 files changed, 325 insertions(+), 31 deletions(-) create mode 100644 public/assets/javascripts/rectangles/engine/rooms/grouper.js (limited to 'test/06-test-grouper.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 1f21636..6b2e65a 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -43,7 +43,7 @@ if (window.scene) { base.clear() base.build() - Rooms.grouper.group() + Rooms.grouper.build() } } diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js new file mode 100644 index 0000000..532146d --- /dev/null +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -0,0 +1,147 @@ +(function(){ + + var Rooms, UidGenerator, Wall, sort + if ('window' in this) { + Rooms = window.Rooms + UidGenerator = window.UidGenerator + Wall = window.Wall + sort = window.sort + } + else { + Rooms = require('./_rooms') + UidGenerator = require('../../util/uid') + Wall = require('../../models/wall') + 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.grouper = new function(){ + + var base = this + + base.list = {} + + base.uid = new UidGenerator(base.list) + + base.build = function (){ + var walls = [] + var collections = base.collect() + base.cull(collections) + base.group(walls, collections, FRONT) + base.group(walls, collections, BACK) + base.group(walls, collections, LEFT) + base.group(walls, collections, RIGHT) + Rooms.walls = walls + base.bind() + } + base.collect = function(){ + var collections = {} + collections[FRONT] = [] + collections[BACK] = [] + collections[LEFT] = [] + collections[RIGHT] = [] + + Rooms.forEach(function(room){ + room.mx_walls.forEach(function(mx){ + var side = mx.side || mx.half_side + collections[side].push(mx) + }) + }) + + base.cull(collections) + + return collections + } + base.cull = function(collections){ + [FRONT, BACK, LEFT, RIGHT].forEach(function(side){ + var collection = collections[side] + var useX = side & FRONT_BACK + var useA = side & (FRONT | RIGHT) + var last_mx + + collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + collection.forEach(function(mx){ + if (last_mx && last_mx.rect.eq(mx.rect)) { +// console.log( mx.y - mx.height/2 ) +// console.log( last_mx.y - last_mx.height/2 ) +// console.log(last_mx.y, mx.y) +// console.log(last_mx.height, mx.height) +// console.log(Rooms.list[ last_mx.rect.id ].height, Rooms.list[ mx.rect.id ].height) +// console.log("___") +// last_mx.height += mx.height/2 +// last_mx.y += mx.height/4 + if (last_mx.rect.id == mx.rect.id) { + last_mx.height += mx.height/2 + last_mx.y += mx.height/4 + } + last_mx.side = side + + mx.culled = true + scene.remove(mx) + return + } + last_mx = mx + }) + }) + return collections + } + base.group = function(walls, collections, side){ + var collection = collections[side] + var wall + var useX = side & FRONT_BACK + var useA = side & (FRONT | RIGHT) + + // collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + + collection.forEach(function(mx){ + if (mx.culled) return + var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b'] + + if (wall && coplanar) { + if (useX && wall.vec.b == mx.rect.x.a) { + wall.vec.b = mx.rect.x.b + wall.mx.push(mx) + return + } + else if (! useX && wall.vec.b == mx.rect.y.a) { + wall.vec.b = mx.rect.y.b + wall.mx.push(mx) + return + } + } + wall = new Wall ({ + id: base.uid(), + side: side, + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + el: mx, + }) + walls.push(wall) + }) + + return walls + } + + base.bind = function(){ + Rooms.walls.forEach(function(wall){ + wall.bind() + wall.randomize_colors() + }) + } + + } + +})() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index f2b8bca..41d7235 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -17,6 +17,7 @@ this.id = opt.id this.uid = wall_uid this.vec = opt.vec + this.edge = opt.edge this.side = opt.side this.mx = [] if (opt.el) { @@ -148,20 +149,18 @@ var ctx = canvas.getContext('2d') var useX = this.side & FRONT_BACK var shouldFlip = this.side & (LEFT | BACK) + var mx = this.mx - var sortedWalls = this.siblings().reduce(function(a,w){ - return a.concat(w.mx) - }, []).sort( useX ? sort.compare_x : sort.compare_z ) - - if (shouldFlip) { - sortedWalls = sortedWalls.reverse() + // console.log( sidesToString(this.side), mx.length ) + if (! shouldFlip) { + mx = mx.reverse() } - var len = sortedWalls.length + var len = this.mx.length zz = window.zz || 0 - sortedWalls.forEach(function(mx, i){ + mx.forEach(function(mx, i){ if (mx.outlined) return mx.outlined = true canvas.width = mx.width @@ -170,10 +169,10 @@ ctx.fillRect(0, 0, canvas.width, canvas.height) ctx.fillStyle = "rgba(0,0,0,1.0)" - // all walls except top-half walls get bottom lines + // all walls get bottom lines ctx.fillRect(0, 0, canvas.width, 1) - // all walls except bottom-half walls get top lines + // all walls get top lines ctx.fillRect(0, canvas.height-1, canvas.width, 1) // walls on initial sides get left lines @@ -185,7 +184,6 @@ // walls on terminal sides get right lines.... // if their right edge lines up with the rect edge if (i == len-1) { - // ctx.fillStyle = "rgba(255,0,0,1.0)" ctx.fillRect(canvas.width-1, 0, 1, canvas.height) } var dataUrl = canvas.toDataURL() @@ -195,21 +193,18 @@ } Wall.prototype.siblings = function(){ - var base = this - var match = base.side | base.half_side - var walls = Rooms.list[this.room].walls.filter(function(w){ - return (w.side | w.half_side) & match && w.$walls - }) - return walls + return this +// var base = this +// var match = base.side | base.half_side +// var walls = Rooms.list[this.room].walls.filter(function(w){ +// return (w.side | w.half_side) & match && w.$walls +// }) +// return walls } Wall.prototype.randomize_colors = function(){ var color = window.grayColors[ this.side | this.half_side ] - var siblings = this.siblings() - siblings.forEach(function(w, i){ - if (! w.$walls) return - w.color(color) - }) + this.color(color) } Wall.prototype.stroke_colors = function(){ diff --git a/public/assets/javascripts/rectangles/util/colors.js b/public/assets/javascripts/rectangles/util/colors.js index 95827cc..16d34dd 100644 --- a/public/assets/javascripts/rectangles/util/colors.js +++ b/public/assets/javascripts/rectangles/util/colors.js @@ -58,7 +58,8 @@ select.blur() }) - window.colors = color_palettes[select ? select.value : 'alphaQuad'] + window.colors = color_palettes[select ? select.value : 'colors'] +// window.colors = color_palettes[select ? select.value : 'alphaQuad'] window.grayColors = {} _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.alphaQuad).map(function(pair){ window.grayColors[pair[0]] = pair[1] diff --git a/public/assets/javascripts/rectangles/util/sort.js b/public/assets/javascripts/rectangles/util/sort.js index 7aa40a2..cf8d6b1 100644 --- a/public/assets/javascripts/rectangles/util/sort.js +++ b/public/assets/javascripts/rectangles/util/sort.js @@ -95,9 +95,38 @@ return a.x < b.x ? -1 : a.x == b.x ? 0 : 1 } sort.compare_z = function (a,b){ - return a.z > b.z ? -1 : a.z == b.z ? 0 : 1 + return a.z < b.z ? -1 : a.z == b.z ? 0 : 1 } - + sort.compare_xz = function(a,b){ + if (a.x < b.x) { + return -1 + } + if (a.x > b.x) { + return 1 + } + if (a.z < b.z) { + return -1 + } + if (a.z > b.z) { + return 1 + } + return 0 + } + sort.compare_zx = function(a,b){ + if (a.z < b.z) { + return -1 + } + if (a.z > b.z) { + return 1 + } + if (a.x < b.x) { + return -1 + } + if (a.x > b.x) { + return 1 + } + return 0 + } if ("window" in this) { window.sort = sort } diff --git a/server/index.js b/server/index.js index 71cd862..ad437ff 100644 --- a/server/index.js +++ b/server/index.js @@ -50,7 +50,7 @@ site.setup = function(){ app.use(express.session({ key: 'vvalls.sid', secret: 'flibbertigibbet', - cookie: { domain: '.' + config.hostName, maxAge: 432000000 }, + cookie: { domain: '.' + config.hostName, maxAge: 43200000000 }, store: SessionStore })); app.use(bodyParser()); diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 1707e4c..41ed0b0 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -29,10 +29,12 @@ 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 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 peninsula_taller = new Room({ id: "peninsula", rect: peninsula, height: 3 }) +var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 }) var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) @@ -118,4 +120,123 @@ describe('grouper(rect,east)', function(){ }) }) +describe('grouper(rect,corner)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#group(rect,corner)', 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("left has 2 walls", function(){ + assert.equal(2, left_walls.length) + }) + it("right has 2 walls", function(){ + assert.equal(2, right_walls.length) + }) + it("front has 2 walls", function(){ + assert.equal(2, front_walls.length) + }) + it("back has 2 walls", function(){ + assert.equal(2, back_walls.length) + }) + it("front/right walls are one narrow, one wide", function(){ + assert.equal(4, front_walls[0].vec.length()) + assert.equal(2, front_walls[1].vec.length()) + assert.equal(2, right_walls[0].vec.length()) + assert.equal(4, right_walls[1].vec.length()) + }) + it("back/left walls are one wide, one narrow", function(){ + assert.equal(2, back_walls[0].vec.length()) + assert.equal(4, back_walls[1].vec.length()) + assert.equal(4, left_walls[0].vec.length()) + assert.equal(2, left_walls[1].vec.length()) + }) + }) +}) + +describe('grouper(rect,corner,peninsula)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + Rooms.add( peninsula_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect,corner,peninsula)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(3, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(5, collections[LEFT].length) + assert.equal(5, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula)', 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("left has 3 walls", function(){ + assert.equal(3, left_walls.length) + }) + it("right has 4 walls", function(){ + assert.equal(4, right_walls.length) + }) + it("front has 2 walls", function(){ + assert.equal(2, front_walls.length) + }) + it("back has 4 walls", function(){ + assert.equal(4, back_walls.length) + }) + }) +}) + + +describe('grouper(rect,corner,peninsula_taller)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + Rooms.add( peninsula_taller ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect,corner,peninsula_taller)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(5, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(6, collections[LEFT].length) + assert.equal(6, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula_taller)', 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("left has 4 walls", function(){ + assert.equal(4, left_walls.length) + }) + it("right has 5 walls", function(){ + assert.equal(5, right_walls.length) + }) + it("front has 3 walls", function(){ + assert.equal(3, front_walls.length) + }) + it("back has 4 walls", function(){ + assert.equal(4, back_walls.length) + }) + }) +}) diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 0133ad0..915027d 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -38,6 +38,7 @@ + -- cgit v1.2.3-70-g09d2 From a8307b74bd429227fe1f2a06d8fac5d61530608f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 5 Aug 2014 14:31:56 -0400 Subject: test bounds --- .../javascripts/rectangles/engine/rooms/builder.js | 2 +- .../javascripts/rectangles/engine/rooms/grouper.js | 34 +++++++------ .../rectangles/engine/scenery/_scenery.js | 20 +++++--- .../javascripts/rectangles/engine/scenery/move.js | 2 +- .../rectangles/engine/scenery/types/_object.js | 4 +- .../assets/javascripts/rectangles/models/wall.js | 59 ++++++++++++---------- test/06-test-grouper.js | 46 +++++++++++++++++ 7 files changed, 115 insertions(+), 52 deletions(-) (limited to 'test/06-test-grouper.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 6b2e65a..f321f71 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -293,7 +293,7 @@ el.side = 0 el.rect = null el.destroy = function(){ - this.el = this.rect = null + this.el = this.rect = this.face = null } // possible if walls are opaque diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index 532146d..ba081e3 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,7 +1,9 @@ (function(){ - var Rooms, UidGenerator, Wall, sort + var vec2, Rect, Rooms, UidGenerator, Wall, sort if ('window' in this) { + vec2 = window.vec2 + Rect = window.Rect Rooms = window.Rooms UidGenerator = window.UidGenerator Wall = window.Wall @@ -10,6 +12,8 @@ else { Rooms = require('./_rooms') UidGenerator = require('../../util/uid') + vec2 = require('../../models/vec2') + Rect = require('../../models/rect') Wall = require('../../models/wall') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 @@ -71,28 +75,25 @@ var useX = side & FRONT_BACK var useA = side & (FRONT | RIGHT) var last_mx + var widthVec, heightVec collection.sort( useX ? sort.compare_zx : sort.compare_xz ) collection.forEach(function(mx){ if (last_mx && last_mx.rect.eq(mx.rect)) { -// console.log( mx.y - mx.height/2 ) -// console.log( last_mx.y - last_mx.height/2 ) -// console.log(last_mx.y, mx.y) -// console.log(last_mx.height, mx.height) -// console.log(Rooms.list[ last_mx.rect.id ].height, Rooms.list[ mx.rect.id ].height) -// console.log("___") -// last_mx.height += mx.height/2 -// last_mx.y += mx.height/4 if (last_mx.rect.id == mx.rect.id) { last_mx.height += mx.height/2 last_mx.y += mx.height/4 + last_mx.face.y.b += mx.height/2 } last_mx.side = side - mx.culled = true + mx.destroy() scene.remove(mx) return } + widthVec = mx.rect[useX ? 'x' : 'y'].clone() + heightVec = new vec2( mx.y - mx.height/2, mx.y + mx.height/2 ) + mx.face = new Rect( widthVec, heightVec ) last_mx = mx }) }) @@ -114,20 +115,23 @@ if (useX && wall.vec.b == mx.rect.x.a) { wall.vec.b = mx.rect.x.b wall.mx.push(mx) + wall.faces.push(mx.face) return } else if (! useX && wall.vec.b == mx.rect.y.a) { wall.vec.b = mx.rect.y.b wall.mx.push(mx) + wall.faces.push(mx.face) return } } wall = new Wall ({ - id: base.uid(), - side: side, - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], - el: mx, + id: base.uid(), + side: side, + mx: [ mx ], + faces: [ mx.face ], + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], }) walls.push(wall) }) diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index fe5f037..137c74a 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -12,25 +12,29 @@ var Scenery = new function(){ base.resize.init() } - base.add = function(wall, media, id){ + base.add = function(opt){ var scene_media switch (media.type) { case 'image': - scene_media = new Scenery.types.image ({ media: media, wall: wall, id: id }) + scene_media = new Scenery.types.image (opt) break case 'video': case 'youtube': case 'vimeo': - scene_media = new Scenery.types.video ({ media: media, wall: wall, id: id }) + scene_media = new Scenery.types.video (opt) break } base.list[scene_media.id] = scene_media return scene_media } - base.addNextToWall = function(wall){ - base.add(wall, base.nextMedia) + base.addNextToWall = function(wall, mx){ + base.add({ + wall: wall, + media: base.nextMedia, + mx: mx + }) base.nextMedia = null } @@ -64,7 +68,11 @@ var Scenery = new function(){ base.deserialize = function(scenery_data){ scenery_data.forEach(function(data){ var wall = Rooms.walls[data.wall_id] - var scene_media = base.add(wall, data.media, data.id) + var scene_media = base.add({ + wall: wall, + media: data.media, + id: data.id + }) scene_media.deserialize(data) }) } diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index 94a4e52..cc5b014 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -69,7 +69,7 @@ Scenery.move = function(base){ function switch_wall (e, new_wall, cursor){ if (! dragging) return - if (new_wall.uid == base.wall.uid) return + if (new_wall.id == base.wall.id) return if (! new_wall.fits(base.media, base.scale)) return var old_wall_side = base.wall.side diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 7202ce0..aa1fefb 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -12,7 +12,7 @@ Scenery.types.base = Fiber.extend(function(base){ this.scale = this.media.scale if (opt.wall) { - this.set_wall(opt.wall) + this.set_wall(opt.wall, opt.mx) } }, @@ -60,7 +60,7 @@ Scenery.types.base = Fiber.extend(function(base){ } }, - set_wall: function(wall){ + set_wall: function(wall, mx){ this.wall = wall || this.wall this.bounds = this.wall.bounds_for(this.media, this.scale) this.center = this.wall.center() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 41d7235..bbe6b97 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -11,18 +11,14 @@ Rect = require('./rect') UidGenerator = require('../util/uid') } - var wall_uid = new UidGenerator({}) var Wall = function(opt){ this.id = opt.id - this.uid = wall_uid this.vec = opt.vec this.edge = opt.edge this.side = opt.side - this.mx = [] - if (opt.el) { - this.mx.push(opt.el) - } + this.faces = opt.faces + this.mx = opt.mx } Wall.prototype.toString = function(){ @@ -42,27 +38,30 @@ Wall.prototype.bind = function(){ var base = this base.$walls = $( this.mx.map(function(mx){ return mx.el }) ) - base.$walls.bind({ - mouseover: function(){ - }, - mouseenter: function(e){ - Scenery.mouse.mouseenter(e, base) - }, - mousemove: function(e){ - }, - mousedown: function(){ - // base.randomize_colors() - // console.log(sidesToString(base.side)) - if (Scenery.nextMedia) { - Scenery.addNextToWall(base) - } - else if (Scenery.nextWallpaper) { - base.wallpaper() - } - else { - app.controller.hideExtras() + + this.mx.forEach(function(mx){ + $(mx.el).bind({ + mouseover: function(){ + }, + mouseenter: function(e){ + Scenery.mouse.mouseenter(e, base, mx) + }, + mousemove: function(e){ + }, + mousedown: function(){ + // base.randomize_colors() + // console.log(sidesToString(base.side)) + if (Scenery.nextMedia) { + Scenery.addNextToWall(base, mx) + } + else if (Scenery.nextWallpaper) { + base.wallpaper() + } + else { + app.controller.hideExtras() + } } - } + }) }) this.outline() } @@ -77,6 +76,12 @@ new vec2( halfHeight, Rooms.list[this.room].height - halfHeight ) ) } + Wall.prototype.bounds_for_mx = function(img, scale, mx) { + // + } + Wall.prototype.fits_mx = function(img, scale, mx) { + } + Wall.prototype.fits = function(img, scale){ if (this.side & FRONT_BACK && this.rect.x.length() < img.width * scale) { return false @@ -165,7 +170,7 @@ mx.outlined = true canvas.width = mx.width canvas.height = mx.height - ctx.fillStyle = "rgba(255,255,255,0.9)" + ctx.fillStyle = "rgba(255,255,255,0.95)" ctx.fillRect(0, 0, canvas.width, canvas.height) ctx.fillStyle = "rgba(0,0,0,1.0)" diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 41ed0b0..5388875 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -29,6 +29,9 @@ 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 big_rect = new Rect( new vec(1,5), new vec(1,5) ) +var hall_rect = new Rect( new vec(3,4), new vec(4,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 }) @@ -38,6 +41,9 @@ var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 } var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) +var big_room = new Room({ id: "big_room", rect: big_rect, height: 4 }) +var hallway = new Room({ id: "hallway", rect: hall_rect, height: 2 }) + function report(a) { console.log( a.join("\n") ) } @@ -230,6 +236,7 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) it("right has 5 walls", function(){ assert.equal(5, right_walls.length) + // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) @@ -240,3 +247,42 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) }) +describe('grouper(room,hallway)', function(){ + reset() + Rooms.add( big_room ) + Rooms.add( hallway ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(room,hallway)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(3, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(3, collections[LEFT].length) + assert.equal(3, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula_taller)', 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("left has 2 walls", function(){ + assert.equal(2, left_walls.length) + }) + it("right has 2 walls", function(){ + assert.equal(2, right_walls.length) + }) + it("front has 1 wall", function(){ + assert.equal(1, front_walls.length) + }) + it("back has 2 walls", function(){ + assert.equal(2, back_walls.length) + // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + }) + }) +}) + -- cgit v1.2.3-70-g09d2 From 50ad04ca01da10f87bf907fdf3c185a1d68be506 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 6 Aug 2014 17:41:12 -0400 Subject: surface stuff --- .../javascripts/rectangles/engine/rooms/grouper.js | 20 ++-- .../assets/javascripts/rectangles/models/rect.js | 3 + .../javascripts/rectangles/models/surface.js | 111 +++++++++++++++++++++ .../assets/javascripts/rectangles/models/wall.js | 2 +- test/06-test-grouper.js | 4 +- test/07-test-bounds.js | 67 ------------- test/07-test-surface.js | 60 +++++++++++ views/partials/scripts.ejs | 3 +- 8 files changed, 190 insertions(+), 80 deletions(-) create mode 100644 public/assets/javascripts/rectangles/models/surface.js delete mode 100644 test/07-test-bounds.js create mode 100644 test/07-test-surface.js (limited to 'test/06-test-grouper.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index ba081e3..4ad3bd8 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,9 +1,10 @@ (function(){ - var vec2, Rect, Rooms, UidGenerator, Wall, sort + var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort if ('window' in this) { vec2 = window.vec2 Rect = window.Rect + Surface = window.Surface Rooms = window.Rooms UidGenerator = window.UidGenerator Wall = window.Wall @@ -15,6 +16,7 @@ vec2 = require('../../models/vec2') Rect = require('../../models/rect') Wall = require('../../models/wall') + Surface = require('../../models/surface') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 PI = Math.PI @@ -115,23 +117,23 @@ if (useX && wall.vec.b == mx.rect.x.a) { wall.vec.b = mx.rect.x.b wall.mx.push(mx) - wall.faces.push(mx.face) + wall.surface.add(mx.face) return } else if (! useX && wall.vec.b == mx.rect.y.a) { wall.vec.b = mx.rect.y.b wall.mx.push(mx) - wall.faces.push(mx.face) + wall.surface.add(mx.face) return } } wall = new Wall ({ - id: base.uid(), - side: side, - mx: [ mx ], - faces: [ mx.face ], - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + id: base.uid(), + side: side, + mx: [ mx ], + surface: new Surface( mx.face ), + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], }) walls.push(wall) }) diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 590440a..500ee6d 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -107,6 +107,9 @@ Rect.prototype.eq = function(r){ return this.x.eq(r.x) && this.y.eq(r.y) } + Rect.prototype.fits = function(v){ + return this.x.length() >= v.a && this.y.length() >= v.b + } Rect.prototype.nearEdge = function (x, y, r) { var edges = 0 if (x < this.x.a+r) { diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js new file mode 100644 index 0000000..448722b --- /dev/null +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -0,0 +1,111 @@ +(function(){ + + var vec2, Rect + if ('window' in this) { + vec2 = window.vec2 + Rect = window.Rect + } + else { + vec2 = require('./vec2') + Rect = require('./rect') + } + + var Surface = function (face){ + this.width = 0 + this.height = 0 + this.faces = [] + if (face) { + this.add(face) + } + } + Surface.prototype.add = function(rect){ + this.faces.push(rect) + this.width += rect.width() + this.height = Math.max(this.height, rect.height()) + } + Surface.prototype.fits_scale = function(v, scale){ + v = v.clone().mul(scale) + return this.fits(v) + } + Surface.prototype.fits = function(v){ + var faces = this.faces + var scratch + if (this.width < v.a || this.height < v.b) { + return null + } + for (var i = 0; i < faces.length; i++) { + if (faces[i].fits(v)) { + return faces[i] + } + } + scratch = new Rect (0,0,0,0) + for (var i = 0; i < faces.length; i++) { + if (faces[i].y.length() < v.b) { + continue + } + scratch.x.a = faces[i].x.a + scratch.x.b = faces[i].x.b + scratch.y.a = faces[i].y.a + scratch.y.b = faces[i].y.b + SEARCH: for (var j = i+1; j < faces.length; j++) { + if (faces[j].y.a > scratch.y.a) { + scratch.y.a = faces[j].y.a + } + if (faces[j].y.b < scratch.y.b) { + scratch.y.b = faces[j].y.b + } + if (scratch.y.b <= scratch.y.a || scratch.y.length() < v.b) { + break SEARCH + } + scratch.x.b = faces[j].x.b + if (scratch.fits(v)) { + return scratch + } + } + } + return null + } + Surface.prototype.place = function(v, index){ + var face, faces = this.faces + } + Surface.prototype.bounds = function(index){ + var bounds = faces[index].clone() + var height = faces[index].height() + + for (var i = index-1; i > 0; i--) { + var face = faces[i] + if (face.y.length() < height) { + continue + } + if (face.y.a > bounds.y.a) { + continue + } + if (face.y.b < bounds.y.b) { + continue + } + bounds.x.a = bounds.x.a + } + + for (var i = index+1; i < faces.length; i++) { + var face = faces[i] + if (face.y.length() < height) { + continue + } + if (face.y.a > bounds.y.a) { + continue + } + if (face.y.b < bounds.y.b) { + continue + } + bounds.x.b = bounds.x.b + } + return bounds + } + + if ('window' in this) { + window.Surface = Surface + } + else { + module.exports = Surface + } +})() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index bbe6b97..fdc91fd 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -17,7 +17,7 @@ this.vec = opt.vec this.edge = opt.edge this.side = opt.side - this.faces = opt.faces + this.surface = opt.surface this.mx = opt.mx } diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 5388875..0f14217 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -236,7 +236,7 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) it("right has 5 walls", function(){ assert.equal(5, right_walls.length) - // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) @@ -281,7 +281,7 @@ describe('grouper(room,hallway)', function(){ }) it("back has 2 walls", function(){ assert.equal(2, back_walls.length) - // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) }) }) }) diff --git a/test/07-test-bounds.js b/test/07-test-bounds.js deleted file mode 100644 index 20afbfa..0000000 --- a/test/07-test-bounds.js +++ /dev/null @@ -1,67 +0,0 @@ -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 - -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 big_rect = new Rect( new vec(1,5), new vec(1,5) ) -var hall_rect = new Rect( new vec(3,4), new vec(4,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 peninsula_taller = new Room({ id: "peninsula", rect: peninsula, height: 3 }) -var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 }) - -var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) - -var big_room = new Room({ id: "big_room", rect: big_rect, height: 4 }) -var hallway = new Room({ id: "hallway", rect: hall_rect, height: 2 }) - -function reset(){ - Rooms.forEach(function(room){ - room.reset() - }) - Rooms.list = {} - Rooms.regions = [] -} -function rebuild(){ - Rooms.clipper.solve_rects() - Rooms.builder.build() -} - -describe('wall', function(){ - reset() - Rooms.add( big_room ) - Rooms.add( hallway ) - rebuild() - - var collections = Rooms.grouper.collect() - 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) - - console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) - var u_wall = back_walls[0] - - describe('#fits()', function(){ - it("", function(){ - }) - }) - describe('#bounds_for()', function(){ - it("", function(){ - }) - }) -}) - diff --git a/test/07-test-surface.js b/test/07-test-surface.js new file mode 100644 index 0000000..8f69e77 --- /dev/null +++ b/test/07-test-surface.js @@ -0,0 +1,60 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Surface = require("../public/assets/javascripts/rectangles/models/surface.js") + +// [[1 3] [0 4]] front back left right +// [[3 4] [2 4]] front back left right +// [[4 5] [0 4]] front back left right + +describe('basic surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var large = new vec2(10, 10) + var oblong = new vec2(4, 1) + + describe('#fits()', function(){ + it("fits something small", function(){ + assert.equal(true, !! surface.fits(small)) + }) + it("doesn't fit something large", function(){ + assert.equal(false, !! surface.fits(large)) + }) + it("fits something oblong", function(){ + assert.equal(true, !! surface.fits(oblong)) + }) + }) + describe('#fits_scale()', function(){ + it("fits something large, scaled down", function(){ + assert.equal(true, !! surface.fits_scale(large, 0.1)) + }) + it("doesn't fit something small, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(small, 10)) + }) + it("doesn't fit something oblong, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(oblong, 10)) + }) + }) + + describe('#place()', function(){ + it("fits a small element on the top left", function(){ + var bounds = surface.place(small, new vec2(1,3)) + console.log(bounds) + }) + it("places a small element on the right", function(){ + var bounds = surface.place(small, new vec2(4,6)) + console.log(bounds) + }) + }) + + describe('#clamp()', function(){ + it("", function(){ + }) + }) + +}) + diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 915027d..acb3bff 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -31,6 +31,7 @@ + @@ -99,4 +100,4 @@ - \ No newline at end of file + -- cgit v1.2.3-70-g09d2 From 8ed5bf2ceba8e85be9c911f8b33483242e979ab7 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 14 Aug 2014 17:45:08 -0400 Subject: fix last wrinkle in grouper --- .../javascripts/rectangles/engine/rooms/grouper.js | 73 +++++++++++++--------- .../rectangles/engine/scenery/_scenery.js | 4 +- .../rectangles/engine/scenery/types/_object.js | 4 +- .../rectangles/engine/scenery/types/image.js | 8 ++- .../rectangles/engine/scenery/types/video.js | 8 ++- .../assets/javascripts/rectangles/models/wall.js | 19 +++--- test/06-test-grouper.js | 15 ++--- test/09-test-undo.js | 2 +- 8 files changed, 79 insertions(+), 54 deletions(-) (limited to 'test/06-test-grouper.js') diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index 4ad3bd8..cde9fbb 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,6 +1,6 @@ (function(){ - var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort + var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort, _ if ('window' in this) { vec2 = window.vec2 Rect = window.Rect @@ -9,6 +9,7 @@ UidGenerator = window.UidGenerator Wall = window.Wall sort = window.sort + _ = window._ } else { Rooms = require('./_rooms') @@ -18,6 +19,7 @@ Wall = require('../../models/wall') Surface = require('../../models/surface') sort = require('../../util/sort') + _ = require('lodash') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 PI = Math.PI HALF_PI = PI/2 @@ -82,6 +84,7 @@ collection.sort( useX ? sort.compare_zx : sort.compare_xz ) collection.forEach(function(mx){ if (last_mx && last_mx.rect.eq(mx.rect)) { + // culls half-walls if (last_mx.rect.id == mx.rect.id) { last_mx.height += mx.height/2 last_mx.y += mx.height/4 @@ -103,41 +106,53 @@ } base.group = function(walls, collections, side){ var collection = collections[side] - var wall var useX = side & FRONT_BACK - var useA = side & (FRONT | RIGHT) + var useA = side & (FRONT | LEFT) // collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + var planes = {} + collection.forEach(function(mx){ if (mx.culled) return - var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b'] - - if (wall && coplanar) { - if (useX && wall.vec.b == mx.rect.x.a) { - wall.vec.b = mx.rect.x.b - wall.mx.push(mx) - wall.surface.add(mx.face) - return - } - else if (! useX && wall.vec.b == mx.rect.y.a) { - wall.vec.b = mx.rect.y.b - wall.mx.push(mx) - wall.surface.add(mx.face) - return - } - } - wall = new Wall ({ - id: base.uid(), - side: side, - mx: [ mx ], - surface: new Surface( mx.face ), - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], - }) - walls.push(wall) + var edge = mx.rect[useX ? 'y': 'x'][ useA ? 'a': 'b'] + planes[edge] = planes[edge] || [] + planes[edge].push(mx) }) - + + var edges = _.keys(planes) + edges.forEach(function(edge){ + + var wall + + planes[edge].forEach(function(mx){ + + if (wall) { + if (useX && wall.vec.b == mx.rect.x.a) { + wall.vec.b = mx.rect.x.b + wall.mx.push(mx) + wall.surface.add(mx.face) + return + } + else if (! useX && wall.vec.b == mx.rect.y.a) { + wall.vec.b = mx.rect.y.b + wall.mx.push(mx) + wall.surface.add(mx.face) + return + } + } + wall = new Wall ({ + id: base.uid(), + side: side, + mx: [ mx ], + surface: new Surface( mx.face ), + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + }) + walls.push(wall) + }) + }) + return walls } diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index c43ef14..b4a38f8 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -14,7 +14,7 @@ var Scenery = new function(){ base.add = function(opt){ var scene_media - switch (media.type) { + switch (opt.media.type) { case 'image': scene_media = new Scenery.types.image (opt) break @@ -74,11 +74,11 @@ var Scenery = new function(){ scenery_data.forEach(function(data){ var wall = Rooms.walls[data.wall_id] var scene_media = base.add({ + data: data, wall: wall, media: data.media, id: data.id }) - scene_media.deserialize(data) }) } diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 46bc0e7..3a2dcc2 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -62,8 +62,8 @@ Scenery.types.base = Fiber.extend(function(base){ set_wall: function(wall, mx){ this.wall = wall || this.wall - this.bounds = this.wall.bounds_for(this.media, this.scale) - this.center = this.wall.center() + // this.bounds = this.wall.bounds_for(this.media, this.scale) + // this.center = this.wall.center() }, set_scale: function(scale){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 99c1810..576242e 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -10,7 +10,13 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ this.build() this.bind() this.set_wall() - this.recenter() + + if (opt.data) { + this.deserialize(opt.data) + } + else { + this.recenter() + } }, build: function(){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/video.js b/public/assets/javascripts/rectangles/engine/scenery/types/video.js index a8df875..0bd5c06 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/video.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/video.js @@ -10,7 +10,13 @@ Scenery.types.video = Scenery.types.base.extend(function(base){ this.build() this.bind() this.set_wall() - this.recenter() + + if (opt.data) { + this.deserialize(opt.data) + } + else { + this.recenter() + } }, build: function(){ diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index b66d5f5..460963b 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -144,18 +144,15 @@ Wall.prototype.wallpaper = function(){ var useX = this.side & FRONT_BACK var shouldFlip = this.side & (LEFT | BACK) - this.siblings().forEach(function(w){ - w.mx.forEach(function(mx){ - - var partitionOffset = useX ? mx.x : mx.z - if (shouldFlip) partitionOffset *= -1 - partitionOffset += mx.width/2 - var floorOffset = mx.y + mx.height/2 + this.mx.forEach(function(mx){ + var partitionOffset = useX ? mx.x : mx.z + if (shouldFlip) partitionOffset *= -1 + partitionOffset += mx.width/2 + var floorOffset = mx.y + mx.height/2 - mx.el.style.backgroundImage = Scenery.nextWallpaper - mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px" - }) - }) + mx.el.style.backgroundImage = Scenery.nextWallpaper + mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px" + }) } Wall.prototype.outline = function(){ diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 0f14217..184a3d7 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -194,8 +194,8 @@ describe('grouper(rect,corner,peninsula)', function(){ it("left has 3 walls", function(){ assert.equal(3, left_walls.length) }) - it("right has 4 walls", function(){ - assert.equal(4, right_walls.length) + it("right has 3 walls", function(){ + assert.equal(3, right_walls.length) }) it("front has 2 walls", function(){ assert.equal(2, front_walls.length) @@ -229,14 +229,15 @@ describe('grouper(rect,corner,peninsula_taller)', 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) + Rooms.grouper.debug = true var right_walls = Rooms.grouper.group([], collections, RIGHT) + Rooms.grouper.debug = false - it("left has 4 walls", function(){ - assert.equal(4, left_walls.length) + it("left has 3 walls", function(){ + assert.equal(3, left_walls.length) }) - it("right has 5 walls", function(){ - assert.equal(5, right_walls.length) - // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) + it("right has 3 walls", function(){ + assert.equal(3, right_walls.length) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) diff --git a/test/09-test-undo.js b/test/09-test-undo.js index dbca90e..60fbbb6 100644 --- a/test/09-test-undo.js +++ b/test/09-test-undo.js @@ -1,5 +1,5 @@ var assert = require("assert") -var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js") +var UndoStack = require("../public/assets/javascripts/rectangles/util/undostack.js") UndoStack.debug = false describe('undo', function(){ -- cgit v1.2.3-70-g09d2