diff --git a/lib/server/entities/entity.js b/lib/server/entities/entity.js index dd3045f..cdcd4c6 100644 --- a/lib/server/entities/entity.js +++ b/lib/server/entities/entity.js @@ -9,6 +9,10 @@ function Entity (id) { Entity.prototype = { addState: function (state, executionTime) { + this.addHistory(state, executionTime); + this.sequence += 1; + }, + addHistory: function (state, executionTime) { var minTime, spliceTo = 0; this.state = state; this.stateHistory.push({ state: state, executionTime: executionTime }); diff --git a/lib/server/entities/player.js b/lib/server/entities/player.js index 9b1eed7..2d795ea 100644 --- a/lib/server/entities/player.js +++ b/lib/server/entities/player.js @@ -8,4 +8,10 @@ function Player (client) { this.inputs = []; } -Player.prototype = Object.create(entity.prototype); \ No newline at end of file +Player.prototype = Object.create(entity.prototype); + +Player.prototype.addState = function (state, executionTime) { + this.addHistory(state, executionTime); + this.sequence += this.inputs.length; + this.inputs = []; +}; \ No newline at end of file diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 17f9925..63fc6cc 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -64,21 +64,22 @@ GarageServerGame.prototype.getPlayer = function (id) { }; GarageServerGame.prototype.updatePlayerState = function (id, state) { - this.updateState(this.playerController, id, state); + var currentTime = new Date().getTime() - this.startTime; + + this.playerController.entities.some(function (player) { + if (player.id === id) { + player.addState(state, currentTime); + return true; + } + }); }; GarageServerGame.prototype.updateEntityState = function (id, state) { - this.updateState(this.entityController, id, state); -}; - -GarageServerGame.prototype.updateState = function (controller, id, state) { var currentTime = new Date().getTime() - this.startTime; - controller.entities.some(function (player) { - if (player.id === id) { - player.addState(state, currentTime); - player.sequence += player.inputs.length; - player.inputs = []; + this.entityController.entities.some(function (entity) { + if (entity.id === id) { + entity.addState(state, currentTime); return true; } });