diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:10 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-06-03 16:24:28 -0400 |
| commit | 607f69c67a5b4dc72d2754192e3cdf67d0ad11d0 (patch) | |
| tree | 6556e7922c5bedb274bb1650e5dd100643a7895d /client/assets/javascripts/rectangles/engine/map | |
| parent | d31259291d807c851de4396921e0c26b6dd8dce2 (diff) | |
partitioning client and serveR
Diffstat (limited to 'client/assets/javascripts/rectangles/engine/map')
3 files changed, 333 insertions, 0 deletions
diff --git a/client/assets/javascripts/rectangles/engine/map/_map.js b/client/assets/javascripts/rectangles/engine/map/_map.js new file mode 100644 index 0000000..53084bb --- /dev/null +++ b/client/assets/javascripts/rectangles/engine/map/_map.js @@ -0,0 +1,40 @@ +/* +*/ + +window.ctx = window.w = window.h = null; + +var map = new function(){ + var base = this + base.el = document.querySelector("#map") + base.dimensions = new vec2(500,500) + base.bounds = new vec2(500,500) + base.center = new vec2(0,0) + + 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, + base.center.a + sides.a, -base.center.b + sides.b ) + } + + base.zoom = 1/8 + base.zoom_exponent = -3 + base.set_zoom = function (n) { + base.zoom_exponent = n + base.zoom = pow(2, base.zoom_exponent) + } + + var canvas = document.createElement("canvas") + var ctx = window.ctx = canvas.getContext("2d") + var w = window.w = canvas.width = 500 + var h = window.h = canvas.height = 500 + document.querySelector("#map").appendChild(canvas) + + base.update = function(){ + base.draw.animate() + } + + base.toggle = function(){ + $(base.el).toggle() + } + +} diff --git a/client/assets/javascripts/rectangles/engine/map/draw.js b/client/assets/javascripts/rectangles/engine/map/draw.js new file mode 100644 index 0000000..b2fc05f --- /dev/null +++ b/client/assets/javascripts/rectangles/engine/map/draw.js @@ -0,0 +1,162 @@ + +map.draw = new function(){ + + var base = this + + base.animate = function(){ + ctx.save() + map.draw.clear_canvas() + map.draw.ruler() + + ctx.translate( map.bounds.a * 1/2, map.bounds.b * 1/2) + ctx.scale( map.zoom, map.zoom ) + ctx.translate( map.center.a, map.center.b) + ctx.scale( -1, 1 ) + + map.draw.regions(Rooms.regions) + map.draw.mouse(map.ui.mouse.cursor) + map.draw.coords() + scene && map.draw.camera(scene.camera) + + ctx.restore() + } + + base.clear_canvas = function(){ + ctx.fillStyle = "rgba(255,255,255,0.99)" + ctx.clearRect(0,0,w,h) + ctx.fillRect(0,0,w,h) + } + + base.ruler = function (){ + ctx.strokeStyle = "rgba(80,80,80,0.5)" + ctx.lineWidth = 1 + var len = 5 + for (var i = 0.5; i < w; i += 10) { + line(i, 0, i, len) + line(0, i, len, i) + } + } + + base.regions = function(regions){ + for (var i = 0; i < regions.length; i++) { + if (regions[i].dupe) continue + ctx.fillStyle = colors[i % colors.length] + fill_region(regions[i]) + stroke_sides(regions[i]) + } + } + + base.mouse = function(mouse){ + var radius = 3 / map.zoom + + ctx.fillStyle = "rgba(255,0,0,0.4)"; + ctx.beginPath(); + ctx.arc(mouse.x.b, mouse.y.b, radius, 0, 2*Math.PI, false); + ctx.fill(); + + if (mouse.width() != 0 && mouse.height() != 0) { + if (map.ui.dragging) { + stroke_rect(mouse) + } + else { + ctx.fillStyle = "rgba(255,255,0,0.5)" + fill_region( mouse.clone().translate() ) + } + } + } + + base.camera = function(cam){ + ctx.lineWidth = 0.5 + + ctx.save() + + ctx.translate(cam.x, cam.z) + ctx.rotate(cam.rotationY) + + var radius = 3 / map.zoom + + ctx.fillStyle = '#888'; + + ctx.beginPath(); + ctx.moveTo(0,0) + ctx.lineTo(-radius,-radius/2) + ctx.lineTo(0,radius*3) + ctx.lineTo(radius,-radius/2) + ctx.moveTo(0,0) + ctx.fill() + + ctx.restore() + } + + base.coords = function(){ + ctx.fillStyle = "#888"; + dot_at(0,0) + ctx.fillStyle = "#bbb"; + dot_at(100,0) + dot_at(0,100) + ctx.fillStyle = "#ddd"; + dot_at(200,0) + dot_at(0,200) + ctx.fillStyle = "#eee"; + dot_at(300,0) + dot_at(0,300) + + ctx.strokeStyle = "rgba(0,0,0,0.1)" + ctx.lineWidth = 1/map.zoom + + var sides = map.sides() + var quant = sides.clone().quantize(200) + for (var x = quant.x.a - 200; x <= quant.x.b; x += 200) { + line(x, sides.y.a, x, sides.y.b) + } + for (var y = quant.y.a - 200; y <= quant.y.b; y += 200) { + line(sides.x.a, y, sides.x.b, y) + } + } + + // + + function line (x,y,a,b,translation){ + if (translation) { + x += translation.a + a += translation.a + y += translation.b + b += translation.b + } + ctx.beginPath() + ctx.moveTo(x,y) + ctx.lineTo(a,b) + ctx.stroke() + } + + function fill_region(r){ + ctx.fillRect(r.x.a + r.translation.a, + r.y.a + r.translation.b, + r.x.length(), + r.y.length()) + } + + function stroke_sides (r){ + if (r.sides & FRONT) line(r.x.a, r.y.a, r.x.b, r.y.a) + if (r.sides & BACK) line(r.x.a, r.y.b, r.x.b, r.y.b) + if (r.sides & LEFT) line(r.x.a, r.y.a, r.x.a, r.y.b) + if (r.sides & RIGHT) line(r.x.b, r.y.a, r.x.b, r.y.b) + } + + function stroke_rect (r){ + line(r.x.a, r.y.a, r.x.b, r.y.b, r.translation) + } + + function dot_at (x,z){ + ctx.save() + ctx.translate(x,z) + + var radius = 2 / map.zoom + + ctx.beginPath(); + ctx.arc(0, 0, radius, 0, 2*Math.PI, false); + ctx.fill(); + + ctx.restore() + } +}
\ No newline at end of file diff --git a/client/assets/javascripts/rectangles/engine/map/ui.js b/client/assets/javascripts/rectangles/engine/map/ui.js new file mode 100644 index 0000000..6e9a5ab --- /dev/null +++ b/client/assets/javascripts/rectangles/engine/map/ui.js @@ -0,0 +1,131 @@ + +map.ui = new function(){ + + var base = this + + base.creating = base.dragging = base.resizing = false + + base.mouse = new mouse({ + el: map.el, + down: down, + move: move, + drag: drag, + up: up, + rightclick: rightclick, + }) + + base.wheel = new wheel({ + el: map.el, + update: mousewheel, + }) + + // + + function down (e, cursor){ + cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a) + cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b) + + if (e.ctrlKey || e.which === 3) { + cursor.quantize(1) + map.center.a = cursor.x.a + map.center.b = -cursor.y.a + console.log(map.center+"") + cursor.x.b = cursor.x.a + cursor.y.b = cursor.y.a + base.mouse.down = false + e.preventDefault() + e.stopPropagation() + return + } + + var intersects = Rooms.filter(function(r){ + return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) + }) + + if (intersects.length) { + base.dragging = intersects[0] + base.resizing = base.dragging.rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom) + base.dragging.rect.translation.sides = base.resizing + } + else { + base.creating = true + } + + if (e.shiftKey && base.dragging) { + base.dragging.rect.quantize(10) + } + } + + function move (e, cursor) { + cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a) + cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b) + } + + function drag (e, cursor) { + cursor.x.b = ((cursor.x.b/w)+0.5) * map.bounds.a / map.zoom + map.center.a + cursor.y.b = ((cursor.y.b/h)-0.5) * map.bounds.b / map.zoom - map.center.b + + if (base.resizing) { + var x_length = base.dragging.rect.x.length(), + y_length = base.dragging.rect.y.length() + + if (base.resizing & LEFT) { + base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), x_length - side_max, x_length - side_min ) + } + if (base.resizing & RIGHT) { + base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), side_min - x_length, side_max - x_length ) + } + if (base.resizing & FRONT) { + base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), y_length - side_max, y_length - side_min ) + } + if (base.resizing & BACK) { + base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), side_min - y_length, side_max - y_length ) + } + } + else if (base.dragging) { + base.dragging.rect.translation.a = cursor.x.magnitude() + base.dragging.rect.translation.b = cursor.y.magnitude() + } + } + + function up (e, cursor, new_cursor) { + new_cursor.x.div(w).add(0.5).mul(map.bounds.a / map.zoom).add(map.center.a) + new_cursor.y.div(h).sub(0.5).mul(map.bounds.b / map.zoom).sub(map.center.b) + + if (base.creating) { + if (cursor.height() > side_min && cursor.width() > side_min) { + cursor.x.abs().quantize(1) + cursor.y.abs().quantize(1) + Rooms.add_with_rect( cursor ) + } + } + + if (base.resizing) { + base.dragging.rect.resize() + } + else if (base.dragging) { + base.dragging.rect.translate() + } + + base.creating = base.dragging = base.resizing = false + } + + function mousewheel (e, val, delta){ + var cursor = base.mouse.cursor + + var intersects = Rooms.filter(function(r){ + return r.focused = r.rect.contains(cursor.x.a, cursor.y.a) + }) + + if (intersects.length) { + intersects[0].height = clamp( ~~(intersects[0].height - delta), height_min, height_max ) + Rooms.clipper.update() + } + else { + map.set_zoom(map.zoom_exponent - delta/20) + } + } + + function rightclick (e){ + } +} |
