diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-07-29 18:19:13 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-07-29 18:19:13 -0400 |
| commit | 7f7262473b844bc48f59f0a6e6ef2c7231b33987 (patch) | |
| tree | 1b74a3e6ec3ce499d30869b9808b853086485987 /test | |
| parent | e082a27d835495f7ea27292ffdc62688a22a67a1 (diff) | |
| parent | 1e044ca74467d5ff46292822a274e678bd74a9b3 (diff) | |
merge
Diffstat (limited to 'test')
| -rw-r--r-- | test/00-setup.js | 2 | ||||
| -rw-r--r-- | test/01-test-vec2.js | 102 | ||||
| -rw-r--r-- | test/02-test-rect.js | 211 | ||||
| -rw-r--r-- | test/03-test-clipper.js | 129 | ||||
| -rw-r--r-- | test/04-test-builder.js | 260 | ||||
| -rw-r--r-- | test/05-test-mover.js | 74 | ||||
| -rw-r--r-- | test/mocks/mx.js | 22 |
7 files changed, 800 insertions, 0 deletions
diff --git a/test/00-setup.js b/test/00-setup.js new file mode 100644 index 0000000..78ad2c4 --- /dev/null +++ b/test/00-setup.js @@ -0,0 +1,2 @@ +Error.stackTraceLimit = 5 + diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js new file mode 100644 index 0000000..6104f92 --- /dev/null +++ b/test/01-test-vec2.js @@ -0,0 +1,102 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") + +describe('vec2', function(){ + var vec = new vec2(0, 10) + describe('#intersects()', function(){ + + it('intersects itself', function(){ + assert.equal(true, vec.intersects( new vec2(0, 10) )); + }) + it('intersects w/ same startpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 5) )); + }) + it('intersects w/ same startpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 15) )); + }) + it('intersects w/ same endpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(5, 10) )); + }) + it('intersects w/ same endpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 10) )); + }) + it('intersects when contained', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 15) )); + }) + it('does not intersect when before', function(){ + assert.equal(false, vec.intersects( new vec2(-10, -5) )); + }) + it('does not intersect when after', function(){ + assert.equal(false, vec.intersects( new vec2(15, 20) )); + }) + it('intersects when only startpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 0) )); + }) + it('intersects when only endpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(10, 15) )); + }) + }) + + describe('#contains()', function(){ + it('contains itself', function(){ + assert.equal(true, vec.contains( 0 )); + assert.equal(true, vec.contains( 5 )); + assert.equal(true, vec.contains( 10 )); + }) + it('does not contain before or after', function(){ + assert.equal(false, vec.contains( -5 )); + assert.equal(false, vec.contains( 15 )); + }) + }) + + describe('#containsCenter()', function(){ + it('containsCenter itself', function(){ + assert.equal(true, vec.containsCenter( 5 )); + }) + it('does not containsCenter its endpoints', function(){ + assert.equal(false, vec.containsCenter( 0 )); + assert.equal(false, vec.containsCenter( 10 )); + }) + }) + + describe('#containsDisc()', function(){ + it('containsDisc within a radius', function(){ + assert.equal(true, vec.containsDisc( 2, 2 )); + assert.equal(true, vec.containsDisc( 5, 2 )); + assert.equal(true, vec.containsDisc( 7, 2 )); + }) + it('does not containsDisc its endpoints', function(){ + assert.equal(false, vec.containsDisc( 0, 2 )); + assert.equal(false, vec.containsDisc( 1, 2 )); + assert.equal(false, vec.containsDisc( 9, 2 )); + assert.equal(false, vec.containsDisc( 10, 2 )); + }) + it('does not containsDisc outside points', function(){ + assert.equal(false, vec.containsDisc( -5, 2 )); + assert.equal(false, vec.containsDisc( -2, 2 )); + assert.equal(false, vec.containsDisc( 12, 2 )); + assert.equal(false, vec.containsDisc( 15, 2 )); + }) + }) + + describe('#clampDisc()', function(){ + it('clampDisc clamps on the left', function(){ + assert.equal(2, vec.clampDisc( -1, 2 )); + assert.equal(2, vec.clampDisc( 0, 2 )); + assert.equal(2, vec.clampDisc( 1, 2 )); + assert.equal(2, vec.clampDisc( 2, 2 )); + }) + it('clampDisc clamps on the right', function(){ + assert.equal(8, vec.clampDisc( 8, 2 )); + assert.equal(8, vec.clampDisc( 9, 2 )); + assert.equal(8, vec.clampDisc( 10, 2 )); + assert.equal(8, vec.clampDisc( 11, 2 )); + }) + it('clampDisc doesnt clamp in the middle', function(){ + assert.equal(3, vec.clampDisc( 3, 2 )); + assert.equal(4, vec.clampDisc( 4, 2 )); + assert.equal(5, vec.clampDisc( 5, 2 )); + }) + }) +}) + diff --git a/test/02-test-rect.js b/test/02-test-rect.js new file mode 100644 index 0000000..29998da --- /dev/null +++ b/test/02-test-rect.js @@ -0,0 +1,211 @@ +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 FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT + +describe('rect', function(){ + describe('#intersects()', function(){ + var rect = new Rect(0, 0, 10, 10) + + it('intersects itself', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) )); + }) + it('intersects more wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) )); + }) + it('intersects less wide', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) )); + }) + it('intersects more tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) )); + }) + it('intersects less tall', function(){ + assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) )); + }) + it('intersects if right-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) )); + }) + it('intersects if bottom-adjacent', function(){ + assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) )); + }) + it('does not intersect if to the right', function(){ + assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) )); + }) + it('does not intersect if beneath', function(){ + assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) )); + }) + /* + it('does not intersect if corners intersect', function(){ + assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) )); + }) + */ + + }) + + var rect = new Rect( new vec(1,4), new vec(1,4) ) + + var east_in = new Rect( new vec(2,3), new vec(1,4) ) + var east_edge = new Rect( new vec(2,4), new vec(1,4) ) + var east = new Rect( new vec(2,5), new vec(1,4) ) + + var south_in = new Rect( new vec(1,4), new vec(2,3) ) + var south_edge = new Rect( new vec(1,4), new vec(2,4) ) + var south = new Rect( new vec(1,4), new vec(2,5) ) + + var corner = new Rect( new vec(3,5), new vec(3,5) ) + + function sides (s) { + return s.reduce(function(prev, curr){ + return prev | curr.sides + }, 0) + } + /* + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + */ + + describe('#split(rect, east)', function(){ + var s0 = rect.split(east) + var s1 = east.split(rect) + + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(FRONT | BACK | LEFT, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, east_in)', function(){ + var s0 = rect.split(east_in) + var s1 = east_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('east_in only has front/back', function(){ + assert.equal(FRONT | BACK, sides(s1)) + }) + }) + + describe('#split(rect, east_edge)', function(){ + var s0 = rect.split(east_edge) + var s1 = east_edge.split(rect) + + it('has no degenerate vectors', function(){ + s0.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + s1.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + }) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/back/left', function(){ + assert.equal(ALL, sides(s0)) + }) + it('east is front/back/right', function(){ + assert.equal(FRONT | BACK | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, south)', function(){ + var s0 = rect.split(south) + var s1 = south.split(rect) + + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is front/left/right', function(){ + assert.equal(FRONT | LEFT | RIGHT, sides(s0)) + }) + it('south is back/left/right', function(){ + assert.equal(BACK | LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, south_in)', function(){ + var s0 = rect.split(south_in) + var s1 = south_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south_in only has left/right', function(){ + assert.equal(LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, south_edge)', function(){ + var s0 = rect.split(south_edge) + var s1 = south_edge.split(rect) + + it('has no degenerate vectors', function(){ + s0.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + s1.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + }) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south is back/left/right', function(){ + assert.equal(BACK | LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, corner)', function(){ + var s0 = rect.split(corner) + var s1 = corner.split(rect) + + it('rect splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('corner splits on all 4 sides', function(){ + assert.equal(ALL, sides(s1)) + }) + + var rect_map = {} + var corner_map = {} + var rect_state = s0.forEach(function(r){ + rect_map[r.sides] = rect_map[r.sides] || [] + rect_map[r.sides].push(r) + }) + var corner_state = s1.forEach(function(r){ + corner_map[r.sides] = corner_map[r.sides] || [] + corner_map[r.sides].push(r) + }) + + it('rect contains a rect with no sides', function(){ + assert.equal(1, rect_map[0].length) + }) + it('corner contains a rect with no sides', function(){ + assert.equal(1, corner_map[0].length) + }) + it('rect and corner overlap', function(){ + assert.equal(String(rect_map[0][0]), String(corner_map[0][0])) + }) + + }) + +}) + diff --git a/test/03-test-clipper.js b/test/03-test-clipper.js new file mode 100644 index 0000000..923a8a5 --- /dev/null +++ b/test/03-test-clipper.js @@ -0,0 +1,129 @@ +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 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) ) + +function report(a) { + console.log( a.join("\n") ) +} + +describe('clipper', function(){ + Rooms.list = {} + Rooms.regions = [] + Rooms.add_with_rect( rect ) + Rooms.add_with_rect( east ) + + describe('#clip_rects(rect, east)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + + it('contains duplicates', function(){ + var map = {} + var state = regions.some(function(a){ + var s = a.toString() + if (s in map) return true + map[s] = s + return false + }) + assert.equal(true, state) + }) + }) + + describe('#cull_rects(rect, east)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + var culled_dupes = regions.filter(function(r){ return r.dupe }) + + it('clipper returns 4 rects', function(){ + assert.equal(4, regions.length) + }) + it('culling marks 1 duplicate', function(){ + assert.equal(1, culled_dupes.length) + }) + it('culling returns 3 non-duplicate', function(){ + assert.equal(3, culled.length) + }) + }) + + // + + Rooms.list = {} + Rooms.regions = [] + Rooms.add_with_rect( rect ) + Rooms.add_with_rect( corner ) + + describe('#cull_rects(rect, corner)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + var culled_dupes = regions.filter(function(r){ return r.dupe }) + + it('clipper returns 8 rects', function(){ + assert.equal(8, regions.length) + }) + it('culling marks 1 duplicate', function(){ + assert.equal(1, culled_dupes.length) + }) + it('culling returns 7 non-duplicate', function(){ + assert.equal(7, culled.length) + }) + }) + + // + + Rooms.list = {} + Rooms.regions = [] + Rooms.add_with_rect( rect ) + Rooms.add_with_rect( corner ) + Rooms.add_with_rect( peninsula ) + +/* + // this method uses a tree to match areas, which tends to leave extra overlapping regions + describe('#cull_rects(rect, corner, peninsula)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects() + var culled_dupes = regions.filter(function(r){ return r.dupe }) + + it('clipper returns 16 rects', function(){ + assert.equal(16, regions.length) + }) + it('culling marks 3 duplicate', function(){ + assert.equal(3, culled_dupes.length) + }) + it('culling returns 13 non-duplicate', function(){ + assert.equal(13, regions.length) + }) + }) +*/ + + // this method iterates to match areas, which omits regions in some cases + describe('#cull_rects_iterative(rect, corner, peninsula)', function(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + var culled_dupes = regions.filter(function(r){ return r.dupe }) + + it('clipper returns 16 rects', function(){ + assert.equal(16, regions.length) + }) + it('culling marks 3 duplicate', function(){ + assert.equal(3, culled_dupes.length) + }) + it('culling returns 14 non-duplicate', function(){ + assert.equal(13, culled.length) + }) + }) + +}) + diff --git a/test/04-test-builder.js b/test/04-test-builder.js new file mode 100644 index 0000000..fa624f8 --- /dev/null +++ b/test/04-test-builder.js @@ -0,0 +1,260 @@ +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 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 count_wall_sides (wall_groups) { + var wall_sides = {} + wall_sides[LEFT] = 0 + wall_sides[RIGHT] = 0 + wall_sides[FRONT] = 0 + wall_sides[BACK] = 0 + wall_sides[TOP] = 0 + wall_sides[BOTTOM] = 0 + wall_sides.total = 0 + wall_groups.map(function(walls){ + walls.forEach(function(wall){ + wall_sides[wall.side] += 1 + wall_sides.total += 1 + }) + }) + return wall_sides +} +function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) + Rooms.list = {} + Rooms.regions = [] +} +function rebuild(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + return culled +} + +describe('builder', function(){ + + reset() + Rooms.add( rect_room ) + rebuild() + + describe('#build_walls(rect)', function(){ + var walls = Rooms.builder.build_walls(rect_room.regions[0]) + + it("should return 4 walls", function(){ + assert.equal(4, walls.length) + }) + it("should have one side per wall", function(){ + assert.equal(1, bitcount(walls[0].side)) + assert.equal(1, bitcount(walls[1].side)) + assert.equal(1, bitcount(walls[2].side)) + assert.equal(1, bitcount(walls[3].side)) + }) + }) + + describe('#build_floors(rect)', function(){ + var floors = Rooms.builder.build_floors(rect_room) + it("should make 2 floors", function(){ + assert.equal(2, floors.length) + }) + }) + + // rect vs east + + reset() + Rooms.add( rect_room ) + Rooms.add( east_room ) + var regions = rebuild() + + describe('#build_walls(rect, east)', function(){ + + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) + + // reportSides(w); console.log("--") + + it("should return 8 walls", function(){ + assert.equal(8, wall_sides.total) + }) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 1 left wall", function(){ + assert.equal(1, wall_sides[LEFT]) + }) + it("should have 1 right wall", function(){ + assert.equal(1, wall_sides[RIGHT]) + }) + }) + + describe('#build_floors(rect, east)', function(){ + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 0 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(0, fg2_halves.length) + }) + }) + + // rect vs corner + + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + var regions = rebuild() + + describe('#build_walls(rect, corner)', function(){ + + var wall_groups = regions.map(Rooms.builder.build_walls.bind(Rooms.builder)) + var wall_sides = count_wall_sides(wall_groups) + + // reportSides(w); console.log("--") + + it("should return 12 walls", function(){ + assert.equal(12, wall_sides.total) + }) + it("should have 3 front walls", function(){ + assert.equal(3, wall_sides[FRONT]) + }) + it("should have 3 back walls", function(){ + assert.equal(3, wall_sides[BACK]) + }) + it("should have 3 left walls", function(){ + assert.equal(3, wall_sides[LEFT]) + }) + it("should have 3 right walls", function(){ + assert.equal(3, wall_sides[RIGHT]) + }) + }) + + describe('#build_floors(rect, corner)', function(){ + var fg = Rooms.builder.build_floors(rect_room) + var fg2 = Rooms.builder.build_floors(corner_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 8 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(6, fg2_floors.length) + }) + it("should make 0 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(0, fg2_halves.length) + }) + }) + + // taller (rect) vs east + + reset() + Rooms.add( taller_room ) + Rooms.add( east_room ) + var regions = rebuild() + + describe('#build_floors(taller, east)', function(){ + var fg = Rooms.builder.build_floors(taller_room) + var fg2 = Rooms.builder.build_floors(east_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 4 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(2, fg2_floors.length) + }) + it("should make 3 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(3, fg2_halves.length) + }) + }) + + // taller vs corner + + reset() + Rooms.add( taller_room ) + Rooms.add( corner_room ) + var regions = rebuild() + + describe('#build_floors(taller, corner)', function(){ + var fg = Rooms.builder.build_floors(taller_room) + var fg2 = Rooms.builder.build_floors(corner_room) + var fg_floors = fg.filter(function(r){ return ! r.half_side }) + var fg_halves = fg.filter(function(r){ return r.half_side }) + var fg2_floors = fg2.filter(function(r){ return ! r.half_side }) + var fg2_halves = fg2.filter(function(r){ return r.half_side }) + + it("should make 8 floors", function(){ + assert.equal(2, fg_floors.length) + assert.equal(6, fg2_floors.length) + }) + it("should make 2 half-walls", function(){ + assert.equal(0, fg_halves.length) + assert.equal(2, fg2_halves.length) + }) + }) + + // TODO ... test half-wall generation on rooms intersecting each other crossways: + // right now this generates extra half-walls from cross on the box side of bulge + // this could be fixed by cropping regions during the cull stage? +/* + var box = new Rect( new vec(1,6), new vec(1,5) ) + var bulge = new Rect( new vec(2,5), new vec(4,6) ) + var cross = new Rect( new vec(3,4), new vec(3,7) ) +*/ + +}) + diff --git a/test/05-test-mover.js b/test/05-test-mover.js new file mode 100644 index 0000000..42c8653 --- /dev/null +++ b/test/05-test-mover.js @@ -0,0 +1,74 @@ +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 FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT +var NONE = 0x0 +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 radius = 1 + +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 }) + +function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) + Rooms.list = {} + Rooms.regions = [] +} +function rebuild(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + return culled +} + +describe('mover', function(){ + reset() + Rooms.add( rect_room ) + rebuild() + + describe('room#collidesDisc()', function(){ + var room = rect_room + it("should not collide in the center", function(){ + assert.equal(NONE, room.collidesDisc(3, 3, radius)) + assert.equal(NONE, room.collidesDisc(2, 2, radius)) + assert.equal(NONE, room.collidesDisc(4, 4, radius)) + }) + it("should collide on the sides", function(){ + assert.equal(FRONT, room.collidesDisc(3, 1, radius)) + assert.equal(BACK, room.collidesDisc(3, 5, radius)) + assert.equal(LEFT, room.collidesDisc(1, 3, radius)) + assert.equal(RIGHT, room.collidesDisc(5, 3, radius)) + }) + it("should collide in the corners", function(){ + assert.equal(LEFT | FRONT, room.collidesDisc(1, 1, radius)) + assert.equal(RIGHT | FRONT, room.collidesDisc(5, 1, radius)) + assert.equal(LEFT | BACK, room.collidesDisc(1, 5, radius)) + assert.equal(RIGHT | BACK, room.collidesDisc(5, 5, radius)) + }) + }) +}) + diff --git a/test/mocks/mx.js b/test/mocks/mx.js new file mode 100644 index 0000000..2a76ffc --- /dev/null +++ b/test/mocks/mx.js @@ -0,0 +1,22 @@ +// Non-DOM-dependent stub MX library +// Used for testing code that builds MX elements, without a DOM dependency + +var MX = module.exports = {} + +MX.Object3D = function (klass) { + this.klass = klass + this.width = this.height = this.scaleX = this.scaleY = this.scaleZ = 1 + this.z = this.y = this.x = 0 + this.side = 0 + this.type = "Face" + this.el = { style: {} } + this.rect = null +} + +MX.Scene = { + els: [], + add: function (el) { + els.push(el) + } +} + |
