diff options
Diffstat (limited to 'assets')
17 files changed, 231 insertions, 213 deletions
diff --git a/assets/javascripts/mx/extensions/mx.movements.js b/assets/javascripts/mx/extensions/mx.movements.js index f2f9581..074ca9a 100644 --- a/assets/javascripts/mx/extensions/mx.movements.js +++ b/assets/javascripts/mx/extensions/mx.movements.js @@ -260,7 +260,7 @@ MX.Movements = function (cam, viewHeight) { vz = vz || 1 } - var ceiling = (Mover.room ? Mover.room.height : 5000) + var ceiling = (Rooms.mover.room ? Rooms.mover.room.height : 5000) if (pos.y >= ceiling) { vy = 0 diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js index 18d4f05..349f601 100644 --- a/assets/javascripts/rectangles/_env.js +++ b/assets/javascripts/rectangles/_env.js @@ -13,10 +13,10 @@ environment.init = function(){ scene.camera.radius = 20 } - 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 Rooms.list.push( new Room ({ rect: new Rect(-500,-500, 500,500), diff --git a/assets/javascripts/rectangles/map/_map.js b/assets/javascripts/rectangles/engine/map/_map.js index 53084bb..53084bb 100644 --- a/assets/javascripts/rectangles/map/_map.js +++ b/assets/javascripts/rectangles/engine/map/_map.js diff --git a/assets/javascripts/rectangles/map/draw.js b/assets/javascripts/rectangles/engine/map/draw.js index b2fc05f..b2fc05f 100644 --- a/assets/javascripts/rectangles/map/draw.js +++ b/assets/javascripts/rectangles/engine/map/draw.js diff --git a/assets/javascripts/rectangles/map/ui.js b/assets/javascripts/rectangles/engine/map/ui.js index c6ba4b3..6e9a5ab 100644 --- a/assets/javascripts/rectangles/map/ui.js +++ b/assets/javascripts/rectangles/engine/map/ui.js @@ -1,15 +1,8 @@ -var height_min = 200, - height_max = 2000, - side_min = 10, - side_max = 5000, - resize_margin = 8 map.ui = new function(){ var base = this - - base.creating = base.dragging = base.resizing = false base.mouse = new mouse({ @@ -126,7 +119,7 @@ map.ui = new function(){ if (intersects.length) { intersects[0].height = clamp( ~~(intersects[0].height - delta), height_min, height_max ) - Clipper.update() + Rooms.clipper.update() } else { map.set_zoom(map.zoom_exponent - delta/20) diff --git a/assets/javascripts/rectangles/engine/rooms.js b/assets/javascripts/rectangles/engine/rooms/_rooms.js index 93ba7b8..411a093 100644 --- a/assets/javascripts/rectangles/engine/rooms.js +++ b/assets/javascripts/rectangles/engine/rooms/_rooms.js @@ -6,9 +6,9 @@ var Rooms = new function(){ base.regions = [] base.init = function(){ - Builder.init() - Clipper.init() - Mover.init() + Rooms.builder.init() + Rooms.clipper.init() + Rooms.mover.init() } base.filter = function(f){ diff --git a/assets/javascripts/rectangles/engine/builder.js b/assets/javascripts/rectangles/engine/rooms/builder.js index 91bdeb4..8586a7b 100644 --- a/assets/javascripts/rectangles/engine/builder.js +++ b/assets/javascripts/rectangles/engine/rooms/builder.js @@ -1,5 +1,5 @@ -var Builder = new function(){ +Rooms.builder = new function(){ var base = this var els = [] diff --git a/assets/javascripts/rectangles/engine/clipper.js b/assets/javascripts/rectangles/engine/rooms/clipper.js index d814fec..8989ba8 100644 --- a/assets/javascripts/rectangles/engine/clipper.js +++ b/assets/javascripts/rectangles/engine/rooms/clipper.js @@ -1,5 +1,5 @@ -var Clipper = new function(){ +Rooms.clipper = new function(){ var base = this base.init = function(){ diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/rooms/mover.js index 413715a..e67d9bc 100644 --- a/assets/javascripts/rectangles/engine/mover.js +++ b/assets/javascripts/rectangles/engine/rooms/mover.js @@ -1,4 +1,4 @@ -var Mover = new function(){ +Rooms.mover = new function(){ var base = this base.room = null @@ -14,7 +14,7 @@ var Mover = new function(){ keys.on("backslash", function(){ base.noclip = ! base.noclip base.room = null - app.movements.gravity( ! app.movements.gravity() ) + app.movements.gravity( ! base.noclip ) }) } diff --git a/assets/javascripts/rectangles/engine/scenery.js b/assets/javascripts/rectangles/engine/scenery.js deleted file mode 100644 index 1301949..0000000 --- a/assets/javascripts/rectangles/engine/scenery.js +++ /dev/null @@ -1,177 +0,0 @@ -var wall_rotation = {} -wall_rotation[FRONT] = PI -wall_rotation[BACK] = 0 -wall_rotation[LEFT] = HALF_PI -wall_rotation[RIGHT] = -HALF_PI - -var Scenery = new function(){ - - var base = this; - - base.mouse = new mouse ({ use_offset: false }) - - base.init = function(){ - var urls = [ - "http://okfocus.s3.amazonaws.com/office/ducks/duck1.jpg", - "http://okfocus.s3.amazonaws.com/office/ducks/duck2.jpg", - "http://okfocus.s3.amazonaws.com/office/ducks/duck3.jpg", - "http://okfocus.s3.amazonaws.com/office/ducks/duck4.jpg", - "http://okfocus.s3.amazonaws.com/office/ducks/duck5.jpg", - ] - var loader = new Loader(function(){ - base.load(loader.images) - }) - loader.preloadImages(urls) - } - - base.load = function(images){ - images.forEach(function(img){ - img.width = 300 - img.height = ~~(300 * img.naturalHeight/img.naturalWidth) - }) - - Rooms.forEach(function(room){ - room.walls.forEach(function(wall){ - new_image(wall, choice(images)) - }) - }) - } - - function new_image (wall, img) { - var x, z - - if (! wall.fits(img)) return - - var mx_img = new MX.Image({ - src: img.src, - x: 0, - y: Rooms.list[wall.room].height/2 - img.height/2 - 20, - z: 0, - scale: 300/img.naturalWidth, - rotationY: 0, - backface: false, - }) - - var center = wall.center_for(img, null) - mx_img.move({ - x: center.a, - z: center.b, - rotationY: wall_rotation[ wall.side ] - }) - scene.add(mx_img) - - // https://s3.amazonaws.com/luckyplop/f5b2c20e602cdfc86383910f294dcf23d91fa956.png - - var x = 0, y = 0, z = 0, bounds - - // should be proportional to distance from wall - var cursor_amp = 1.5 - var dragging = false - - base.mouse.bind_el(mx_img.el) - base.mouse.tube.on("down", function(e, cursor){ - if (e.target != mx_img.el) return; - dragging = true - x = mx_img.x - y = mx_img.y - z = mx_img.z - bounds = wall.bounds_for(img) - document.body.classList.add("dragging") - }) - base.mouse.tube.on("enter", function(e, new_wall, cursor){ - if (! dragging) return - if (new_wall.uid == wall.uid) return - if (! new_wall.fits(img)) return - - bounds = new_wall.bounds_for(img) - center = new_wall.center_for(img) - - x = center.a - z = center.b - - var wall_group = wall.side | new_wall.side - - if (wall.side !== new_wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) { - switch (wall.side) { - case FRONT: - z = bounds.x.a - break - case BACK: - z = bounds.x.b - break - case LEFT: - x = bounds.x.a - break - case RIGHT: - x = bounds.x.b - break - } - } - - cursor.x.a = cursor.x.b - - mx_img.move({ - x: x, - z: z, - rotationY: wall_rotation[ new_wall.side ] - }) - - wall = new_wall - }) - base.mouse.tube.on("drag", function(e, cursor){ - if (! dragging) return - - mx_img.y = bounds.y.clamp( y - cursor.y.magnitude()*cursor_amp ) - switch (wall.side) { - case FRONT: - mx_img.x = bounds.x.clamp( x - cursor.x.magnitude()*cursor_amp ) - break - case BACK: - mx_img.x = bounds.x.clamp( x + cursor.x.magnitude()*cursor_amp ) - break - case LEFT: - mx_img.z = bounds.x.clamp( z + cursor.x.magnitude()*cursor_amp ) - break - case RIGHT: - mx_img.z = bounds.x.clamp( z - cursor.x.magnitude()*cursor_amp ) - break - } - }) - base.mouse.tube.on("up", function(e, cursor){ - dragging = false - document.body.classList.remove("dragging") - }) - } - - function side_direction (a, b) { - if (a === b) return 0 - if ((a | b) === FRONT_BACK) return 0 - if ((a | b) === LEFT_RIGHT) return 0 - switch (a) { - case FRONT: - return b & LEFT ? -1 : 1 - case BACK: - return b & RIGHT ? -1 : 1 - case LEFT: - return b & FRONT ? -1 : 1 - case RIGHT: - return b & BACK ? -1 : 1 - } - } - -// console.log([ -// compare_sides( LEFT, RIGHT ), -// compare_sides( FRONT, BACK ), -// compare_sides( FRONT, LEFT ), -// compare_sides( FRONT, RIGHT ), -// compare_sides( BACK, RIGHT ), -// compare_sides( BACK, LEFT ), -// compare_sides( LEFT, FRONT ), -// compare_sides( LEFT, BACK ), -// compare_sides( RIGHT, BACK ), -// compare_sides( RIGHT, FRONT ), -// ].join("\n")) - - return base - -} diff --git a/assets/javascripts/rectangles/engine/scenery/_scenery.js b/assets/javascripts/rectangles/engine/scenery/_scenery.js new file mode 100644 index 0000000..dbd9dbe --- /dev/null +++ b/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -0,0 +1,44 @@ + +var Scenery = new function(){ + + var base = this; + + base.images = [] + + base.mouse = new mouse ({ use_offset: false }) + + base.init = function(){ + var urls = [ + "http://okfocus.s3.amazonaws.com/office/ducks/duck1.jpg", + "http://okfocus.s3.amazonaws.com/office/ducks/duck2.jpg", + "http://okfocus.s3.amazonaws.com/office/ducks/duck3.jpg", + "http://okfocus.s3.amazonaws.com/office/ducks/duck4.jpg", + "http://okfocus.s3.amazonaws.com/office/ducks/duck5.jpg", + ] + var loader = new Loader(function(){ + base.load(loader.images) + }) + loader.preloadImages(urls) + } + + base.load = function(images){ + images.forEach(function(img){ + img.width = 300 + img.height = ~~(300 * img.naturalHeight/img.naturalWidth) + }) + + Rooms.forEach(function(room){ + room.walls.forEach(function(wall){ + var img = choice(images) + if (wall.fits(img)) { + var scene_img = new Scenery.image (wall, img) + base.images.push(scene_img) + scene_img.init() + } + }) + }) + } + + return base +} + diff --git a/assets/javascripts/rectangles/engine/scenery/image.js b/assets/javascripts/rectangles/engine/scenery/image.js new file mode 100644 index 0000000..6434603 --- /dev/null +++ b/assets/javascripts/rectangles/engine/scenery/image.js @@ -0,0 +1,115 @@ +Scenery.image = function (wall, img) { + + var base = this + var center + var x = 0, y = 0, z = 0, bounds + var dragging = false + + // should be proportional to distance from wall + var cursor_amp = 1.5 + + base.init = function(){ + base.build() + base.bind() + } + + base.build = function(){ + center = wall.center_for(img) + base.mx_img = new MX.Image({ + src: img.src, + x: center.a, + y: Rooms.list[wall.room].height/2 - img.height/2 - 20, + z: center.b, + scale: 300/img.naturalWidth, + rotationY: wall_rotation[ wall.side ], + backface: false, + }) + scene.add( base.mx_img ) + } + + base.bind = function(){ + Scenery.mouse.bind_el(base.mx_img.el) + Scenery.mouse.on("down", down) + Scenery.mouse.on("enter", switch_wall) + Scenery.mouse.on("drag", drag) + Scenery.mouse.on("up", up) + } + + function down (e, cursor){ + if (e.target != base.mx_img.el) return; + dragging = true + x = base.mx_img.x + y = base.mx_img.y + z = base.mx_img.z + bounds = wall.bounds_for(img) + document.body.classList.add("dragging") + } + + function switch_wall (e, new_wall, cursor){ + if (! dragging) return + if (new_wall.uid == wall.uid) return + if (! new_wall.fits(img)) return + + bounds = new_wall.bounds_for(img) + center = new_wall.center_for(img) + + x = center.a + z = center.b + + var wall_group = wall.side | new_wall.side + + if (wall.side !== new_wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) { + switch (wall.side) { + case FRONT: + z = bounds.x.a + break + case BACK: + z = bounds.x.b + break + case LEFT: + x = bounds.x.a + break + case RIGHT: + x = bounds.x.b + break + } + } + + cursor.x.a = cursor.x.b + + base.mx_img.move({ + x: x, + z: z, + rotationY: wall_rotation[ new_wall.side ] + }) + + wall = new_wall + } + + function drag (e, cursor){ + if (! dragging) return + + base.mx_img.y = bounds.y.clamp( y - cursor.y.magnitude()*cursor_amp ) + switch (wall.side) { + case FRONT: + base.mx_img.x = bounds.x.clamp( x - cursor.x.magnitude()*cursor_amp ) + break + case BACK: + base.mx_img.x = bounds.x.clamp( x + cursor.x.magnitude()*cursor_amp ) + break + case LEFT: + base.mx_img.z = bounds.x.clamp( z + cursor.x.magnitude()*cursor_amp ) + break + case RIGHT: + base.mx_img.z = bounds.x.clamp( z - cursor.x.magnitude()*cursor_amp ) + break + } + } + + function up (e, cursor){ + dragging = false + document.body.classList.remove("dragging") + } + + return base +} diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js index aca9212..7a2ac6f 100644 --- a/assets/javascripts/rectangles/models/rect.js +++ b/assets/javascripts/rectangles/models/rect.js @@ -1,13 +1,4 @@ -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 " - return s -} - window.Rect = (function(){ var Rect = function (x0,y0,x1,y1){ if (x0 instanceof vec2) { diff --git a/assets/javascripts/rectangles/models/room.js b/assets/javascripts/rectangles/models/room.js index 4c5643a..731411c 100644 --- a/assets/javascripts/rectangles/models/room.js +++ b/assets/javascripts/rectangles/models/room.js @@ -1,6 +1,3 @@ -var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 - FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT, FLOOR_CEILING = FLOOR | CEILING - window.Room = (function(){ var Room = function(opt){ diff --git a/assets/javascripts/rectangles/models/wall.js b/assets/javascripts/rectangles/models/wall.js index ae23125..a9c2fd6 100644 --- a/assets/javascripts/rectangles/models/wall.js +++ b/assets/javascripts/rectangles/models/wall.js @@ -1,6 +1,3 @@ -var painting_distance_from_wall = 8 - - window.Wall = (function(){ var Wall = function(opt){ @@ -23,6 +20,7 @@ window.Wall = (function(){ Wall.prototype.reset = function(){ } + Wall.prototype.destroy = function(){ this.mx.forEach(function(mx){ mx.destroy && mx.destroy() diff --git a/assets/javascripts/rectangles/util/constants.js b/assets/javascripts/rectangles/util/constants.js new file mode 100644 index 0000000..db4eebf --- /dev/null +++ b/assets/javascripts/rectangles/util/constants.js @@ -0,0 +1,46 @@ +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT, FLOOR_CEILING = FLOOR | CEILING + +var height_min = 200, + height_max = 2000, + side_min = 10, + side_max = 5000, + resize_margin = 8 + +var painting_distance_from_wall = 8 + +var wall_rotation = {} +wall_rotation[FRONT] = PI +wall_rotation[BACK] = 0 +wall_rotation[LEFT] = HALF_PI +wall_rotation[RIGHT] = -HALF_PI + + + + + + +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 " + return s +} + +function side_direction (a, b) { + if (a === b) return 0 + if ((a | b) === FRONT_BACK) return 0 + if ((a | b) === LEFT_RIGHT) return 0 + switch (a) { + case FRONT: + return b & LEFT ? -1 : 1 + case BACK: + return b & RIGHT ? -1 : 1 + case LEFT: + return b & FRONT ? -1 : 1 + case RIGHT: + return b & BACK ? -1 : 1 + } +} diff --git a/assets/javascripts/rectangles/util/mouse.js b/assets/javascripts/rectangles/util/mouse.js index ef6fa0f..16ca101 100644 --- a/assets/javascripts/rectangles/util/mouse.js +++ b/assets/javascripts/rectangles/util/mouse.js @@ -58,6 +58,14 @@ function mouse (opt) { base.bind() } + base.on = function(){ + base.tube.on.apply(base.tube, arguments) + } + + base.off = function(){ + base.tube.off.apply(base.tube, arguments) + } + base.bind = function(){ if (opt.el) { opt.el.addEventListener("mousedown", base.mousedown) @@ -66,9 +74,12 @@ function mouse (opt) { window.addEventListener("mousemove", base.mousemove) window.addEventListener("mouseup", base.mouseup) } + base.bind_el = function(el){ el.addEventListener("mousedown", base.mousedown) - // todo.. need a way to remove this event potentially? + } + base.unbind_el = function(el){ + el.removeEventListener("mousedown", base.mousedown) } function positionFromMouse(e) { |
