mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-09-11 11:53:23 +08:00
Progress: Client Updates
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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: {},
|
||||
|
||||
Reference in New Issue
Block a user