mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-19 11:19:49 +08:00
Progress: Client Updates
This commit is contained in:
@@ -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
@@ -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 });
|
||||
|
||||
@@ -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