Progress: client loops

This commit is contained in:
Jeremiah Billmann
2013-06-08 15:49:50 -04:00
parent f8614ec902
commit a46831d6ae
2 changed files with 29 additions and 11 deletions
+24 -9
View File
@@ -102,6 +102,19 @@ window.GarageServerIO = (function (window, socketio) {
}
},
removePlayer = function (id) {
for (var i = 0; i < players.length; i ++) {
if (players[i].id === id) {
players.splice(i, 1)[0];
return;
}
}
if (options.onPlayerDisconnect) {
options.onPlayerDisconnect(id);
}
},
addPlayerInput = function (input) {
sequenceNumber += 1;
inputs.push(input);
@@ -129,17 +142,18 @@ window.GarageServerIO = (function (window, socketio) {
}
}
},
removePlayer = function (id) {
getPlayerStates = function (stateCallback) {
var maxUpdate = 0;
for (var i = 0; i < players.length; i ++) {
if (players[i].id === id) {
players.splice(i, 1)[0];
return;
if (players[i].updates.length > 0) {
maxUpdate = players[i].updates.length - 1;
stateCallback(players[i].updates[maxUpdate].state);
}
}
if (options.onPlayerDisconnect) {
options.onPlayerDisconnect(id);
if (updates.length > 0) {
maxUpdate = updates.length - 1;
stateCallback(updates[maxUpdate].state);
}
};
@@ -147,7 +161,8 @@ window.GarageServerIO = (function (window, socketio) {
connectToGarageServer: connectToGarageServer,
addPlayerInput: addPlayerInput,
processPlayerInput: processPlayerInput,
processClientInput: processClientInput
processClientInput: processClientInput,
getPlayerStates: getPlayerStates
};
}) (window, io);
+5 -2
View File
@@ -1,5 +1,5 @@
$(function () {
GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: true });
GarageServerIO.connectToGarageServer('http://garageserver_io.jbillmann.c9.io', { logging: false });
var gameCanvas = document.getElementById('gameCanvas'),
@@ -36,7 +36,10 @@ $(function () {
draw = function () {
ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
ctxGameCanvas.fillRect(x, y, 10, 10);
GarageServerIO.getPlayerStates(function (state) {
ctxGameCanvas.fillRect(state.x, state.y, 10, 10);
});
},
update = function () {