Rooms.mover = new function(){ var base = this base.room = null base.noclip = false base.init = function(){ base.bind() base.update(scene.camera) } base.bind = function(){ app.on("move", base.update) keys.on("backslash", function(e){ if ( ! (e.ctrlKey || e.metaKey) ) return base.noclip = ! base.noclip base.room = null app.movements.gravity( ! base.noclip ) }) } base.update = function(pos){ var radius = scene.camera.radius cam.y = pos.y // if we were in a room already.. if (base.room) { // check if we're still in the room if (base.room.rect.containsDisc(pos.x, pos.z, radius)) { cam.x = pos.x cam.z = pos.z return } // check if we've breached one of the walls.. clamp position if so var collision = base.room.collidesDisc(cam, pos, radius) /* var dz = new vec2( cam.z, pos.z ) dz.normalize() var dx = new vec2( cam.x, pos.x ) dx.normalize() Walls.list.forEach(function(wall){ switch (wall.side) { case FRONT: break case BACK: break case LEFT: if (cam.x >= wall.edge + radius && wall.edge + radius >= pos.x && (wall.vec.intersects(dz) ) { // intersects the wall.. next check if it intersects any of the surface frames wall.surface.faces.some(function(face, i){ // if we can pass through the wall at this point, we do not need to clamp if (face.y.a === 0 && face.x.intersects(dz)) { dz.a = pos.z = face.x.clamp(pos.z) dz.b = cam.z dz.normalize() return true } }) } break case RIGHT: if (cam.x <= wall.edge - radius && wall.edge - radius <= pos.x && (wall.vec.contains(cam.z) || wall.vec.contains(pos.z)) ) { // intersects } break } }) */ if (collision && ! base.noclip) { cam.x = (collision & LEFT_RIGHT) ? base.room.rect.x.clampDisc(pos.x, radius) : pos.x cam.z = (collision & FRONT_BACK) ? base.room.rect.y.clampDisc(pos.z, radius) : pos.z return } // in this case, we appear to have left the room.. // $(".face.active").removeClass("active") Walls.clearBodyColor() base.room = null } // collision test failed, so update position cam.x = pos.x cam.z = pos.z // determine what room we are in now var intersects = Rooms.filter(function(r){ return r.rect.contains(pos.x, pos.z) }) // did we actually enter a room? if (intersects.length) { base.room = intersects[0] if (! base.noclip) { Walls.setBodyColor() } app.tube("change-room", { room: base.room }) } } }