diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 2205aac..a6b03b4 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -11,151 +11,144 @@ options = { window.GarageServerIO = (function (window, socketio) { var io = socketio, + socket = null, + sequenceNumber = 0, + players = [], + inputs = [], + updates = [], + options = null, - socket = null, + // TODO: DONE CALLBACK + connectToGarageServer = function (path, opts) { + socket = io.connect(path + '/garageserver.io'); + options = opts; + registerSocketEvents(); + }, - sequenceNumber = 0, - - players = [], - - inputs = [], - - updates = [], - - options = null, - - // TODO: DONE CALLBACK - connectToGarageServer = function (path, opts) { - socket = io.connect(path + '/garageserver.io'); - options = opts; - registerSocketEvents(); - }, - - registerSocketEvents = function () { - socket.on('update', function(data) { - updatePlayerInput(data); - if (options.logging) { - console.log('garageserver.io:: socket update ' + data); - } - }); - socket.on('ping', function(data) { - if (options.logging) { - console.log('garageserver.io:: socket ping ' + data); - } - }); - socket.on('removePlayer', function(id) { - removePlayer(id); - if (options.logging) { - console.log('garageserver.io:: socket removePlayer ' + id); - } - }); - }, - - updatePlayerInput = function (data) { - var playerFound = false, - updateFound = false, - playerIdx = 0, - updateIdx = 0; - - if (socket.socket.sessionid === data.id) { - for (updateIdx = 0; updateIdx < updates.length; updateIdx ++) { - if (updates[updateIdx].seq === data.seq) { - updates[updateIdx].state = data.state; - updateFound = true; - break; + registerSocketEvents = function () { + socket.on('update', function(data) { + updatePlayerInput(data); + if (options.logging) { + console.log('garageserver.io:: socket update ' + data); } - } - if (!updateFound) { - updates.push(data); - } - } - else { - for (playerIdx = 0; playerIdx < players.length; playerIdx ++) { - if (players[playerIdx].id === data.id) { - playerFound = true; - for (updateIdx = 0; updateIdx < players[playerIdx].updates.length; updateIdx ++) { - if (players[playerIdx].updates[updateIdx].seq === data.seq) { - players[playerIdx].updates[updateIdx].state = data.state; - updateFound = true; - break; + }); + socket.on('ping', function(data) { + if (options.logging) { + console.log('garageserver.io:: socket ping ' + data); + } + }); + socket.on('removePlayer', function(id) { + removePlayer(id); + if (options.logging) { + console.log('garageserver.io:: socket removePlayer ' + id); + } + }); + }, + + updatePlayerInput = function (data) { + var playerFound = false, + updateFound = false, + playerIdx = 0, + updateIdx = 0; + + if (socket.socket.sessionid === data.id) { + for (updateIdx = 0; updateIdx < updates.length; updateIdx ++) { + if (updates[updateIdx].seq === data.seq) { + updates[updateIdx].state = data.state; + updateFound = true; + break; + } + } + if (!updateFound) { + updates.push(data); + } + } else { + for (playerIdx = 0; playerIdx < players.length; playerIdx ++) { + if (players[playerIdx].id === data.id) { + playerFound = true; + for (updateIdx = 0; updateIdx < players[playerIdx].updates.length; updateIdx ++) { + if (players[playerIdx].updates[updateIdx].seq === data.seq) { + players[playerIdx].updates[updateIdx].state = data.state; + updateFound = true; + break; + } } + if (!updateFound) { + players[playerIdx].updates.push( { state: data.state, seq: data.seq } ); + } + break; } - if (!updateFound) { - players[playerIdx].updates.push( { state: data.state, seq: data.seq } ); - } - break; + } + + if (!playerFound) { + var player = { + id: data.id, + updates: [] + }; + player.updates.push( { state: data.state, seq: data.seq } ); + players.push(player); } } - if (!playerFound) { - var player = { - id: data.id, - updates: [] - }; - player.updates.push( { state: data.state, seq: data.seq } ); - players.push(player); + if (options.onPlayerUpdate) { + options.onPlayerUpdate(data); } - } - - if (options.onPlayerUpdate) { - options.onPlayerUpdate(data); - } - }, + }, - removePlayer = function (id) { - for (var i = 0; i < players.length; i ++) { - if (players[i].id === id) { - players.splice(i, 1)[0]; - return; + removePlayer = function (id) { + for (var i = 0; i < players.length; i ++) { + if (players[i].id === id) { + players.splice(i, 1)[0]; + return; + } } - } - - if (options.onPlayerDisconnect) { - options.onPlayerDisconnect(id); - } - }, - - addPlayerInput = function (input) { - sequenceNumber += 1; - inputs.push(input); - sendPlayerInput(input); - }, - - sendPlayerInput = function (input) { - var currentTime = new Date().getTime(); - socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime }); - }, - - processPlayerInput = function () { - for (var i = 0; i < players.length; i ++) { - if (players[i].id !== socket.socket.sessionid) { + if (options.onPlayerDisconnect) { + options.onPlayerDisconnect(id); } - } - }, + }, - processClientInput = function () { - for (var i = 0; i < players.length; i ++) { - if (players[i].id === socket.socket.sessionid) { + addPlayerInput = function (input) { + sequenceNumber += 1; + inputs.push(input); + sendPlayerInput(input); + }, + + sendPlayerInput = function (input) { + var currentTime = new Date().getTime(); + socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime }); + }, + + processPlayerInput = function () { + for (var i = 0; i < players.length; i ++) { + if (players[i].id !== socket.socket.sessionid) { + + } + } + }, + + processClientInput = function () { + for (var i = 0; i < players.length; i ++) { + if (players[i].id === socket.socket.sessionid) { - break; + break; + } } - } - }, - - getPlayerStates = function (stateCallback) { - var maxUpdate = 0; - for (var i = 0; i < players.length; i ++) { - if (players[i].updates.length > 0) { - maxUpdate = players[i].updates.length - 1; - stateCallback(players[i].updates[maxUpdate].state); + }, + + getPlayerStates = function (stateCallback) { + var maxUpdate = 0; + for (var i = 0; i < players.length; i ++) { + if (players[i].updates.length > 0) { + maxUpdate = players[i].updates.length - 1; + stateCallback(players[i].updates[maxUpdate].state); + } } - } - if (updates.length > 0) { - maxUpdate = updates.length - 1; - stateCallback(updates[maxUpdate].state); - } - }; + if (updates.length > 0) { + maxUpdate = updates.length - 1; + stateCallback(updates[maxUpdate].state); + } + }; return { connectToGarageServer: connectToGarageServer, diff --git a/example/app.js b/example/app.js index 8d53f89..eecdf44 100644 --- a/example/app.js +++ b/example/app.js @@ -3,31 +3,31 @@ * Module dependencies. */ -var express = require('express') - , routes = require('./routes') - , user = require('./routes/user') - , http = require('http') - , path = require('path') - , io = require('socket.io') - , garageServer = require('../lib/server/garageserver.io'); +var express = require('express'), + routes = require('./routes'), + user = require('./routes/user'), + http = require('http'), + path = require('path'), + io = require('socket.io'), + garageServer = require('../lib/server/garageserver.io'); var app = express(); -app.configure(function(){ - app.set('port', process.env.PORT || 2121); - app.set('views', __dirname + '/views'); - app.set('view engine', 'jade'); - app.use(express.favicon()); - app.use(express.logger('dev')); - app.use(express.bodyParser()); - app.use(express.methodOverride()); - app.use(app.router); - app.use(express.static(path.join(__dirname, 'public'))); - app.use(express.static(path.join(__dirname, '..', 'client'))); +app.configure(function () { + app.set('port', process.env.PORT || 2121); + app.set('views', __dirname + '/views'); + app.set('view engine', 'jade'); + app.use(express.favicon()); + app.use(express.logger('dev')); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(app.router); + app.use(express.static(path.join(__dirname, 'public'))); + app.use(express.static(path.join(__dirname, '..', 'client'))); }); -app.configure('development', function(){ - app.use(express.errorHandler()); +app.configure('development', function () { + app.use(express.errorHandler()); }); app.get('/', routes.index); @@ -35,15 +35,15 @@ app.get('/users', user.list); var server = http.createServer(app); -server.listen(app.get('port'), function(){ - console.log("Express server listening on port " + app.get('port')); +server.listen(app.get('port'), function () { + console.log("Express server listening on port " + app.get('port')); }); var sockets = io.listen(server); sockets.set('log level', 0); -garageServer.createGarageServer(sockets, { +garageServer.createGarageServer(sockets,{ logging: true, onUpdatePhysics: function (state, inputs) { var i = 0; @@ -54,14 +54,11 @@ garageServer.createGarageServer(sockets, { for (i = 0; i < inputs.length; i ++) { if (inputs[i].input === 'left') { state.x -= 1; - } - else if (inputs[i].input === 'right') { + } else if (inputs[i].input === 'right') { state.x += 1; - } - else if (inputs[i].input === 'down') { + } else if (inputs[i].input === 'down') { state.y += 1; - } - else if (inputs[i].input === 'up') { + } else if (inputs[i].input === 'up') { state.y -= 1; } } diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index b3d2c79..7996a25 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -3,60 +3,64 @@ $(function () { var gameCanvas = document.getElementById('gameCanvas'), - keyboard = new THREEx.KeyboardState(), + keyboard = new THREEx.KeyboardState(), - ctxGameCanvas = gameCanvas.getContext('2d'), + ctxGameCanvas = gameCanvas.getContext('2d'), - x = 0, y =0, fps = 0, now, lastUpdate = (new Date)*1 - 1, fpsFilter = 50, + fps = 0, - requestAnimFrame = (function(){ - return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000/60); }; - })(), + now, - processClientInput = function () { - if (keyboard.pressed('left')) { - GarageServerIO.addPlayerInput('left'); - } - if (keyboard.pressed('right')) { - GarageServerIO.addPlayerInput('right'); - } - if (keyboard.pressed('down')) { - GarageServerIO.addPlayerInput('down'); - } - if (keyboard.pressed('up')) { - GarageServerIO.addPlayerInput('up'); - } - }, - - processServerInput = function () { - //GarageServerIO.processPlayerInput - - //GarageServerIO.processClientInput - }, - - draw = function () { - ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); - - GarageServerIO.getPlayerStates(function (state) { - ctxGameCanvas.fillRect(state.x, state.y, 10, 10); - }); - }, + lastUpdate = (new Date()) * 1 - 1, - update = function () { - requestAnimFrame(update); - - processClientInput(); - processServerInput(); + fpsFilter = 50, - draw(); + requestAnimFrame = (function () { + return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000/60); }; + })(), - var thisFrameFPS = 1000 / ((now=new Date) - lastUpdate); - fps += (thisFrameFPS - fps) / fpsFilter; - lastUpdate = now; + processClientInput = function () { + if (keyboard.pressed('left')) { + GarageServerIO.addPlayerInput('left'); + } + if (keyboard.pressed('right')) { + GarageServerIO.addPlayerInput('right'); + } + if (keyboard.pressed('down')) { + GarageServerIO.addPlayerInput('down'); + } + if (keyboard.pressed('up')) { + GarageServerIO.addPlayerInput('up'); + } + }, - $('#fps').html('FPS: ' + Math.round(fps)); - }; + processServerInput = function () { + //GarageServerIO.processPlayerInput + //GarageServerIO.processClientInput + }, + + draw = function () { + ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height); + + GarageServerIO.getPlayerStates(function (state) { + ctxGameCanvas.fillRect(state.x, state.y, 10, 10); + }); + }, + + update = function () { + requestAnimFrame(update); + + processClientInput(); + processServerInput(); + + draw(); + + var thisFrameFPS = 1000 / ((now = new Date()) - lastUpdate); + fps += (thisFrameFPS - fps) / fpsFilter; + lastUpdate = now; + + $('#fps').html('FPS: ' + Math.round(fps)); + }; update(); - }); \ No newline at end of file diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 2627b62..ff664c3 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -21,7 +21,7 @@ function GarageServer (socketio, options) { GarageServer.prototype.registerSocketEvents = function (options) { var self = this; - + self.io.of('/garageserver.io').on('connection', function (socket) { if (options.logging) { console.log('garageserver.io:: socket ' + socket.id + ' connection'); @@ -68,18 +68,18 @@ GarageServer.prototype.onPlayerDisconnect = function (socket, options) { GarageServer.prototype.onPlayerInput = function (socket, input, options) { this.game.addPlayerInput(socket, input); - if(options.onPlayerInput) { + if (options.onPlayerInput) { options.onPlayerInput(socket, input); } }; -GarageServer.prototype.onPing = function(socket, data, options) { +GarageServer.prototype.onPing = function (socket, data, options) { socket.emit('ping', data); if (options.onPing) { options.onPing(socket, data); } }; -exports.createGarageServer = function (io, options){ - return new GarageServer(io, options); +exports.createGarageServer = function (io, options) { + return new GarageServer(io, options); }; \ No newline at end of file diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 533e77f..6f34b0c 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -1,4 +1,4 @@ -function GarageServerGame (options) { +function GarageServerGame(options) { var self = this; this.players = []; @@ -8,7 +8,7 @@ function GarageServerGame (options) { this.playersIntervalId = setInterval(function () { self.updatePlayers(options); }, options.playersInterval ? options.playersInterval : 45); } -GarageServerGame.prototype.updatePlayers = function () { +GarageServerGame.prototype.updatePlayers = function (options) { var currentTime = new Date().getTime(); for (var i = 0; i < this.players.length; i ++) { //TODO: Retink efficiency @@ -17,7 +17,7 @@ GarageServerGame.prototype.updatePlayers = function () { } }; -GarageServerGame.prototype.updatePhysics = function () { +GarageServerGame.prototype.updatePhysics = function (options) { for (var i = 0; i < this.players.length; i ++) { if (this.players[i].inputs.length > 0) { if (this.options.onUpdatePhysics) {