diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-04-23 13:00:39 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-04-23 13:00:39 -0400 |
| commit | 1282559bffc1acbc99a22ecfef44227eefbc9817 (patch) | |
| tree | fadeb5e64949ad50ce725c2dbddde05a73dddc5a | |
| parent | f94cf0d133a5d426a20cb5ac7eeb60f72b690119 (diff) | |
detect what room you're in
| -rw-r--r-- | assets/javascripts/rectangles/_env.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/builder.js | 71 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/clipper.js | 4 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/mover.js | 18 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/room.js | 6 | ||||
| -rw-r--r-- | rectangles.html | 11 |
6 files changed, 70 insertions, 42 deletions
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js index be3d931..f014a8e 100644 --- a/assets/javascripts/rectangles/_env.js +++ b/assets/javascripts/rectangles/_env.js @@ -38,8 +38,8 @@ environment.init = function(){ } builder.init() - mover.init() clipper.init() + mover.init() window.scene && scene.update() environment.update() diff --git a/assets/javascripts/rectangles/engine/builder.js b/assets/javascripts/rectangles/engine/builder.js index 8e1508a..c48e8e4 100644 --- a/assets/javascripts/rectangles/engine/builder.js +++ b/assets/javascripts/rectangles/engine/builder.js @@ -19,12 +19,9 @@ var builder = new function(){ } function build (){ clipper.rooms = sort_rooms_by_id(clipper.rooms) - clipper.rooms.forEach(function(r){ - r.walls = [] - r.floors = [] - }) + clipper.regions.forEach(function(r){ - walls(r).forEach(function(el){ + build_walls(r).forEach(function(el){ els.push(el) scene.add(el) }) @@ -32,7 +29,7 @@ var builder = new function(){ clipper.rooms = sort_rooms_by_height(clipper.rooms) clipper.rooms.forEach(function(r){ - floors(r).forEach(function(el){ + build_floors(r).forEach(function(el){ els.push(el) scene.add(el) }) @@ -45,7 +42,9 @@ var builder = new function(){ els = [] } - function walls (r){ + function build_walls (r){ + var rm = clipper.rooms[ r.id ] + var list = [], el = null var width = r.x.length() @@ -53,50 +52,54 @@ var builder = new function(){ var height = clipper.rooms[r.id].height if (r.sides & FRONT) { - el = wall('.front') + el = make_wall('.front') el.width = width el.height = height el.rotationY = PI el.x = r.x.a + width/2 el.y = height/2 el.z = r.y.a + rm.$walls.push(el.el) list.push(el) } if (r.sides & BACK) { - var el = wall('.back') + var el = make_wall('.back') el.width = width el.height = height el.rotationY = 0 el.x = r.x.b - width/2 el.y = height/2 el.z = r.y.b + rm.$walls.push(el.el) list.push(el) } if (r.sides & LEFT) { - el = wall('.left') + el = make_wall('.left') el.rotationY = HALF_PI el.height = height el.width = depth el.x = r.x.a el.y = height/2 el.z = r.y.a + depth/2 + rm.$walls.push(el.el) list.push(el) } if (r.sides & RIGHT) { - el = wall('.right') + el = make_wall('.right') el.rotationY = -HALF_PI el.height = height el.width = depth el.x = r.x.b el.y = height/2 el.z = r.y.b - depth/2 + rm.$walls.push(el.el) list.push(el) } return list } - function floors(rm){ + function build_floors(rm){ var list = [], el = null var already_constructed = rm.intersects.filter(function(rr){ return rr.constructed }) @@ -115,28 +118,38 @@ var builder = new function(){ else if (already_constructed[i].rect.intersects(r)) { intersected = true if (rm.height < already_constructed[i].height) { - list = list.concat( ceiling_walls( rm, already_constructed[i], r ) ) + list = list.concat( make_ceiling_walls( rm, already_constructed[i], r ) ) } } } if (! intersected) { - list.push( ground(rm, r) ) - list.push( ceiling(rm, r) ) + el = make_floor(rm, r) + list.push( el ) + rm.$floor.push(el.el) + + el = make_ceiling(rm, r) + list.push( el ) + rm.$ceiling.push(el.el) } }) } else { // render floor and ceiling for the entire rectangle - list.push( ground(rm, rm.rect) ) - list.push( ceiling(rm, rm.rect) ) + el = make_floor(rm, rm.rect) + list.push( el ) + rm.$floor.push(el.el) + + el = make_ceiling(rm, rm.rect) + list.push( el ) + rm.$ceiling.push(el.el) } rm.constructed = true return list } - function ceiling_walls( lo, hi, r ){ + function make_ceiling_walls( lo, hi, r ){ var list = [] var width = r.x.length() @@ -144,7 +157,7 @@ var builder = new function(){ var height = hi.height - lo.height if (! (r.sides & LEFT) && r.x.a == hi.rect.x.a) { - el = wall('.left') + el = make_wall('.left') el.rotationY = HALF_PI el.height = height el.width = depth @@ -157,7 +170,7 @@ var builder = new function(){ } if (! (r.sides & RIGHT) && r.x.b == hi.rect.x.b) { - el = wall('.right') + el = make_wall('.right') el.rotationY = -HALF_PI el.height = height el.width = depth @@ -169,7 +182,7 @@ var builder = new function(){ } if (! (r.sides & FRONT) && r.y.a == hi.rect.y.a) { - el = wall('.front') + el = make_wall('.front') el.width = width el.height = height el.rotationY = PI @@ -181,7 +194,7 @@ var builder = new function(){ } if (! (r.sides & BACK) && r.y.b == hi.rect.y.b) { - el = wall('.back') + el = make_wall('.back') el.width = width el.height = height el.rotationY = 0 @@ -194,40 +207,36 @@ var builder = new function(){ return list } - function ground(rm, r){ + function make_floor(rm, r){ var width = r.x.length() var depth = r.y.length() - var el = wall('.bottom') + var el = make_wall('.floor') el.height = depth el.width = width el.x = r.x.a + width/2 el.y = 0 el.z = r.y.a + depth/2 el.rotationX = PI/2 - el.el.style.backgroundColor = "#f00" - - rm.floors.push(el) return el } - function ceiling(rm, r){ + function make_ceiling(rm, r){ var width = r.x.length() var depth = r.y.length() var height = rm.height - var el = wall('.top') + var el = make_wall('.ceiling') el.height = depth el.width = width el.x = r.x.a + width/2 el.y = height el.z = r.y.a + depth/2 el.rotationX = -PI/2 - el.el.style.backgroundColor = "#00f" return el } - function wall(klass){ + function make_wall(klass){ var el = new MX.Object3D(".face" + (klass || "")) el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1 el.z = el.y = el.x = 0 diff --git a/assets/javascripts/rectangles/engine/clipper.js b/assets/javascripts/rectangles/engine/clipper.js index bcd0586..43a1bab 100644 --- a/assets/javascripts/rectangles/engine/clipper.js +++ b/assets/javascripts/rectangles/engine/clipper.js @@ -97,10 +97,6 @@ var clipper = new function(){ base.regions = sort_rects_by_position(regions) } - // generate floor and ceiling for some regions - // generate walls from surviving regions - // generate ceiling-walls where ceiling has discontinuity - return base } diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js index ebe4447..90cabdd 100644 --- a/assets/javascripts/rectangles/engine/mover.js +++ b/assets/javascripts/rectangles/engine/mover.js @@ -1,11 +1,11 @@ var mover = new function(){ var base = this - var last_room = null + base.room = null base.init = function(){ - last_room = clipper.rooms[0] base.bind() + base.update(scene.camera) } base.bind = function(){ @@ -13,7 +13,7 @@ var mover = new function(){ } base.update = function(pos){ - if (last_room && last_room.rect.contains(pos.x, pos.z)) return; + if (base.room && base.room.rect.contains(pos.x, pos.z)) return; var intersects = [] clipper.rooms.forEach(function(r){ @@ -22,10 +22,16 @@ var mover = new function(){ } }) + $(".face.active").removeClass("active") if (intersects.length) { - $(".face.active").removeClass("active") - last_room = intersects[0] - } + base.room = intersects[0] + base.room.$floor.addClass("active") + base.room.$ceiling.addClass("active") + base.room.$walls.addClass("active") + } + else { + base.room = null + } } } diff --git a/assets/javascripts/rectangles/models/room.js b/assets/javascripts/rectangles/models/room.js index 3d512c3..3ccf1fb 100644 --- a/assets/javascripts/rectangles/models/room.js +++ b/assets/javascripts/rectangles/models/room.js @@ -6,6 +6,9 @@ window.room = (function(){ this.regions = [] this.height = opt.height || 200 this.focused = false + this.$walls = $([]) + this.$floor = $([]) + this.$ceiling = $([]) } room.prototype.toString = function(){ @@ -19,6 +22,9 @@ window.room = (function(){ this.regions = [ copy ] this.intersects = [] this.constructed = false + this.$walls = $([]) + this.$floor = $([]) + this.$ceiling = $([]) } room.prototype.clipTo = function(r){ diff --git a/rectangles.html b/rectangles.html index f5d9404..9713561 100644 --- a/rectangles.html +++ b/rectangles.html @@ -22,6 +22,17 @@ body > div { .back { background-color: #bbb; } .left { background-color: #aaa; } .right { background-color: #888; } +.floor { background-color: #00f; } +.ceiling { background-color: #f00; } + +.active.front { background-color: #ddf; } +.active.back { background-color: #bbe; } +.active.left { background-color: #aad; } +.active.right { background-color: #88b; } +.active.floor { background-color: #0ff; } +.active.ceiling { background-color: #ff0; } + + #map canvas { border-bottom: 2px solid #ddd; border-right: 2px solid #ddd; |
