From bed0f6c6ab01d06cd0543067e4b1cf064b97149d Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Tue, 9 Jul 2013 19:28:17 -0400 Subject: [PATCH] Major refactor for #17 - prompted by #3 --- example/app.js | 15 +++--------- example/routes/user.js | 8 ------- lib/server/garageserver.io.js | 4 ++++ lib/server/garageservergame.js | 42 ++++++++++++++++++++++++++++------ 4 files changed, 42 insertions(+), 27 deletions(-) delete mode 100644 example/routes/user.js diff --git a/example/app.js b/example/app.js index 3fa2971..41a3853 100644 --- a/example/app.js +++ b/example/app.js @@ -5,12 +5,10 @@ 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'), - gamePhysics = require('./shared/core'); + Game = require('./game.js'); var app = express(); @@ -33,7 +31,6 @@ app.configure('development', function () { }); app.get('/', routes.index); -app.get('/users', user.list); var server = http.createServer(app); @@ -42,13 +39,7 @@ server.listen(app.get('port'), function () { }); var sockets = io.listen(server); - sockets.set('log level', 0); -garageServer.createGarageServer(sockets,{ - logging: true, - interpolation: true, - clientSidePrediction: true, - onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics, - onUpdateEntityPhysics: gamePhysics.onUpdateEntityPhysics -}); \ No newline at end of file +var game = new Game(sockets); +game.start(); \ No newline at end of file diff --git a/example/routes/user.js b/example/routes/user.js deleted file mode 100644 index d5b34aa..0000000 --- a/example/routes/user.js +++ /dev/null @@ -1,8 +0,0 @@ - -/* - * GET users listing. - */ - -exports.list = function(req, res){ - res.send("respond with a resource"); -}; \ No newline at end of file diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index e0589c6..3f5b68e 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -107,6 +107,10 @@ GarageServer.prototype.onPlayerState = function (socket, data, options) { } }; +GarageServer.prototype.start = function () { + +}; + 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 c8bcb9d..4a28fe9 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -4,19 +4,21 @@ var PlayerController = require('./controllers/playercontroller'), exports = module.exports = GarageServerGame; function GarageServerGame(options) { - var self = this; - this.playerController = new PlayerController(); this.entityController = new EntityController(); this.options = options; - this.startTime = new Date().getTime(); - this.physicsInterval = options.physicsInterval ? options.physicsInterval : 15; + this.startTime; this.stateInterval = options.stateInterval ? options.stateInterval : 45; - this.physicsDelta = this.physicsInterval / 1000; - this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, this.physicsInterval); - this.stateIntervalId = setInterval(function () { self.updateState(options); }, this.stateInterval); + this.stateIntervalId = 0; } +GarageServerGame.prototype.start = function () { + var self = this; + + this.startTime = new Date().getTime(); + this.stateIntervalId = setInterval(function () { self.updateState(self.options); }, this.stateInterval); +}; + GarageServerGame.prototype.updateState = function (options) { var currentTime = new Date().getTime() - this.startTime, state = { time: currentTime, playerStates: [], entityStates: [] }; @@ -58,6 +60,32 @@ GarageServerGame.prototype.updatePhysics = function (options) { }); }; +GarageServerGame.prototype.getPlayerInputs = function (id) { + var inputs; + + this.playerController.entities.forSome(function (player) { + if (player.id === id) { + inputs = player.inputs; + return true; + } + }); + + return inputs; +}; + +GarageServerGame.prototype.updatePlayerState = function (id, state) { + var currentTime = new Date().getTime() - this.startTime; + + this.playerController.entities.forSome(function (player) { + if (player.id === id) { + player.addState(state, currentTime); + player.sequence += player.inputs.length; + player.inputs = []; + return true; + } + }); +}; + GarageServerGame.prototype.addPlayer = function (client) { this.playerController.add(client); };