Progress: Client

This commit is contained in:
Jeremiah Billmann
2013-05-24 22:13:59 -04:00
parent 94c89e2254
commit dd688fa3f5
2 changed files with 34 additions and 30 deletions
+28 -30
View File
@@ -2,62 +2,60 @@ window.GarageServerIO = (function (window, socketio) {
var io = socketio,
garageServerGame = null,
socket = null,
sequenceNumber = 0,
players = [],
inputs = [],
updates = [],
// TODO: DONE CALLBACK
connectToGarageServer = function (path, options) {
registerSocketEvents(io.connect(path + '/garageserver.io'));
socket = io.connect(path + '/garageserver.io');
registerSocketEvents();
},
registerSocketEvents = function (socket) {
registerSocketEvents = function () {
socket.on('update', function(data) {
if(garageServerGame.addPlayerInput) {
garageServerGame.addPlayerInput(data);
if(addPlayerInput) {
addPlayerInput(data);
}
});
socket.on('ping', function(data) {
});
socket.on('removePlayer', function(id) {
if(garageServerGame.removePlayer) {
garageServerGame.removePlayer(id);
if(removePlayer) {
removePlayer(id);
}
});
},
startGarageServerGame = function (options) {
garageServerGame = new GarageServerGame(options);
};
addPlayerInput = function (input) {
sequenceNumber += 1;
inputs.push({ input: input, seq: sequenceNumber });
},
function GarageServerGame (options) {
this.players = [];
this.updates = [];
}
GarageServerGame.prototype.addPlayerInput = function (input) {
this.updates.push(input);
};
GarageServerGame.prototype.sendPlayerInput = function (input) {
sendPlayerInput = function () {
};
},
GarageServerGame.prototype.removePlayer = function (id) {
for(var i = 0; i < this.players.length; i ++) {
if(this.players[i].id === id) {
this.players.splice(i, 1)[0];
removePlayer = function (id) {
for(var i = 0; i < players.length; i ++) {
if(players[i].id === id) {
players.splice(i, 1)[0];
return;
}
}
};
GarageServerGame.prototype.updatePlayers = function () {
};
return {
connectToGarageServer: connectToGarageServer,
startGarageServerGame: startGarageServerGame
addPlayerInput: addPlayerInput,
sendPlayerInput: sendPlayerInput
};
}) (window, io);
+6
View File
@@ -16,16 +16,22 @@ $(function () {
handleInput = function (){
if(keyboard.pressed('left')) {
x -= 1;
GarageServerIO.addPlayerInput('left');
}
if(keyboard.pressed('right')) {
x += 1;
GarageServerIO.addPlayerInput('right');
}
if(keyboard.pressed('down')) {
y += 1;
GarageServerIO.addPlayerInput('down');
}
if(keyboard.pressed('up')) {
y -= 1;
GarageServerIO.addPlayerInput('up');
}
GarageServerIO.sendPlayerInput();
},
update = function () {