diff options
| -rw-r--r-- | assets/javascripts/rectangles/_env.js | 8 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/engine/mover.js | 23 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/rect.js | 3 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/models/room.js | 38 |
4 files changed, 49 insertions, 23 deletions
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js index f014a8e..5b4643a 100644 --- a/assets/javascripts/rectangles/_env.js +++ b/assets/javascripts/rectangles/_env.js @@ -9,10 +9,10 @@ environment.init = function(){ "rotationY": PI/2, // PI }) -// map.center.a = scene.camera.x -// map.center.b = scene.camera.z - map.center.a = 0 - map.center.b = 0 + map.center.a = scene.camera.x + map.center.b = scene.camera.z +// map.center.a = 0 +// map.center.b = 0 clipper.rooms.push( new room ({ rect: new rect(0,0, 500,500), diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js index 5a1f7d9..311ed83 100644 --- a/assets/javascripts/rectangles/engine/mover.js +++ b/assets/javascripts/rectangles/engine/mover.js @@ -15,15 +15,17 @@ var mover = new function(){ base.update = function(pos){ cam.y = pos.y - // if we were in a room + // if we were in a room already.. if (base.room) { + // check if we're still in the room if (base.room.rect.contains(pos.x, pos.z)) { cam.x = pos.x cam.z = pos.z return } - // check if we've crossed one of the walls.. clamp position if so + + // check if we've breached one of the walls.. clamp position if so var collision = base.room.collides(pos.x, pos.z) if (collision) { if (! (collision & LEFT || collision & RIGHT)) { @@ -34,26 +36,29 @@ var mover = new function(){ } return } + + // in this case, we appear to have left the room.. + $(".face.active").removeClass("active") + base.room = null } + + // collision test failed, so update position + cam.x = pos.x + cam.z = pos.z - // otherwise we've either entered into a new room, or broken free + // determine what room we are in now var intersects = clipper.rooms.filter(function(r){ return r.rect.contains(pos.x, pos.z) }) - $(".face.active").removeClass("active") + // 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") } - else { - base.room = null - } - cam.x = pos.x - cam.z = pos.z } } diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js index d18c6af..86cdae3 100644 --- a/assets/javascripts/rectangles/models/rect.js +++ b/assets/javascripts/rectangles/models/rect.js @@ -1,4 +1,5 @@ -var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8 +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, + FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT window.rect = (function(){ var rect = function (x0,y0,x1,y1){ diff --git a/assets/javascripts/rectangles/models/room.js b/assets/javascripts/rectangles/models/room.js index c760174..4d83d56 100644 --- a/assets/javascripts/rectangles/models/room.js +++ b/assets/javascripts/rectangles/models/room.js @@ -4,8 +4,10 @@ window.room = (function(){ this.id = opt.id || clipper.rooms.length this.rect = opt.rect this.regions = [] + this.height = opt.height || 200 this.focused = false + this.$walls = $([]) this.$floor = $([]) this.$ceiling = $([]) @@ -20,8 +22,10 @@ window.room = (function(){ copy.id = this.id copy.sides = FRONT | BACK | LEFT | RIGHT this.regions = [ copy ] + this.intersects = [] this.constructed = false + this.$walls = $([]) this.$floor = $([]) this.$ceiling = $([]) @@ -42,21 +46,37 @@ window.room = (function(){ } room.prototype.collides = function(x,y){ - var collision = 0 + var collision = 0, wall_collision, contains_x, contains_y this.regions.forEach(function(r){ if (! r.sides) return + + wall_collision = 0 - if ((r.sides & FRONT) && y < r.y.a && r.x.contains(x)) { - collision |= FRONT + if ((r.sides & FRONT) && y < r.y.a) { + wall_collision |= FRONT + } + if ((r.sides & BACK) && r.y.b < y) { + wall_collision |= BACK } - if ((r.sides & BACK) && r.y.b < y && r.x.contains(x)) { - collision |= BACK + if ((r.sides & LEFT) && x < r.x.a) { + wall_collision |= LEFT + } + if ((r.sides & RIGHT) && r.x.b < x) { + wall_collision |= RIGHT + } + if (! wall_collision) return + + contains_y = r.y.contains(y) + contains_x = r.x.contains(x) + + if (contains_x) { + collision |= wall_collision & FRONT_BACK } - if ((r.sides & LEFT) && x < r.x.a && r.y.contains(y)) { - collision |= LEFT + else if (contains_y) { + collision |= wall_collision & LEFT_RIGHT } - if ((r.sides & RIGHT) && r.x.b < x && r.y.contains(y)) { - collision |= RIGHT + else if (bitcount(wall_collision) > 1) { + collision |= wall_collision } }) return collision |
