Progress: Client Updates

This commit is contained in:
Jeremiah Billmann
2013-06-01 16:17:11 -04:00
parent 746a43c2d8
commit a6c6753815
4 changed files with 17 additions and 13 deletions
-3
View File
@@ -20,9 +20,6 @@ window.GarageServerIO = (function (window, socketio) {
registerSocketEvents = function (options) {
socket.on('update', function(data) {
if(options.logging) {
console.log('garageserver.io:: socket update ' + ' ' + data.id + ' ' + data.state + ' ' + data.seq);
}
updatePlayerInput(data);
});
socket.on('ping', function(data) {
+5 -1
View File
@@ -39,4 +39,8 @@ server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
garageServer.createGarageServer(io.listen(server), { logging: true });
var sockets = io.listen(server);
sockets.set('log level', 0);
garageServer.createGarageServer(sockets, { logging: true });
+3 -1
View File
@@ -2,14 +2,16 @@ var garageServerGame = require('./garageservergame');
function GarageServer (socketio, options) {
this.io = socketio;
this.game = null;
this.registerSocketEvents(options);
this.game = new garageServerGame.createGame(socketio, options);
}
GarageServer.prototype.registerSocketEvents = function (options) {
var self = this;
self.io.of('/garageserver.io').on('connection', function (socket) {
self.game = new garageServerGame.createGame(socket, options);
if(options.logging) {
console.log('garageserver.io:: socket ' + socket.id + ' connection');
}
+9 -8
View File
@@ -1,25 +1,26 @@
function GarageServerGame (socketio, options) {
function GarageServerGame (socket, options) {
var self = this;
this.players = [];
this.io = socketio;
this.socket = socket;
this.options = options;
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) {
GarageServerGame.prototype.updatePlayers = function () {
var currentTime = new Date().getTime();
for(var i = 0; i < this.players.length; i ++) {
this.io.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime });
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 (options) {
GarageServerGame.prototype.updatePhysics = function () {
for(var i = 0; i < this.players.length; i ++) {
if(this.players[i].inputs.length > 0) {
if(options.onUpdatePhysics) {
this.players[i].state = options.onUpdatePhysics(this.players[i].state, this.players[i].inputs);
if(this.options.onUpdatePhysics) {
this.players[i].state = this.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 +34,7 @@ GarageServerGame.prototype.addPlayer = function (client) {
return;
}
}
var player = {
client: client,
state: {},