Major refactor for #17 - prompted by #3

This commit is contained in:
Jeremiah Billmann
2013-07-09 19:28:17 -04:00
parent 0380e63899
commit bed0f6c6ab
4 changed files with 42 additions and 27 deletions
+3 -12
View File
@@ -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
});
var game = new Game(sockets);
game.start();
-8
View File
@@ -1,8 +0,0 @@
/*
* GET users listing.
*/
exports.list = function(req, res){
res.send("respond with a resource");
};
+4
View File
@@ -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);
};
+35 -7
View File
@@ -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);
};