This commit is contained in:
Jeremiah Billmann
2013-07-06 14:21:11 -04:00
parent a366576031
commit 8290aa31fa
6 changed files with 42 additions and 7 deletions
+17 -1
View File
@@ -1,3 +1,5 @@
var Entity = require('../entities/entity');
exports = module.exports = EntityController;
function EntityController () {
@@ -6,7 +8,21 @@ function EntityController () {
EntityController.prototype = {
add: function (id) {
var newEntity, entityFound = false;
this.entities.some(function (entity) {
if (entity.id === id) {
newEntity = entity;
entityFound = true;
return true;
}
});
if (!entityFound) {
newEntity = new Entity(id);
this.entities.push(newEntity);
}
return newEntity;
},
remove: function (id) {
+2 -1
View File
@@ -1,7 +1,7 @@
exports = module.exports = Entity;
function Entity (id) {
this.state;
this.state = {};
this.sequence = 1;
this.id = id;
this.stateHistory = [];
@@ -10,6 +10,7 @@ function Entity (id) {
Entity.prototype = {
addState: function (state, executionTime) {
var minTime, spliceTo = 0;
this.state = state;
this.stateHistory.push({ state: state, executionTime: executionTime });
minTime = this.stateHistory[this.stateHistory.length - 1].executionTime - 1000;
+2 -1
View File
@@ -14,7 +14,8 @@ options = {
onPlayerDisconnect: function (socket),
onPing: function (socket, data),
onPlayerState: function (socket, data),
onUpdatePlayerPhysics: function (id, state, inputs, deltaTime)
onUpdatePlayerPhysics: function (id, state, inputs, deltaTime),
onUpdateEntityPhysics: function (id, state, deltaTime)
}
*/
+9 -1
View File
@@ -38,7 +38,9 @@ GarageServerGame.prototype.getState = function (controller) {
};
GarageServerGame.prototype.updatePhysics = function (options) {
var self = this;
var self = this,
currentTime = new Date().getTime() - this.startTime;
this.playerController.entities.forEach(function (player) {
if (player.inputs.length > 0) {
if (options.onUpdatePlayerPhysics) {
@@ -48,6 +50,12 @@ GarageServerGame.prototype.updatePhysics = function (options) {
}
}
});
this.entityController.entities.forEach(function (entity) {
if (options.onUpdateEntityPhysics) {
entity.addState(options.onUpdateEntityPhysics(entity.id, entity.state, self.physicsDelta), currentTime);
entity.sequence += 1;
}
});
};
GarageServerGame.prototype.addPlayer = function (client) {