From dd688fa3f5b235cf748a55af2ea03154f02c8da9 Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Fri, 24 May 2013 22:13:59 -0400 Subject: [PATCH] Progress: Client --- client/garageserver.io.js | 58 +++++++++++++++--------------- example/public/javascripts/game.js | 6 ++++ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 2b6c41c..9f6b482 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -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); \ No newline at end of file diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 6f4367b..8623df0 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -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 () {