mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-22 12:30:42 +08:00
Progress: Server Loops
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user