mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-08-12 11:40:29 +08:00
Code cleanup and option comments
This commit is contained in:
@@ -2,15 +2,14 @@ var garageServerGame = require('./garageservergame');
|
||||
|
||||
/*
|
||||
options = {
|
||||
onUpdatePhysics(state, input),
|
||||
onUpdatePhysics: function (state, input),
|
||||
physicsInterval: 15,
|
||||
playersInterval: 45,
|
||||
logging: true,
|
||||
onPlayerConnect(socket),
|
||||
onPlayerDisconnect(socket),
|
||||
onPlayerInput(socket, input),
|
||||
onPlayerDisconnect(socket),
|
||||
onPing(socket, data),
|
||||
onPlayerConnect: function (socket),
|
||||
onPlayerInput: function (socket, input),
|
||||
onPlayerDisconnect: function (socket),
|
||||
onPing: function (socket, data),
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -26,27 +25,27 @@ GarageServer.prototype.registerSocketEvents = function (options) {
|
||||
self.io.of('/garageserver.io').on('connection', function (socket) {
|
||||
self.game = new garageServerGame.createGame(socket, options);
|
||||
|
||||
if(options.logging) {
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket ' + socket.id + ' connection');
|
||||
}
|
||||
self.onPlayerConnect(socket, options);
|
||||
|
||||
socket.on('disconnect', function () {
|
||||
if(options.logging) {
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket ' + socket.id + ' disconnect');
|
||||
}
|
||||
self.onPlayerDisconnect(socket, options);
|
||||
});
|
||||
|
||||
socket.on('input', function (data) {
|
||||
if(options.logging) {
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket input ' + socket.id + ' ' + data.input + ' ' + data.seq);
|
||||
}
|
||||
self.onPlayerInput(socket, data, options);
|
||||
});
|
||||
|
||||
socket.on('ping', function (data) {
|
||||
if(options.logging) {
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket ping');
|
||||
}
|
||||
self.onPing(socket, data, options);
|
||||
@@ -56,7 +55,7 @@ GarageServer.prototype.registerSocketEvents = function (options) {
|
||||
|
||||
GarageServer.prototype.onPlayerConnect = function (socket, options) {
|
||||
this.game.addPlayer(socket);
|
||||
if(options.onPlayerConnect) {
|
||||
if (options.onPlayerConnect) {
|
||||
options.onPlayerConnect(socket);
|
||||
}
|
||||
};
|
||||
@@ -64,7 +63,7 @@ GarageServer.prototype.onPlayerConnect = function (socket, options) {
|
||||
GarageServer.prototype.onPlayerDisconnect = function (socket, options) {
|
||||
this.game.removePlayer(socket);
|
||||
socket.broadcast.emit('removePlayer', socket.id);
|
||||
if(options.onPlayerDisconnect) {
|
||||
if (options.onPlayerDisconnect) {
|
||||
options.onPlayerDisconnect(socket);
|
||||
}
|
||||
};
|
||||
@@ -78,7 +77,7 @@ GarageServer.prototype.onPlayerInput = function (socket, input, options) {
|
||||
|
||||
GarageServer.prototype.onPing = function(socket, data, options) {
|
||||
socket.emit('ping', data);
|
||||
if(options.onPing) {
|
||||
if (options.onPing) {
|
||||
options.onPing(socket, data);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,16 +11,16 @@ function GarageServerGame (socket, options) {
|
||||
|
||||
GarageServerGame.prototype.updatePlayers = function () {
|
||||
var currentTime = new Date().getTime();
|
||||
for(var i = 0; i < this.players.length; i ++) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
//TODO: Retink efficiency
|
||||
this.socket.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime });
|
||||
}
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePhysics = function () {
|
||||
for(var i = 0; i < this.players.length; i ++) {
|
||||
if(this.players[i].inputs.length > 0) {
|
||||
if(this.options.onUpdatePhysics) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].inputs.length > 0) {
|
||||
if (this.options.onUpdatePhysics) {
|
||||
this.players[i].state = this.options.onUpdatePhysics(this.players[i].state, this.players[i].inputs);
|
||||
}
|
||||
this.players[i].inputs = [];
|
||||
@@ -30,8 +30,8 @@ GarageServerGame.prototype.updatePhysics = function () {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.addPlayer = function (client) {
|
||||
for(var i = 0; i < this.players.length; i ++) {
|
||||
if(this.players[i].client.id === client.id) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].client.id === client.id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,8 @@ GarageServerGame.prototype.addPlayer = function (client) {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.removePlayer = function (client) {
|
||||
for(var i = 0; i < this.players.length; i ++) {
|
||||
if(this.players[i].client.id === client.id) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].client.id === client.id) {
|
||||
this.players.splice(i, 1)[0];
|
||||
return;
|
||||
}
|
||||
@@ -56,8 +56,8 @@ GarageServerGame.prototype.removePlayer = function (client) {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.addPlayerInput = function (client, input) {
|
||||
for(var i = 0; i < this.players.length; i ++) {
|
||||
if(this.players[i].client.id === client.id) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].client.id === client.id) {
|
||||
this.players[i].inputs.push(input);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user