mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-25 13:00:24 +08:00
Progress: Adding delta time and fixed timestep
This commit is contained in:
@@ -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);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user