diff --git a/lib/server/controllers/playercontroller.js b/lib/server/controllers/playercontroller.js index 33caa61..ec563cc 100644 --- a/lib/server/controllers/playercontroller.js +++ b/lib/server/controllers/playercontroller.js @@ -45,7 +45,7 @@ PlayerController.prototype.addInput = function (id, input, sequence, time) { }); }; -PlayerController.prototype.updateState = function (id, state) { +PlayerController.prototype.setState = function (id, state) { this.entities.some(function (player) { if (player.client.id === id) { player.state = state; diff --git a/lib/server/entities/entity.js b/lib/server/entities/entity.js index 5268c8f..5f61591 100644 --- a/lib/server/entities/entity.js +++ b/lib/server/entities/entity.js @@ -4,8 +4,23 @@ function Entity () { this.state; this.sequence = 1; this.id; + this.stateHistory = []; } Entity.prototype = { + addState: function (state, executionTime) { + var minTime, spliceTo = 0; + this.stateHistory.push({ state: state, executionTime: executionTime }); + minTime = this.stateHistory[this.stateHistory - 1].executionTime - 1000; + for (var i = 0; i < this.stateHistory.length; i ++) { + if (this.stateHistory[i].executionTime >= minTime) { + spliceTo = i - 1; + break; + } + } + if (spliceTo > 0) { + this.stateHistory.splice(0, spliceTo); + } + } }; \ No newline at end of file diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index ce03fde..c7000cc 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -59,7 +59,7 @@ GarageServerGame.prototype.removePlayer = function (id) { }; GarageServerGame.prototype.setPlayerState = function (id, state) { - this.playerController.updateState(id, state); + this.playerController.setState(id, state); }; GarageServerGame.prototype.addPlayerInput = function (id, input, sequence, time) {