Progress: Server Loops

This commit is contained in:
Jeremiah Billmann
2013-05-22 21:18:52 -04:00
parent 0e043b2909
commit 909c1b4f71
2 changed files with 10 additions and 5 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ var garageServerGame = require('./garageservergame');
function GarageServer (socketio, options) {
this.io = socketio;
this.registerSocketEvents(options);
this.game = new garageServerGame.createGame(options);
this.game = new garageServerGame.createGame(socketio, options);
}
GarageServer.prototype.registerSocketEvents = function (options) {
+9 -4
View File
@@ -1,14 +1,17 @@
function GarageServerGame (options) {
function GarageServerGame (socketio, options) {
var self = this;
this.players = [];
this.io = socketio;
this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, options.physicsInterval ? options.physicsInterval : 15);
this.playersIntervalId = setInterval(function () { self.updatePlayers(options); }, options.playersInterval ? options.playersInterval : 45);
}
GarageServerGame.prototype.updatePlayers = function (options) {
var currentTime = new Date().getTime();
for(var i = 0; i < this.players.length; i ++) {
this.io.emit('playerUpdate', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime });
}
};
@@ -19,6 +22,7 @@ GarageServerGame.prototype.updatePhysics = function (options) {
this.players[i].state = options.onUpdatePhysics(this.players[i].state, this.players[i].inputs);
}
this.players[i].inputs = [];
this.players[i].sequence += this.players[i].inputs.length;
}
}
};
@@ -33,7 +37,8 @@ GarageServerGame.prototype.addPlayer = function (client) {
var player = {
client: client,
state: {},
inputs: []
inputs: [],
sequence: 0
};
this.players.push(player);
@@ -56,6 +61,6 @@ GarageServerGame.prototype.addPlayerInput = function (client, input) {
}
};
exports.createGame = function (options){
return new GarageServerGame(options);
exports.createGame = function (socketio, options){
return new GarageServerGame(socketio, options);
};