diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 335edc8..2205aac 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -102,6 +102,19 @@ window.GarageServerIO = (function (window, socketio) { } }, + removePlayer = function (id) { + for (var i = 0; i < players.length; i ++) { + if (players[i].id === id) { + players.splice(i, 1)[0]; + return; + } + } + + if (options.onPlayerDisconnect) { + options.onPlayerDisconnect(id); + } + }, + addPlayerInput = function (input) { sequenceNumber += 1; inputs.push(input); @@ -129,17 +142,18 @@ window.GarageServerIO = (function (window, socketio) { } } }, - - removePlayer = function (id) { + + getPlayerStates = function (stateCallback) { + var maxUpdate = 0; for (var i = 0; i < players.length; i ++) { - if (players[i].id === id) { - players.splice(i, 1)[0]; - return; + if (players[i].updates.length > 0) { + maxUpdate = players[i].updates.length - 1; + stateCallback(players[i].updates[maxUpdate].state); } } - - if (options.onPlayerDisconnect) { - options.onPlayerDisconnect(id); + if (updates.length > 0) { + maxUpdate = updates.length - 1; + stateCallback(updates[maxUpdate].state); } }; @@ -147,7 +161,8 @@ window.GarageServerIO = (function (window, socketio) { connectToGarageServer: connectToGarageServer, addPlayerInput: addPlayerInput, processPlayerInput: processPlayerInput, - processClientInput: processClientInput + processClientInput: processClientInput, + getPlayerStates: getPlayerStates }; }) (window, io); \ No newline at end of file diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index e9eb3ed..b3d2c79 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -1,5 +1,5 @@ $(function () { - GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: true }); + GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: false }); var gameCanvas = document.getElementById('gameCanvas'), @@ -36,7 +36,10 @@ $(function () { draw = function () { ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); - ctxGameCanvas.fillRect(x, y, 10, 10); + + GarageServerIO.getPlayerStates(function (state) { + ctxGameCanvas.fillRect(state.x, state.y, 10, 10); + }); }, update = function () {