From 1ae07178cb8a73c48a076e4ecd6fab69ec617470 Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Thu, 27 Jun 2013 21:15:49 -0400 Subject: [PATCH] More code cleanup --- client/garageserver.io.js | 19 ++++-------- lib/server/garageservergame.js | 55 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 56ebda9..725fed7 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -120,11 +120,6 @@ window.GarageServerIO = (function (window, socketio) { this.players.splice(i, 1); return; } - }, - forEach: function (callback) { - for (var i = 0; i < this.players.length; i ++) { - callback(this.players[i]); - } } }; @@ -222,11 +217,7 @@ window.GarageServerIO = (function (window, socketio) { }, updatePlayersState = function (data) { - var stateIdx = 0, playerState; - - for(stateIdx = 0; stateIdx < data.playerStates.length; stateIdx ++) { - playerState = data.playerStates[stateIdx]; - + data.playerStates.forEach(function (playerState) { if (_socket.socket.sessionid === playerState.id) { updatePlayerState(playerState); } else { @@ -236,7 +227,7 @@ window.GarageServerIO = (function (window, socketio) { if (_options.onPlayerUpdate) { _options.onPlayerUpdate(playerState); } - } + }); }, updatePlayerState = function (playerState) { @@ -250,7 +241,7 @@ window.GarageServerIO = (function (window, socketio) { updateOtherPlayersState = function (playerState, time) { var playerFound = false; - _playerController.forEach(function (player) { + _playerController.players.forEach(function (player) { if (player.id === playerState.id) { playerFound = true; player.processState(playerState, time); @@ -278,7 +269,7 @@ window.GarageServerIO = (function (window, socketio) { }, getPlayerStatesCurrent = function (stateCallback) { - _playerController.forEach(function (player) { + _playerController.players.forEach(function (player) { if (player.anyUpdates()) { stateCallback(player.getLatestUpdate()); } @@ -286,7 +277,7 @@ window.GarageServerIO = (function (window, socketio) { }, getPlayerStatesInterpolated = function (stateCallback) { - _playerController.forEach(function (player) { + _playerController.players.forEach(function (player) { if (player.anyUpdates()) { var latestUpdate = player.getLatestUpdate(), positions = player.getPositions(_stateController.time, _stateController.frameTime); diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 1c15133..25b620f 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -19,16 +19,15 @@ GarageServerGame.prototype.updateState = function (options) { GarageServerGame.prototype.updatePlayers = function (options) { var currentTime = new Date().getTime() - this.startTime, - state = { time: currentTime, delta: this.physicsInterval, playerStates: [] }, - i = 0; + state = { time: currentTime, delta: this.physicsInterval, playerStates: [] }; - for (i = 0; i < this.players.length; i ++) { - state.playerStates.push({ id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence }); - } + this.players.forEach(function (player) { + state.playerStates.push({ id: player.client.id, state: player.state, seq: player.sequence }); + }); - for (i = 0; i < this.players.length; i ++) { - this.players[i].client.emit('update', state); - } + this.players.forEach(function (player) { + player.client.emit('update', state); + }); }; GarageServerGame.prototype.updateEntities = function (options) { @@ -36,23 +35,23 @@ GarageServerGame.prototype.updateEntities = function (options) { }; GarageServerGame.prototype.updatePhysics = function (options) { - for (var i = 0; i < this.players.length; i ++) { - if (this.players[i].inputs.length > 0) { - if (this.options.onUpdatePlayerPhysics) { - this.players[i].state = this.options.onUpdatePlayerPhysics(this.players[i].state, this.players[i].inputs); + this.players.forEach(function (player) { + if (player.inputs.length > 0) { + if (options.onUpdatePlayerPhysics) { + player.state = options.onUpdatePlayerPhysics(player.state, player.inputs); } - this.players[i].sequence += this.players[i].inputs.length; - this.players[i].inputs = []; + player.sequence += player.inputs.length; + player.inputs = []; } - } + }); }; GarageServerGame.prototype.addPlayer = function (client) { - for (var i = 0; i < this.players.length; i ++) { - if (this.players[i].client.id === client.id) { + this.players.forEach(function (player) { + if (player.client.id === client.id) { return; } - } + }); // Set player state callback var player = { @@ -75,22 +74,22 @@ GarageServerGame.prototype.removePlayer = function (client) { }; GarageServerGame.prototype.setPlayerState = function (client, state) { - for (var i = 0; i < this.players.length; i ++) { - if (this.players[i].client.id === client.id) { - this.players[i].state = state; - this.players[i].sequence += 1; - this.players[i].inputs = []; + this.players.forEach(function (player) { + if (player.client.id === client.id) { + player.state = state; + player.sequence += 1; + player.inputs = []; } - } + }); }; GarageServerGame.prototype.addPlayerInput = function (client, input) { - for (var i = 0; i < this.players.length; i ++) { - if (this.players[i].client.id === client.id) { - this.players[i].inputs.push(input); + this.players.forEach(function (player) { + if (player.client.id === client.id) { + player.inputs.push(input); return; } - } + }); }; exports.createGame = function (options){