diff --git a/client/garageserver.io.js b/client/garageserver.io.js index f14b0a0..cb6a34f 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -7,7 +7,7 @@ options = { onPlayerRemove: function (id), onGameState: function (), onPing: function (pingDelay), - onUpdatePlayerPhysics: function (state, inputs), + onUpdatePlayerPhysics: function (state, inputs, deltaTime), onInterpolation: function(currentState, previousState, targetState, amount) logging: true, clientSidePrediction: true, @@ -233,7 +233,7 @@ window.GarageServerIO = (function (window, socketio) { _inputController.addInput(clientInput); if (_options.clientSidePrediction && _options.onUpdatePlayerPhysics) { - _stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, [{ input: clientInput }]); + _stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, [{ input: clientInput }], _stateController.physicsDelta); } _socket.emit('input', { input: clientInput, seq: _inputController.sequenceNumber, time: _stateController.renderTime }); }, @@ -264,7 +264,7 @@ window.GarageServerIO = (function (window, socketio) { _inputController.removeUpToSequence(playerState.seq); if (_options.clientSidePrediction && _inputController.any()) { - _stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, _inputController.inputs); + _stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, _inputController.inputs, _stateController.physicsDelta); } }, diff --git a/example/shared/core.js b/example/shared/core.js index badf04c..b34581a 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -1,20 +1,21 @@ (function(exports){ - exports.OnProcessGamePhysics = function (state, inputs) { + exports.OnProcessGamePhysics = function (state, inputs, deltaTime) { var i = 0; + if (!state.x && !state.y) { state.x = 0; state.y = 0; } for (i = 0; i < inputs.length; i ++) { if (inputs[i].input === 'left') { - state.x -= 1; + state.x -= (50 * deltaTime); } else if (inputs[i].input === 'right') { - state.x += 1; + state.x += (50 * deltaTime); } else if (inputs[i].input === 'down') { - state.y += 1; + state.y += (50 * deltaTime); } else if (inputs[i].input === 'up') { - state.y -= 1; + state.y -= (50 * deltaTime); } } return state; diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 7fa49f2..bc14934 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -16,7 +16,7 @@ options = { function GarageServer (socketio, options) { this.io = socketio; - this.physicsInterval = options.physicsInterval ? options.physicsInterval : 15; + this.physicsDelta = (options.physicsInterval ? options.physicsInterval : 15) / 1000; this.game = new garageServerGame.createGame(options); this.registerSocketEvents(options); } @@ -28,7 +28,7 @@ GarageServer.prototype.registerSocketEvents = function (options) { if (options.logging) { console.log('garageserver.io:: socket ' + socket.id + ' connection'); } - socket.emit('state', { physicsDelta: self.physicsInterval }); + socket.emit('state', { physicsDelta: self.physicsDelta }); self.onPlayerConnect(socket, options); socket.on('disconnect', function () { diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 504eb47..337eec2 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -7,7 +7,7 @@ function GarageServerGame(options) { this.startTime = new Date().getTime(); this.physicsInterval = options.physicsInterval ? options.physicsInterval : 15; this.stateInterval = options.stateInterval ? options.stateInterval : 45; - + this.physicsDelta = this.physicsInterval / 1000; this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, this.physicsInterval); this.stateIntervalId = setInterval(function () { self.updateState(options); }, this.stateInterval); } @@ -35,10 +35,11 @@ GarageServerGame.prototype.updateEntities = function (options) { }; GarageServerGame.prototype.updatePhysics = function (options) { + var self = this; this.players.forEach(function (player) { if (player.inputs.length > 0) { if (options.onUpdatePlayerPhysics) { - player.state = options.onUpdatePlayerPhysics(player.state, player.inputs); + player.state = options.onUpdatePlayerPhysics(player.state, player.inputs, self.physicsDelta); } player.sequence += player.inputs.length; player.inputs = [];