diff --git a/example/app.js b/example/app.js index 0a4fc5f..8d53f89 100644 --- a/example/app.js +++ b/example/app.js @@ -47,19 +47,24 @@ garageServer.createGarageServer(sockets, { logging: true, onUpdatePhysics: function (state, inputs) { var i = 0; + if (!state.x && !state.y) { + state.x = 0; + state.y = 0; + } for (i = 0; i < inputs.length; i ++) { - if (inputs[i] === 'left') { + if (inputs[i].input === 'left') { state.x -= 1; } - else if (inputs[i] === 'right') { + else if (inputs[i].input === 'right') { state.x += 1; } - else if (inputs[i] === 'down') { + else if (inputs[i].input === 'down') { state.y += 1; } - else if (inputs[i] === 'up') { + else if (inputs[i].input === 'up') { state.y -= 1; } } + return state; } }); diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 261e246..e9eb3ed 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -13,7 +13,7 @@ $(function () { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000/60); }; })(), - handleInput = function () { + processClientInput = function () { if (keyboard.pressed('left')) { GarageServerIO.addPlayerInput('left'); } @@ -28,18 +28,24 @@ $(function () { } }, - processInputs = function () { + processServerInput = function () { //GarageServerIO.processPlayerInput //GarageServerIO.processClientInput }, + + draw = function () { + ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); + ctxGameCanvas.fillRect(x, y, 10, 10); + }, update = function () { requestAnimFrame(update); - handleInput(); - processInputs(); - ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); - ctxGameCanvas.fillRect(x, y, 10, 10); + + processClientInput(); + processServerInput(); + + draw(); var thisFrameFPS = 1000 / ((now=new Date) - lastUpdate); fps += (thisFrameFPS - fps) / fpsFilter;