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
+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);
};