diff options
| -rw-r--r-- | assets/javascripts/rectangles/_env.js | 12 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/builder.js | 163 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/clipper.js | 12 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/mover.js | 6 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/map/_map.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/rect.js | 38 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/room.js | 56 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/tree.js | 16 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/wall.js | 31 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/util/mouse.js | 6 |
10 files changed, 191 insertions, 151 deletions
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js index 1252778..edec7e4 100644 --- a/assets/javascripts/rectangles/_env.js +++ b/assets/javascripts/rectangles/_env.js @@ -18,16 +18,16 @@ environment.init = function(){ // map.center.a = 0 // map.center.b = 0 - clipper.rooms.push( new room ({ - rect: new rect(0,0, 500,500), + clipper.rooms.push( new Room ({ + rect: new Rect(0,0, 500,500), height: 500, })) - clipper.rooms.push( new room ({ - rect: new rect(600,0, 1100,500), + clipper.rooms.push( new Room ({ + rect: new Rect(600,0, 1100,500), height: 500, })) - clipper.rooms.push( new room ({ - rect: new rect(450,150, 650,350), + clipper.rooms.push( new Room ({ + rect: new Rect(450,150, 650,350), height: 300, })) diff --git a/assets/javascripts/rectangles/engine/builder.js b/assets/javascripts/rectangles/engine/builder.js index 4ccc649..3452ecc 100644 --- a/assets/javascripts/rectangles/engine/builder.js +++ b/assets/javascripts/rectangles/engine/builder.js @@ -21,25 +21,27 @@ var builder = new function(){ function build (){ clipper.rooms = sort_rooms_by_id(clipper.rooms) - clipper.regions.forEach(function(r){ - build_walls(r).forEach(function(el){ + clipper.regions.forEach(function(room){ + build_walls(room).forEach(function(el){ els.push(el) scene.add(el) }) }) clipper.rooms = sort_rooms_by_height(clipper.rooms) - clipper.rooms.forEach(function(r){ - build_floors(r).forEach(function(el){ + clipper.rooms.forEach(function(room){ + build_floors(room).forEach(function(el){ els.push(el) scene.add(el) }) }) + + clipper.rooms = sort_rooms_by_id(clipper.rooms) } function bind (){ - clipper.rooms.forEach(function(r){ - r.bind() + clipper.rooms.forEach(function(room){ + room.bind() }) } @@ -50,200 +52,199 @@ var builder = new function(){ els = [] } - function build_walls (r){ - var rm = clipper.rooms[ r.id ] + function build_walls (region){ + var room = clipper.rooms[ region.id ] var list = [], el = null - var width = r.x.length() - var depth = r.y.length() - var height = clipper.rooms[r.id].height + var width = region.x.length() + var depth = region.y.length() + var height = room.height - if (r.sides & FRONT) { + if (region.sides & FRONT) { el = make_wall('.front') el.width = width el.height = height el.rotationY = PI - el.x = r.x.a + width/2 + el.x = region.x.a + width/2 el.y = height/2 - el.z = r.y.a - rm.$walls.push(el.el) + el.z = region.y.a + room.walls.push(el) list.push(el) } - if (r.sides & BACK) { + if (region.sides & BACK) { var el = make_wall('.back') el.width = width el.height = height el.rotationY = 0 - el.x = r.x.b - width/2 + el.x = region.x.b - width/2 el.y = height/2 - el.z = r.y.b - rm.$walls.push(el.el) + el.z = region.y.b + room.walls.push(el) list.push(el) } - if (r.sides & LEFT) { + if (region.sides & LEFT) { el = make_wall('.left') el.rotationY = HALF_PI el.height = height el.width = depth - el.x = r.x.a + el.x = region.x.a el.y = height/2 - el.z = r.y.a + depth/2 - rm.$walls.push(el.el) + el.z = region.y.a + depth/2 + room.walls.push(el) list.push(el) } - if (r.sides & RIGHT) { + if (region.sides & RIGHT) { el = make_wall('.right') el.rotationY = -HALF_PI el.height = height el.width = depth - el.x = r.x.b + el.x = region.x.b el.y = height/2 - el.z = r.y.b - depth/2 - rm.$walls.push(el.el) + el.z = region.y.b - depth/2 + room.walls.push(el) list.push(el) } return list } - function build_floors(rm){ + function build_floors(room){ var list = [], el = null - var already_constructed = rm.intersects.filter(function(rr){ return rr.constructed }) - sort_rooms_by_height(already_constructed) + var constructed = room.intersects.filter(function(room){ return room.constructed }) + sort_rooms_by_height(constructed) - if (already_constructed.length > 0) { + 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 - rm.regions.forEach(function(r){ + room.regions.forEach(function(region){ var intersected = false - for (var i = 0; i < already_constructed.length; i++) { - if (already_constructed[i].rect.contains(r)) { + for (var i = 0; i < constructed.length; i++) { + if (constructed[i].rect.contains(region)) { intersected = true // r.sides = 0xf // half_sides } - else if (already_constructed[i].rect.intersects(r)) { + else if (constructed[i].rect.intersects(region)) { intersected = true - if (rm.height < already_constructed[i].height) { - list = list.concat( make_ceiling_walls( rm, already_constructed[i], r ) ) + if (room.height < constructed[i].height) { + list = list.concat( make_ceiling_walls( room, constructed[i], region ) ) } } } if (! intersected) { - el = make_floor(rm, r) + el = make_floor(room, region) list.push( el ) - rm.$floor.push(el.el) + room.floor.push(el) - el = make_ceiling(rm, r) + el = make_ceiling(room, region) list.push( el ) - rm.$ceiling.push(el.el) + room.ceiling.push(el) } }) } else { // render floor and ceiling for the entire rectangle - el = make_floor(rm, rm.rect) + el = make_floor(room, room.rect) list.push( el ) - rm.$floor.push(el.el) + room.floor.push(el) - el = make_ceiling(rm, rm.rect) + el = make_ceiling(room, room.rect) list.push( el ) - rm.$ceiling.push(el.el) + room.ceiling.push(el) } - rm.constructed = true + room.constructed = true return list } - function make_ceiling_walls( lo, hi, r ){ + function make_ceiling_walls( lo, hi, region ){ var list = [] - var width = r.x.length() - var depth = r.y.length() + var width = region.x.length() + var depth = region.y.length() var height = hi.height - lo.height - if (! (r.half_sides & LEFT) && r.x.a == hi.rect.x.a) { + 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 = r.x.a + el.x = region.x.a el.y = lo.height + height/2 - el.z = r.y.a + depth/2 + el.z = region.y.a + depth/2 list.push(el) - hi.$walls.push(el.el) - r.half_sides |= LEFT + hi.walls.push(el) + region.half_sides |= LEFT } - if (! (r.half_sides & RIGHT) && r.x.b == hi.rect.x.b) { + 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 = r.x.b + el.x = region.x.b el.y = lo.height + height/2 - el.z = r.y.b - depth/2 + el.z = region.y.b - depth/2 list.push(el) - hi.$walls.push(el.el) - r.half_sides |= RIGHT + hi.walls.push(el) + region.half_sides |= RIGHT } - if (! (r.half_sides & FRONT) && r.y.a == hi.rect.y.a) { + if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) { el = make_wall('.front') el.width = width el.height = height el.rotationY = PI - el.x = r.x.a + width/2 + el.x = region.x.a + width/2 el.y = lo.height + height/2 - el.z = r.y.a + el.z = region.y.a list.push(el) - hi.$walls.push(el.el) - r.half_sides |= FRONT + hi.walls.push(el) + region.half_sides |= FRONT } - if (! (r.half_sides & BACK) && r.y.b == hi.rect.y.b) { + if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) { el = make_wall('.back') el.width = width el.height = height el.rotationY = 0 - el.x = r.x.b - width/2 + el.x = region.x.b - width/2 el.y = lo.height + height/2 - el.z = r.y.b + el.z = region.y.b list.push(el) - hi.$walls.push(el.el) - r.half_sides |= BACK + hi.walls.push(el) + region.half_sides |= BACK } return list } - function make_floor(rm, r){ - var width = r.x.length() - var depth = r.y.length() + 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 = r.x.a + width/2 + el.x = region.x.a + width/2 el.y = 0 - el.z = r.y.a + depth/2 + el.z = region.y.a + depth/2 el.rotationX = PI/2 - return el } - function make_ceiling(rm, r){ - var width = r.x.length() - var depth = r.y.length() - var height = rm.height + function make_ceiling(room, region){ + var width = region.x.length() + var depth = region.y.length() + var height = room.height var el = make_wall('.ceiling') el.height = depth el.width = width - el.x = r.x.a + width/2 + el.x = region.x.a + width/2 el.y = height - el.z = r.y.a + depth/2 + el.z = region.y.a + depth/2 el.rotationX = -PI/2 return el } diff --git a/assets/javascripts/rectangles/engine/clipper.js b/assets/javascripts/rectangles/engine/clipper.js index 43a1bab..7c2f048 100644 --- a/assets/javascripts/rectangles/engine/clipper.js +++ b/assets/javascripts/rectangles/engine/clipper.js @@ -21,10 +21,10 @@ var clipper = new function(){ app.tube("clip") } - base.add_room = function(r){ - rooms.push( new room({ + base.add_room = function(rect){ + rooms.push( new Room({ id: base.rooms.length, - rect: r, + rect: rect, height: quantize(randrange(200,500), 50), }) ) } @@ -63,8 +63,8 @@ var clipper = new 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 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++) { @@ -90,7 +90,7 @@ var clipper = new function(){ } } else { - ttx.data = new tree (regions[i].y.a, [regions[i]]) + ttx.data = new Tree (regions[i].y.a, [regions[i]]) } } diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js index 757dbfa..25809f6 100644 --- a/assets/javascripts/rectangles/engine/mover.js +++ b/assets/javascripts/rectangles/engine/mover.js @@ -59,9 +59,9 @@ var mover = new function(){ // did we actually enter a room? if (intersects.length) { base.room = intersects[0] - base.room.$floor.addClass("active") - base.room.$ceiling.addClass("active") - base.room.$walls.addClass("active") + base.room.floor.forEach(function(w){ $(w.el).addClass("active") }) + base.room.ceiling.forEach(function(w){ $(w.el).addClass("active") }) + base.room.walls.forEach(function(w){ $(w.el).addClass("active") }) } } diff --git a/assets/javascripts/rectangles/map/_map.js b/assets/javascripts/rectangles/map/_map.js index e612c98..53084bb 100644 --- a/assets/javascripts/rectangles/map/_map.js +++ b/assets/javascripts/rectangles/map/_map.js @@ -12,7 +12,7 @@ var map = new function(){ base.sides = function(){ var sides = base.bounds.clone().div(2).div(base.zoom) - return new rect( base.center.a - sides.a, -base.center.b - sides.b, + return new Rect( base.center.a - sides.a, -base.center.b - sides.b, base.center.a + sides.a, -base.center.b + sides.b ) } diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js index 0664693..2d48e13 100644 --- a/assets/javascripts/rectangles/models/rect.js +++ b/assets/javascripts/rectangles/models/rect.js @@ -10,8 +10,8 @@ function sidesToString(sides){ return s } -window.rect = (function(){ - var rect = function (x0,y0,x1,y1){ +window.Rect = (function(){ + var Rect = function (x0,y0,x1,y1){ if (x0 instanceof vec2) { this.x = x0 this.y = y0 @@ -27,56 +27,56 @@ window.rect = (function(){ this.translation = new vec2(0,0) this.sides = FRONT | BACK | LEFT | RIGHT } - rect.prototype.clone = function(){ - return new rect( this.x.clone(), this.y.clone() ) + Rect.prototype.clone = function(){ + return new Rect( this.x.clone(), this.y.clone() ) } - rect.prototype.center = function(){ + Rect.prototype.center = function(){ return new vec2(this.x.midpoint(), this.y.midpoint()) } - rect.prototype.area = function(){ + Rect.prototype.area = function(){ return this.x.length() * this.y.length() } - rect.prototype.mul = function(n){ + Rect.prototype.mul = function(n){ this.x.mul(n) this.y.mul(n) return this } - rect.prototype.div = function(n){ + Rect.prototype.div = function(n){ this.x.div(n) this.y.div(n) return this } - rect.prototype.translate = function(translation){ + Rect.prototype.translate = function(translation){ var translation = translation || this.translation this.x.abs().add(translation.a) this.y.abs().add(translation.b) this.translation.a = this.translation.b = 0 return this } - rect.prototype.contains = function(x,y){ + Rect.prototype.contains = function(x,y){ return this.x.contains(x) && this.y.contains(y) } - rect.prototype.containsDisc = function(x,y,r){ + Rect.prototype.containsDisc = function(x,y,r){ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } - rect.prototype.intersects = function(r){ + Rect.prototype.intersects = function(r){ return this.x.intersects(r.x) && this.y.intersects(r.y) } - rect.prototype.width = function(){ return this.x.length() } - rect.prototype.height = function(){ return this.y.length() } - rect.prototype.toString = function(){ + Rect.prototype.width = function(){ return this.x.length() } + Rect.prototype.height = function(){ return this.y.length() } + Rect.prototype.toString = function(){ var sides = sidesToString(this.sides) var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides return s } - rect.prototype.quantize = function(n){ + Rect.prototype.quantize = function(n){ this.x.quantize(n) this.y.quantize(n) return this } - rect.prototype.split = function(r){ + Rect.prototype.split = function(r){ var rz = this var splits = [] var split_contains = 0 @@ -135,7 +135,7 @@ window.rect = (function(){ x_intervals.forEach(function(x){ y_intervals.forEach(function(y){ - var rn = new rect(x[0], y[0]) + var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) if (r.intersects(rn)) { @@ -148,6 +148,6 @@ window.rect = (function(){ return splits } - return rect + return Rect })() diff --git a/assets/javascripts/rectangles/models/room.js b/assets/javascripts/rectangles/models/room.js index b66d868..c26efa9 100644 --- a/assets/javascripts/rectangles/models/room.js +++ b/assets/javascripts/rectangles/models/room.js @@ -1,23 +1,22 @@ -window.room = (function(){ +window.Room = (function(){ - var room = function(opt){ + var Room = function(opt){ this.id = opt.id || clipper.rooms.length this.rect = opt.rect this.regions = [] + this.walls = [] + this.floor = [] + this.ceiling = [] this.height = opt.height || 200 this.focused = false - - this.$walls = $([]) - this.$floor = $([]) - this.$ceiling = $([]) } - room.prototype.toString = function(){ + Room.prototype.toString = function(){ return this.rect.toString() } - room.prototype.reset = function(){ + Room.prototype.reset = function(){ var copy = this.rect.clone() copy.id = this.id copy.sides = FRONT | BACK | LEFT | RIGHT @@ -26,24 +25,33 @@ window.room = (function(){ this.intersects = [] this.constructed = false - this.$walls = $([]) - this.$floor = $([]) - this.$ceiling = $([]) + this.walls = [] + this.floor = [] + this.ceiling = [] } - room.prototype.bind = function(){ - this.$walls.bind({ - mouseover: function(){ - }, - mousemove: function(e){ - }, - mousedown: function(){ - $(this).css("background-color", choice(window.palettes.colors)) - } + Room.prototype.bind = function(){ + var base = this + base.walls.forEach(function(wall){ + $(wall.el).bind({ + mouseover: function(){ + }, + mousemove: function(e){ + var color = choice(window.palettes.colors) + base.walls.forEach(function(wall){ + $(wall.el).css("background-color", color) + }) + }, + mousedown: function(){ + } + }) }) } + + Room.prototype.add_wall = function(){ + } - room.prototype.clipTo = function(r){ + Room.prototype.clipTo = function(r){ // for each of this rect's regions split the region if necessary var regions = this.regions var splits @@ -57,7 +65,7 @@ window.room = (function(){ this.regions = regions } - room.prototype.collides = function(x,y){ + Room.prototype.collides = function(x,y){ var collision = 0, wall_collision, contains_x, contains_y this.regions.forEach(function(r){ if (! r.sides) return @@ -94,7 +102,7 @@ window.room = (function(){ return collision } - room.prototype.collidesDisc = function(x,y,radius){ + Room.prototype.collidesDisc = function(x,y,radius){ var collision = 0, wall_collision, contains_x, contains_y this.regions.forEach(function(r){ if (! r.sides) return @@ -131,7 +139,7 @@ window.room = (function(){ return collision } - return room + return Room })() diff --git a/assets/javascripts/rectangles/models/tree.js b/assets/javascripts/rectangles/models/tree.js index 577c41a..8193988 100644 --- a/assets/javascripts/rectangles/models/tree.js +++ b/assets/javascripts/rectangles/models/tree.js @@ -1,35 +1,35 @@ -var tree = function(n, data){ +var Tree = function(n, data){ this.lo = null this.hi = null this.value = n this.data = data } -tree.prototype.find = function(n){ +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){ +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) + 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(){ +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(){ +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(){ +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() diff --git a/assets/javascripts/rectangles/models/wall.js b/assets/javascripts/rectangles/models/wall.js new file mode 100644 index 0000000..e25d2dd --- /dev/null +++ b/assets/javascripts/rectangles/models/wall.js @@ -0,0 +1,31 @@ +window.wall = (function(){ + + var wall = function(opt){ + this.id = opt.id + this.room = opt.room + this.rect = opt.rect + this.mx = [] + } + + wall.prototype.toString = function(){ + return this.rect.toString() + } + + wall.prototype.reset = function(){ + } + + wall.prototype.bind = function(){ + this.$walls.bind({ + mouseover: function(){ + }, + mousemove: function(e){ + }, + mousedown: function(){ + $(this).css("background-color", choice(window.palettes.colors)) + } + }) + } + + return wall + +})() diff --git a/assets/javascripts/rectangles/util/mouse.js b/assets/javascripts/rectangles/util/mouse.js index a75d46d..8d94201 100644 --- a/assets/javascripts/rectangles/util/mouse.js +++ b/assets/javascripts/rectangles/util/mouse.js @@ -40,7 +40,7 @@ function mouse (opt) { base.creating = false base.dragging = false - base.cursor = new rect(0,0,0,0) + base.cursor = new Rect(0,0,0,0) base.tube = new Tube () opt.down && base.tube.on("down", opt.down) @@ -74,7 +74,7 @@ function mouse (opt) { var pos = positionFromMouse(e) var x = pos.a, y = pos.b - base.cursor = new rect (x,y, x,y) + base.cursor = new Rect (x,y, x,y) base.down = true base.tube("down", e, base.cursor) @@ -109,7 +109,7 @@ function mouse (opt) { if (base.down) { base.down = false pos = positionFromMouse(e) - new_cursor = new rect(pos.a, pos.b) + new_cursor = new Rect (pos.a, pos.b) base.tube("up", e, base.cursor, new_cursor) base.cursor = new_cursor } |
