From 0453f140d3027a5d019d92a53c66dc4bb305b69e Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Thu, 18 Jul 2013 22:51:02 -0400 Subject: [PATCH] Added onReady callback --- client/garageserver.io.js | 5 +++ example/public/javascripts/game.js | 63 ++++++++++++++++-------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 2e67592..b399960 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -12,6 +12,7 @@ options = { onPing: function (pingDelay), onUpdatePlayerPhysics: function (state, inputs, deltaTime), onInterpolation: function(previousState, targetState, amount) + onReady: function (), logging: true } api methods @@ -199,6 +200,10 @@ var GarageServerIO = (function (socketio) { _stateController.smoothingFactor = data.smoothingFactor; _stateController.worldState = data.worldState; + if(_options.onReady) { + _options.onReady(); + } + setInterval(function (){ _socket.emit('ping', new Date().getTime()); }, _stateController.pingInterval); diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index d133780..58e6ebe 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -7,6 +7,7 @@ $(function () { GarageServerIO.initializeGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: true, + onReady: startGame, onUpdatePlayerPhysics: GamePhysics.getNewPlayerState, onInterpolation: GamePhysics.getInterpolatedState, onWorldState: function (state) { @@ -17,37 +18,39 @@ $(function () { } }); - GameLoop.start( - //Render Loop - function () { - ctxCanvas.clearRect(0, 0, canvas.width, canvas.height); - GarageServerIO.getStates(function (playerStates, entityStates) { - playerStates.forEach(function (player) { - ctxCanvas.fillRect(player.state.x, player.state.y, playerSize, playerSize); - }); + function startGame() { + GameLoop.start( + //Render Loop + function () { + ctxCanvas.clearRect(0, 0, canvas.width, canvas.height); + GarageServerIO.getStates(function (playerStates, entityStates) { + playerStates.forEach(function (player) { + ctxCanvas.fillRect(player.state.x, player.state.y, playerSize, playerSize); + }); - entityStates.forEach(function (entity) { - ctxCanvas.fillRect(entity.state.x, entity.state.y, entitySize, entitySize); + entityStates.forEach(function (entity) { + ctxCanvas.fillRect(entity.state.x, entity.state.y, entitySize, entitySize); + }); }); - }); - }, - //Update Loop - function () { - if (keyboard.pressed('left')) { - GarageServerIO.addInput('left'); + }, + //Update Loop + function () { + if (keyboard.pressed('left')) { + GarageServerIO.addInput('left'); + } + if (keyboard.pressed('right')) { + GarageServerIO.addInput('right'); + } + if (keyboard.pressed('down')) { + GarageServerIO.addInput('down'); + } + if (keyboard.pressed('up')) { + GarageServerIO.addInput('up'); + } + if (keyboard.pressed('space')) { + GarageServerIO.addInput('space'); + } } - if (keyboard.pressed('right')) { - GarageServerIO.addInput('right'); - } - if (keyboard.pressed('down')) { - GarageServerIO.addInput('down'); - } - if (keyboard.pressed('up')) { - GarageServerIO.addInput('up'); - } - if (keyboard.pressed('space')) { - GarageServerIO.addInput('space'); - } - } - ); + ); + } }); \ No newline at end of file