From 074119fb8206320652a08ee3b1c00372881526e9 Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Tue, 9 Jul 2013 20:03:38 -0400 Subject: [PATCH] Finished initial server refactor for #17 --- example/game.js | 11 +++++++++-- example/shared/core.js | 21 +++++++++++++++++++++ lib/server/garageserver.io.js | 10 +++++++++- lib/server/garageservergame.js | 17 ++++++++++------- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/example/game.js b/example/game.js index 7b884d3..8c2048d 100644 --- a/example/game.js +++ b/example/game.js @@ -7,12 +7,13 @@ function Game (sockets) { this.physicsInterval = 15; this.physicsDelta = this.physicsInterval / 1000; this.physicsIntervalId = 0; - + this.server = garageServer.createGarageServer(sockets, { logging: true, interpolation: true, clientSidePrediction: true, + smoothingFactor: this.physicsDelta * 20, onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics, onUpdateEntityPhysics: gamePhysics.onUpdateEntityPhysics }); @@ -20,10 +21,16 @@ function Game (sockets) { Game.prototype.start = function () { var self = this; - this.server.start(); this.physicsIntervalId = setInterval(function () { self.update(); }, this.physicsInterval); + this.server.start(); }; Game.prototype.update = function () { + var players = this.server.getPlayers(), + self = this; + players.forEach(function (player) { + var newState = gamePhysics.getNewPlayerState(player.state, player.inputs, self.physicsDelta); + self.server.updatePlayerState(player.id, newState); + }); }; \ No newline at end of file diff --git a/example/shared/core.js b/example/shared/core.js index 1c082e0..9b247e1 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -21,6 +21,27 @@ return state; }; + exports.getNewPlayerState = function (state, inputs, deltaTime) { + var i = 0; + + if (!state.x && !state.y) { + state.x = 0; + state.y = 0; + } + for (i = 0; i < inputs.length; i ++) { + if (inputs[i].input === 'left') { + state.x -= (50 * deltaTime); + } else if (inputs[i].input === 'right') { + state.x += (50 * deltaTime); + } else if (inputs[i].input === 'down') { + state.y += (50 * deltaTime); + } else if (inputs[i].input === 'up') { + state.y -= (50 * deltaTime); + } + } + return state; + }; + exports.onUpdateEntityPhysics = function (id, state, deltaTime) { if (!state.x && !state.y) { state.x = 0; diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 3f5b68e..8e14eee 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -108,7 +108,15 @@ GarageServer.prototype.onPlayerState = function (socket, data, options) { }; GarageServer.prototype.start = function () { - + this.game.start(); +}; + +GarageServer.prototype.getPlayers = function () { + return this.game.getPlayers(); +}; + +GarageServer.prototype.updatePlayerState = function (id, state) { + this.game.updatePlayerState(id, state); }; exports.createGarageServer = function (io, options) { diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 4a28fe9..b2190d7 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -60,23 +60,26 @@ GarageServerGame.prototype.updatePhysics = function (options) { }); }; -GarageServerGame.prototype.getPlayerInputs = function (id) { - var inputs; +GarageServerGame.prototype.getPlayers = function () { + return this.playerController.entities; +}; - this.playerController.entities.forSome(function (player) { +GarageServerGame.prototype.getPlayer = function (id) { + var playerFound; + + this.playerController.entities.some(function (player) { if (player.id === id) { - inputs = player.inputs; + playerFound = player; return true; } }); - - return inputs; + return playerFound; }; GarageServerGame.prototype.updatePlayerState = function (id, state) { var currentTime = new Date().getTime() - this.startTime; - this.playerController.entities.forSome(function (player) { + this.playerController.entities.some(function (player) { if (player.id === id) { player.addState(state, currentTime); player.sequence += player.inputs.length;