diff --git a/client/garageserver.io.js b/client/garageserver.io.js index f4a0f56..335edc8 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -22,33 +22,36 @@ window.GarageServerIO = (function (window, socketio) { updates = [], + options = null, + // TODO: DONE CALLBACK - connectToGarageServer = function (path, options) { + connectToGarageServer = function (path, opts) { socket = io.connect(path + '/garageserver.io'); - registerSocketEvents(options); + options = opts; + registerSocketEvents(); }, - registerSocketEvents = function (options) { + registerSocketEvents = function () { socket.on('update', function(data) { - updatePlayerInput(data, options); + updatePlayerInput(data); if (options.logging) { console.log('garageserver.io:: socket update ' + data); } }); - socket.on('ping', function(data, options) { + socket.on('ping', function(data) { if (options.logging) { console.log('garageserver.io:: socket ping ' + data); } }); - socket.on('removePlayer', function(id, options) { - removePlayer(id, options); + socket.on('removePlayer', function(id) { + removePlayer(id); if (options.logging) { console.log('garageserver.io:: socket removePlayer ' + id); } }); }, - updatePlayerInput = function (data, options) { + updatePlayerInput = function (data) { var playerFound = false, updateFound = false, playerIdx = 0, @@ -109,7 +112,7 @@ window.GarageServerIO = (function (window, socketio) { var currentTime = new Date().getTime(); socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime }); }, - + processPlayerInput = function () { for (var i = 0; i < players.length; i ++) { if (players[i].id !== socket.socket.sessionid) { @@ -118,7 +121,16 @@ window.GarageServerIO = (function (window, socketio) { } }, - removePlayer = function (id, options) { + processClientInput = function () { + for (var i = 0; i < players.length; i ++) { + if (players[i].id === socket.socket.sessionid) { + + break; + } + } + }, + + removePlayer = function (id) { for (var i = 0; i < players.length; i ++) { if (players[i].id === id) { players.splice(i, 1)[0]; @@ -134,7 +146,8 @@ window.GarageServerIO = (function (window, socketio) { return { connectToGarageServer: connectToGarageServer, addPlayerInput: addPlayerInput, - processPlayerInput: processPlayerInput + processPlayerInput: processPlayerInput, + processClientInput: processClientInput }; }) (window, io); \ No newline at end of file diff --git a/example/app.js b/example/app.js index fd1bee4..0a4fc5f 100644 --- a/example/app.js +++ b/example/app.js @@ -43,4 +43,23 @@ var sockets = io.listen(server); sockets.set('log level', 0); -garageServer.createGarageServer(sockets, { logging: true }); +garageServer.createGarageServer(sockets, { + logging: true, + onUpdatePhysics: function (state, inputs) { + var i = 0; + for (i = 0; i < inputs.length; i ++) { + if (inputs[i] === 'left') { + state.x -= 1; + } + else if (inputs[i] === 'right') { + state.x += 1; + } + else if (inputs[i] === 'down') { + state.y += 1; + } + else if (inputs[i] === 'up') { + state.y -= 1; + } + } + } +}); diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index f2fe3db..261e246 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -15,19 +15,15 @@ $(function () { handleInput = function () { if (keyboard.pressed('left')) { - x -= 1; GarageServerIO.addPlayerInput('left'); } if (keyboard.pressed('right')) { - x += 1; GarageServerIO.addPlayerInput('right'); } if (keyboard.pressed('down')) { - y += 1; GarageServerIO.addPlayerInput('down'); } if (keyboard.pressed('up')) { - y -= 1; GarageServerIO.addPlayerInput('up'); } }, @@ -35,12 +31,13 @@ $(function () { processInputs = function () { //GarageServerIO.processPlayerInput - + //GarageServerIO.processClientInput }, update = function () { requestAnimFrame(update); handleInput(); + processInputs(); ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); ctxGameCanvas.fillRect(x, y, 10, 10);