Progress: Interpolation

This commit is contained in:
Jeremiah Billmann
2013-06-26 20:17:55 -04:00
parent 3d8fe0790f
commit 536238e902
4 changed files with 119 additions and 21 deletions
+15
View File
@@ -9,6 +9,7 @@ options = {
onPlayerInput: function (socket, input),
onPlayerDisconnect: function (socket),
onPing: function (socket, data),
onState: function (socket, data),
onUpdatePlayerPhysics: function (state, inputs),
}
*/
@@ -48,6 +49,13 @@ GarageServer.prototype.registerSocketEvents = function (options) {
}
self.onPing(socket, data, options);
});
socket.on('state', function (data) {
if (options.logging) {
console.log('garageserver.io:: socket state ' + data);
}
self.onPing(socket, data, options);
});
});
};
@@ -80,6 +88,13 @@ GarageServer.prototype.onPing = function (socket, data, options) {
}
};
GarageServer.prototype.onState = function (socket, data, options) {
this.game.setPlayerState(socket, data);
if (options.onState) {
options.onState(socket, data);
}
};
exports.createGarageServer = function (io, options) {
return new GarageServer(io, options);
};
+15 -3
View File
@@ -5,9 +5,11 @@ function GarageServerGame(options) {
this.entities = [];
this.options = options;
this.startTime = new Date().getTime();
this.physicsInterval = options.physicsInterval ? options.physicsInterval : 15;
this.stateInterval = options.stateInterval ? options.stateInterval : 45;
this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, options.physicsInterval ? options.physicsInterval : 15);
this.stateIntervalId = setInterval(function () { self.updateState(options); }, options.stateInterval ? options.stateInterval : 45);
this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, this.physicsInterval);
this.stateIntervalId = setInterval(function () { self.updateState(options); }, this.stateInterval);
}
GarageServerGame.prototype.updateState = function (options) {
@@ -17,7 +19,7 @@ GarageServerGame.prototype.updateState = function (options) {
GarageServerGame.prototype.updatePlayers = function (options) {
var currentTime = new Date().getTime() - this.startTime,
state = { time: currentTime, playerStates: [] },
state = { time: currentTime, delta: this.physicsInterval, playerStates: [] },
i = 0;
for (i = 0; i < this.players.length; i ++) {
@@ -72,6 +74,16 @@ GarageServerGame.prototype.removePlayer = function (client) {
}
};
GarageServerGame.prototype.setPlayerState = function (client, state) {
for (var i = 0; i < this.players.length; i ++) {
if (this.players[i].client.id === client.id) {
this.players[i].state = state;
this.players[i].sequence += 1;
this.players[i].inputs = [];
}
}
};
GarageServerGame.prototype.addPlayerInput = function (client, input) {
for (var i = 0; i < this.players.length; i ++) {
if (this.players[i].client.id === client.id) {