diff --git a/example/game.js b/example/game.js index becada5..12852ad 100644 --- a/example/game.js +++ b/example/game.js @@ -26,10 +26,16 @@ Game.prototype.start = function () { Game.prototype.update = function () { var players = this.server.getPlayers(), + entities = this.server.getEntities(), self = this; players.forEach(function (player) { - var newState = gamePhysics.getNewState(player.state, player.inputs, self.physicsDelta); + var newState = gamePhysics.getNewPlayerState(player.state, player.inputs, self.physicsDelta); self.server.updatePlayerState(player.id, newState); }); + + entities.forEach(function (entity) { + var newState = gamePhysics.getNewPlayerState(entity.state, self.physicsDelta); + self.server.updateEntityState(entity.id, newState); + }); }; \ No newline at end of file diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 4f1c253..eb2efaf 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -6,7 +6,7 @@ $(function () { GarageServerIO.initializeGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: true, - onUpdatePlayerPhysics: GamePhysics.getNewState, + onUpdatePlayerPhysics: GamePhysics.getNewPlayerState, onInterpolation: GamePhysics.getInterpolatedState, onWorldState: function (state) { document.getElementById('gameCanvas').style.width = state.width; diff --git a/example/shared/core.js b/example/shared/core.js index 757a11e..eb80987 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -1,6 +1,6 @@ (function(exports){ - exports.getNewState = function (state, inputs, deltaTime) { + exports.getNewPlayerState = function (state, inputs, deltaTime) { var i = 0; if (!state.x && !state.y) { @@ -16,11 +16,17 @@ state.y += (50 * deltaTime); } else if (inputs[i].input === 'up') { state.y -= (50 * deltaTime); + } else if (inputs[i].input === 'space') { + } } return state; }; + exports.getNewEntityState = function (state, deltaTime) { + + }; + exports.getInterpolatedState = function (previousState, targetState, amount) { var interpolationState = {}; interpolationState.x = (previousState.x + amount * (targetState.x - previousState.x)); diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index b6722cf..cea0680 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -16,14 +16,14 @@ GarageServerGame.prototype.start = function () { var self = this; this.startTime = new Date().getTime(); - this.stateIntervalId = setInterval(function () { self.updateState(); }, this.stateInterval); + this.stateIntervalId = setInterval(function () { self.broadcastState(); }, this.stateInterval); }; GarageServerGame.prototype.stop = function () { clearInterval(this.stateIntervalId); }; -GarageServerGame.prototype.updateState = function () { +GarageServerGame.prototype.broadcastState = function () { var currentTime = new Date().getTime() - this.startTime, state = { time: currentTime, playerStates: [], entityStates: [] }; @@ -64,9 +64,17 @@ GarageServerGame.prototype.getPlayer = function (id) { }; GarageServerGame.prototype.updatePlayerState = function (id, state) { - var currentTime = new Date().getTime() - this.startTime; + this.updateState(this.playerController, id, state); +}; - this.playerController.entities.some(function (player) { +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;