Progress: Client Updates

This commit is contained in:
Jeremiah Billmann
2013-06-01 15:39:23 -04:00
parent d676f72e19
commit 4b24b6a16c
3 changed files with 48 additions and 24 deletions
+44 -20
View File
@@ -1,46 +1,71 @@
window.GarageServerIO = (function (window, socketio) {
var io = socketio,
socket = null,
sequenceNumber = 0,
sequenceNumber = 1,
players = [],
inputs = [],
updates = [],
// TODO: DONE CALLBACK
connectToGarageServer = function (path, options) {
socket = io.connect(path + '/garageserver.io');
registerSocketEvents();
},
registerSocketEvents = function () {
socket.on('update', function(data) {
if(addPlayerInput) {
addPlayerInput(data);
}
updatePlayerInput(data);
});
socket.on('ping', function(data) {
});
socket.on('removePlayer', function(id) {
if(removePlayer) {
removePlayer(id);
}
removePlayer(id);
});
},
updatePlayerInput = function (data) {
var playerFound = false;
if(socket.id === data.id) {
updates.push(data);
}
else {
for(var i = 0; i < players.length; i ++) {
if(players[i].id === data.id) {
playerFound = true;
players[i].state = data.state;
players[i].seq = data.seq;
}
}
if(!playerFound) {
var player = {
id: data.id,
state: data.state,
seq: data.seq
};
players.push(player);
}
}
},
addPlayerInput = function (input) {
sequenceNumber += 1;
inputs.push(input);
sendPlayerInput(input);
},
sendPlayerInput = function (input) {
socket.emit('input', { input: input, seq: sequenceNumber });
},
removePlayer = function (id) {
for(var i = 0; i < players.length; i ++) {
if(players[i].id === id) {
@@ -49,11 +74,10 @@ window.GarageServerIO = (function (window, socketio) {
}
}
};
return {
connectToGarageServer: connectToGarageServer,
addPlayerInput: addPlayerInput,
sendPlayerInput: sendPlayerInput
addPlayerInput: addPlayerInput
};
}) (window, io);