mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-06-27 16:10:34 +08:00
Broke play, adding input processing from server
This commit is contained in:
+24
-11
@@ -22,33 +22,36 @@ window.GarageServerIO = (function (window, socketio) {
|
||||
|
||||
updates = [],
|
||||
|
||||
options = null,
|
||||
|
||||
// TODO: DONE CALLBACK
|
||||
connectToGarageServer = function (path, options) {
|
||||
connectToGarageServer = function (path, opts) {
|
||||
socket = io.connect(path + '/garageserver.io');
|
||||
registerSocketEvents(options);
|
||||
options = opts;
|
||||
registerSocketEvents();
|
||||
},
|
||||
|
||||
registerSocketEvents = function (options) {
|
||||
registerSocketEvents = function () {
|
||||
socket.on('update', function(data) {
|
||||
updatePlayerInput(data, options);
|
||||
updatePlayerInput(data);
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket update ' + data);
|
||||
}
|
||||
});
|
||||
socket.on('ping', function(data, options) {
|
||||
socket.on('ping', function(data) {
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket ping ' + data);
|
||||
}
|
||||
});
|
||||
socket.on('removePlayer', function(id, options) {
|
||||
removePlayer(id, options);
|
||||
socket.on('removePlayer', function(id) {
|
||||
removePlayer(id);
|
||||
if (options.logging) {
|
||||
console.log('garageserver.io:: socket removePlayer ' + id);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
updatePlayerInput = function (data, options) {
|
||||
updatePlayerInput = function (data) {
|
||||
var playerFound = false,
|
||||
updateFound = false,
|
||||
playerIdx = 0,
|
||||
@@ -109,7 +112,7 @@ window.GarageServerIO = (function (window, socketio) {
|
||||
var currentTime = new Date().getTime();
|
||||
socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime });
|
||||
},
|
||||
|
||||
|
||||
processPlayerInput = function () {
|
||||
for (var i = 0; i < players.length; i ++) {
|
||||
if (players[i].id !== socket.socket.sessionid) {
|
||||
@@ -118,7 +121,16 @@ window.GarageServerIO = (function (window, socketio) {
|
||||
}
|
||||
},
|
||||
|
||||
removePlayer = function (id, options) {
|
||||
processClientInput = function () {
|
||||
for (var i = 0; i < players.length; i ++) {
|
||||
if (players[i].id === socket.socket.sessionid) {
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
removePlayer = function (id) {
|
||||
for (var i = 0; i < players.length; i ++) {
|
||||
if (players[i].id === id) {
|
||||
players.splice(i, 1)[0];
|
||||
@@ -134,7 +146,8 @@ window.GarageServerIO = (function (window, socketio) {
|
||||
return {
|
||||
connectToGarageServer: connectToGarageServer,
|
||||
addPlayerInput: addPlayerInput,
|
||||
processPlayerInput: processPlayerInput
|
||||
processPlayerInput: processPlayerInput,
|
||||
processClientInput: processClientInput
|
||||
};
|
||||
|
||||
}) (window, io);
|
||||
+20
-1
@@ -43,4 +43,23 @@ var sockets = io.listen(server);
|
||||
|
||||
sockets.set('log level', 0);
|
||||
|
||||
garageServer.createGarageServer(sockets, { logging: true });
|
||||
garageServer.createGarageServer(sockets, {
|
||||
logging: true,
|
||||
onUpdatePhysics: function (state, inputs) {
|
||||
var i = 0;
|
||||
for (i = 0; i < inputs.length; i ++) {
|
||||
if (inputs[i] === 'left') {
|
||||
state.x -= 1;
|
||||
}
|
||||
else if (inputs[i] === 'right') {
|
||||
state.x += 1;
|
||||
}
|
||||
else if (inputs[i] === 'down') {
|
||||
state.y += 1;
|
||||
}
|
||||
else if (inputs[i] === 'up') {
|
||||
state.y -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -15,19 +15,15 @@ $(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');
|
||||
}
|
||||
},
|
||||
@@ -35,12 +31,13 @@ $(function () {
|
||||
processInputs = function () {
|
||||
//GarageServerIO.processPlayerInput
|
||||
|
||||
|
||||
//GarageServerIO.processClientInput
|
||||
},
|
||||
|
||||
update = function () {
|
||||
requestAnimFrame(update);
|
||||
handleInput();
|
||||
processInputs();
|
||||
ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
|
||||
ctxGameCanvas.fillRect(x, y, 10, 10);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user