diff options
| author | kdam0 <kumar.damani@mail.utoronto.ca> | 2016-11-19 03:00:45 +0000 |
|---|---|---|
| committer | kdam0 <kumar.damani@mail.utoronto.ca> | 2016-11-19 03:00:45 +0000 |
| commit | 81b0036addf46e99327d232d9f0a6a5a2d2a9220 (patch) | |
| tree | 6ad6b9633c9038938afbf9ab8c3f7e43192fcbf4 /assets/js/global.js | |
| parent | 9fa9264c1e2ad0e23948bca96bd44cf29895cd2c (diff) | |
testing cube mechanics
Diffstat (limited to 'assets/js/global.js')
| -rw-r--r-- | assets/js/global.js | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/assets/js/global.js b/assets/js/global.js index dd31ca8..021f14b 100644 --- a/assets/js/global.js +++ b/assets/js/global.js @@ -9,17 +9,18 @@ window.onload = function () { window.CANVAS_X_OFFSET = 200; window.CANVAS_Y_OFFSET = 200; + draw(); - //handleKeys(); + // mapBoard(208,208); + + + handleArrowKeys(); } function draw() { var coords = generateCubeConfig(); - - consOut(coords); - placeDice(coords[0] , coords[1]); // draw the cube @@ -41,8 +42,7 @@ function generateCubeConfig() { // generate a random y between 1-3 incl. var y = Math.floor((Math.random()) * 3 + 1); - consOut(x); - + consOut(arguments.callee.name + '() ' + 'x: ' + x + ' | y: ' + y); return [x,y]; } @@ -149,33 +149,38 @@ function drawDice() { render(); } -function placeDice(x, y) { +function placeDice( x, y ) { var x_inc = (x-1) * CANVAS_X_OFFSET; var y_inc = (y-1) * CANVAS_Y_OFFSET; // get the current position var position = $('#cube-canvas').position(); + consOut(arguments.callee.name + '() ' + 'x: ' + position.left + ' | y: ' + position.top); //initial position + // adjust dice position $('#cube-canvas').css({ left: position.left + x_inc, top: position.top + y_inc }); - consOut('x: ' + position.left + ' | y: ' + position.top); //initial position - consOut("dice placed!"); + consOut(arguments.callee.name + '() ' + "dice placed!"); } -function handleKeys() { +function handleArrowKeys() { $(document).keydown(function(e) { switch (e.which) { - case 37: - placeDice(3,3); - case 38: + case 37: //left + moveDice('left'); break; - case 39: + case 38: //up + moveDice('up'); break; - case 40: + case 39: //right + moveDice('right'); + break; + case 40: //down + moveDice('down'); break; default: return; } @@ -183,14 +188,35 @@ function handleKeys() { }); } +function moveDice( direction ) { + // get canvas position + var position = $('#cube-canvas').position(); + var coords = mapBoard(position.left, position.top); + var x_coord = coords[0]; + var y_coord = coords[1]; + + if (direction == "left") { + placeDice(x_coord - 1, y_coord); + } +} + + +function mapBoard( x, y ) { + var x_coord = ((x-8) / CANVAS_X_OFFSET) + 1; + var y_coord = ((y-8) / CANVAS_Y_OFFSET) + 1; + + consOut(arguments.callee.name + '() ' + 'x: ' + x_coord + ' | y: ' + y_coord); + return [x_coord, y_coord]; +} + // print mssg to console out only if debuging is set to true -function consOut(mssg) { +function consOut( mssg ) { if (window.debug) { console.log(mssg); } } -function swap(arr, a, b) { +function swap( arr, a, b ) { var temp = arr[b]; arr[b] = arr[a]; arr[a] = temp; |
