diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 1755ccf..1908b56 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -3,7 +3,7 @@ var garageServerGame = require('./garageservergame'); function GarageServer (socketio, options) { this.io = socketio; this.registerSocketEvents(options); - this.game = new garageServerGame.createGame(options); + this.game = new garageServerGame.createGame(socketio, options); } GarageServer.prototype.registerSocketEvents = function (options) { diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index a0b393f..4e7c5c1 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -1,14 +1,17 @@ -function GarageServerGame (options) { +function GarageServerGame (socketio, options) { var self = this; this.players = []; + this.io = socketio; this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, options.physicsInterval ? options.physicsInterval : 15); this.playersIntervalId = setInterval(function () { self.updatePlayers(options); }, options.playersInterval ? options.playersInterval : 45); } GarageServerGame.prototype.updatePlayers = function (options) { + var currentTime = new Date().getTime(); for(var i = 0; i < this.players.length; i ++) { + this.io.emit('playerUpdate', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime }); } }; @@ -19,6 +22,7 @@ GarageServerGame.prototype.updatePhysics = function (options) { this.players[i].state = options.onUpdatePhysics(this.players[i].state, this.players[i].inputs); } this.players[i].inputs = []; + this.players[i].sequence += this.players[i].inputs.length; } } }; @@ -33,7 +37,8 @@ GarageServerGame.prototype.addPlayer = function (client) { var player = { client: client, state: {}, - inputs: [] + inputs: [], + sequence: 0 }; this.players.push(player); @@ -56,6 +61,6 @@ GarageServerGame.prototype.addPlayerInput = function (client, input) { } }; -exports.createGame = function (options){ - return new GarageServerGame(options); +exports.createGame = function (socketio, options){ + return new GarageServerGame(socketio, options); }; \ No newline at end of file