mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-09-09 11:14:19 +08:00
Progress: Adding delta time and fixed timestep
This commit is contained in:
@@ -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