mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-09-09 11:14:19 +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 = [],
|
updates = [],
|
||||||
|
|
||||||
|
options = null,
|
||||||
|
|
||||||
// TODO: DONE CALLBACK
|
// TODO: DONE CALLBACK
|
||||||
connectToGarageServer = function (path, options) {
|
connectToGarageServer = function (path, opts) {
|
||||||
socket = io.connect(path + '/garageserver.io');
|
socket = io.connect(path + '/garageserver.io');
|
||||||
registerSocketEvents(options);
|
options = opts;
|
||||||
|
registerSocketEvents();
|
||||||
},
|
},
|
||||||
|
|
||||||
registerSocketEvents = function (options) {
|
registerSocketEvents = function () {
|
||||||
socket.on('update', function(data) {
|
socket.on('update', function(data) {
|
||||||
updatePlayerInput(data, options);
|
updatePlayerInput(data);
|
||||||
if (options.logging) {
|
if (options.logging) {
|
||||||
console.log('garageserver.io:: socket update ' + data);
|
console.log('garageserver.io:: socket update ' + data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.on('ping', function(data, options) {
|
socket.on('ping', function(data) {
|
||||||
if (options.logging) {
|
if (options.logging) {
|
||||||
console.log('garageserver.io:: socket ping ' + data);
|
console.log('garageserver.io:: socket ping ' + data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.on('removePlayer', function(id, options) {
|
socket.on('removePlayer', function(id) {
|
||||||
removePlayer(id, options);
|
removePlayer(id);
|
||||||
if (options.logging) {
|
if (options.logging) {
|
||||||
console.log('garageserver.io:: socket removePlayer ' + id);
|
console.log('garageserver.io:: socket removePlayer ' + id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
updatePlayerInput = function (data, options) {
|
updatePlayerInput = function (data) {
|
||||||
var playerFound = false,
|
var playerFound = false,
|
||||||
updateFound = false,
|
updateFound = false,
|
||||||
playerIdx = 0,
|
playerIdx = 0,
|
||||||
@@ -109,7 +112,7 @@ window.GarageServerIO = (function (window, socketio) {
|
|||||||
var currentTime = new Date().getTime();
|
var currentTime = new Date().getTime();
|
||||||
socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime });
|
socket.emit('input', { input: input, seq: sequenceNumber, timestamp: currentTime });
|
||||||
},
|
},
|
||||||
|
|
||||||
processPlayerInput = function () {
|
processPlayerInput = function () {
|
||||||
for (var i = 0; i < players.length; i ++) {
|
for (var i = 0; i < players.length; i ++) {
|
||||||
if (players[i].id !== socket.socket.sessionid) {
|
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 ++) {
|
for (var i = 0; i < players.length; i ++) {
|
||||||
if (players[i].id === id) {
|
if (players[i].id === id) {
|
||||||
players.splice(i, 1)[0];
|
players.splice(i, 1)[0];
|
||||||
@@ -134,7 +146,8 @@ window.GarageServerIO = (function (window, socketio) {
|
|||||||
return {
|
return {
|
||||||
connectToGarageServer: connectToGarageServer,
|
connectToGarageServer: connectToGarageServer,
|
||||||
addPlayerInput: addPlayerInput,
|
addPlayerInput: addPlayerInput,
|
||||||
processPlayerInput: processPlayerInput
|
processPlayerInput: processPlayerInput,
|
||||||
|
processClientInput: processClientInput
|
||||||
};
|
};
|
||||||
|
|
||||||
}) (window, io);
|
}) (window, io);
|
||||||
+20
-1
@@ -43,4 +43,23 @@ var sockets = io.listen(server);
|
|||||||
|
|
||||||
sockets.set('log level', 0);
|
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 () {
|
handleInput = function () {
|
||||||
if (keyboard.pressed('left')) {
|
if (keyboard.pressed('left')) {
|
||||||
x -= 1;
|
|
||||||
GarageServerIO.addPlayerInput('left');
|
GarageServerIO.addPlayerInput('left');
|
||||||
}
|
}
|
||||||
if (keyboard.pressed('right')) {
|
if (keyboard.pressed('right')) {
|
||||||
x += 1;
|
|
||||||
GarageServerIO.addPlayerInput('right');
|
GarageServerIO.addPlayerInput('right');
|
||||||
}
|
}
|
||||||
if (keyboard.pressed('down')) {
|
if (keyboard.pressed('down')) {
|
||||||
y += 1;
|
|
||||||
GarageServerIO.addPlayerInput('down');
|
GarageServerIO.addPlayerInput('down');
|
||||||
}
|
}
|
||||||
if (keyboard.pressed('up')) {
|
if (keyboard.pressed('up')) {
|
||||||
y -= 1;
|
|
||||||
GarageServerIO.addPlayerInput('up');
|
GarageServerIO.addPlayerInput('up');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -35,12 +31,13 @@ $(function () {
|
|||||||
processInputs = function () {
|
processInputs = function () {
|
||||||
//GarageServerIO.processPlayerInput
|
//GarageServerIO.processPlayerInput
|
||||||
|
|
||||||
|
//GarageServerIO.processClientInput
|
||||||
},
|
},
|
||||||
|
|
||||||
update = function () {
|
update = function () {
|
||||||
requestAnimFrame(update);
|
requestAnimFrame(update);
|
||||||
handleInput();
|
handleInput();
|
||||||
|
processInputs();
|
||||||
ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
|
ctxGameCanvas.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
|
||||||
ctxGameCanvas.fillRect(x, y, 10, 10);
|
ctxGameCanvas.fillRect(x, y, 10, 10);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user