From e5fe1487ff3131c8975e29ca4fdc98c42056450d Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Fri, 5 Jul 2013 14:56:56 -0400 Subject: [PATCH] Moved interpolation & prediction options to server. --- client/garageserver.io.js | 34 ++++++++++++------------------ example/app.js | 2 ++ example/public/javascripts/game.js | 2 -- lib/server/garageserver.io.js | 12 ++++++++++- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 228b6c6..abf601d 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -11,11 +11,7 @@ options = { onUpdatePlayerPhysics: function (id, state, inputs, deltaTime), onUpdate: function (), onInterpolation: function(id, previousState, targetState, amount) - logging: true, - clientSidePrediction: true, - interpolation: true, - interpolationDelay: 100, - pingInterval: 2000 + logging: true } */ @@ -32,6 +28,9 @@ window.GarageServerIO = (function (window, socketio) { this.playerId; this.pingDelay = 100; this.interpolationDelay = 100; + this.interpolation = false; + this.pingInterval = 2000; + this.clientSidePrediction = false; } StateController.prototype = { setTime: function (serverTime) { @@ -172,13 +171,11 @@ window.GarageServerIO = (function (window, socketio) { _options = opts; _socket = _io.connect(path + '/garageserver.io'); registerSocketEvents(); - registerPinger(); }, registerSocketEvents = function () { _socket.on('connect', function () { _stateController.playerId = _socket.id; - _stateController.interpolationDelay = _options.interpolationDelay ? _options.interpolationDelay : 100; if (_options.onPlayerConnect) { _options.onPlayerConnect(); } @@ -191,6 +188,13 @@ window.GarageServerIO = (function (window, socketio) { _options.onGameState(data); } _stateController.physicsDelta = data.physicsDelta; + _stateController.interpolation = data.interpolation; + _stateController.interpolationDelay = data.interpolationDelay; + _stateController.pingInterval = data.pingInterval; + _stateController.clientSidePrediction = data.clientSidePrediction; + setInterval(function (){ + _socket.emit('ping', new Date().getTime()); + }, _stateController.pingInterval); }); _socket.on('disconnect', function () { if (_options.onPlayerDisconnect) { @@ -231,16 +235,6 @@ window.GarageServerIO = (function (window, socketio) { }); }, - registerPinger = function () { - var interval = 2000; - if (_options.pingInterval) { - interval = _options.pingInterval; - } - setInterval(function (){ - _socket.emit('ping', new Date().getTime()); - }, interval); - }, - start = function () { var self = this; _stateController.currentTime = new Date().getTime(); @@ -272,7 +266,7 @@ window.GarageServerIO = (function (window, socketio) { addPlayerInput = function (clientInput) { _inputController.add(clientInput); - if (_options.clientSidePrediction && _options.onUpdatePlayerPhysics) { + if (_stateController.clientSidePrediction && _options.onUpdatePlayerPhysics) { _stateController.state = _options.onUpdatePlayerPhysics(_stateController.playerId, _stateController.state, [{ input: clientInput }], _stateController.physicsDelta); } _socket.emit('input', [ clientInput, _inputController.sequenceNumber, _stateController.renderTime ]); @@ -303,7 +297,7 @@ window.GarageServerIO = (function (window, socketio) { _stateController.state = playerState[1]; _inputController.remove(playerState[2]); - if (_options.clientSidePrediction && _inputController.any()) { + if (_stateController.clientSidePrediction && _inputController.any()) { _stateController.state = _options.onUpdatePlayerPhysics(_stateController.playerId, _stateController.state, _inputController.inputs, _stateController.physicsDelta); } }, @@ -347,7 +341,7 @@ window.GarageServerIO = (function (window, socketio) { }, getStates = function (controller, stateCallback) { - if(_options.interpolation && _options.onInterpolation) { + if(_stateController.interpolation && _options.onInterpolation) { getEntityStatesInterpolated(controller, stateCallback); } else { diff --git a/example/app.js b/example/app.js index 70fcc60..0b97c98 100644 --- a/example/app.js +++ b/example/app.js @@ -47,5 +47,7 @@ sockets.set('log level', 0); garageServer.createGarageServer(sockets,{ logging: true, + interpolation: true, + clientSidePrediction: true, onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics }); diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 20a478c..1b82e7b 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -6,8 +6,6 @@ $(function () { GarageServerIO.initializeGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: true, - clientSidePrediction: true, - interpolation: true, onUpdatePlayerPhysics: onUpdatePlayerPhysics, onInterpolation: function (id, previousState, targetState, amount) { var interpolationState = {}; diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 5e03bf2..77a0e9c 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -5,6 +5,10 @@ options = { physicsInterval: 15, stateInterval: 45, logging: true, + clientSidePrediction: true, + interpolation: true, + interpolationDelay: 100, + pingInterval: 2000, onPlayerConnect: function (socket), onPlayerInput: function (socket, input), onPlayerDisconnect: function (socket), @@ -27,7 +31,13 @@ GarageServer.prototype.registerSocketEvents = function (options) { if (options.logging) { console.log('garageserver.io:: socket ' + socket.id + ' connection'); } - socket.emit('state', { physicsDelta: (options.physicsInterval ? options.physicsInterval : 15) / 1000 }); + socket.emit('state', { + physicsDelta: (options.physicsInterval ? options.physicsInterval : 15) / 1000, + interpolation: options.interpolation ? options.interpolation : false, + interpolationDelay: options.interpolationDelay ? options.interpolationDelay : 100, + pingInterval: options.pingInterval ? options.pingInterval : 2000, + clientSidePrediction: options.clientSidePrediction ? options.clientSidePrediction : false + }); self.onPlayerConnect(socket, options); socket.on('disconnect', function () {