mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-01 16:50:06 +08:00
More code cleanup
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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){
|
||||
|
||||
Reference in New Issue
Block a user