dronedemo/server.js

47 lines
1.2 KiB
JavaScript

var app = require('./app');
const http = require('./http');
const io = require('./io');
const emitState = require('./emitstate.js');
const physics = require('./physics');
var gameState = require('./gamestate');
io.on('connection', (socket) => {
console.log('A user connected');
gameState.clients[socket.id] = {
controlState: {},
controllingId: 'human1',
viewingId: 'human1',
controlDelay: 0,
viewDelay: 0
};
socket.emit('initialState', gameState);
socket.on('keyEvent', ({ key, isDown }) => {
const client = gameState.clients[socket.id];
if (client.controlDelay > 0) {
setTimeout(() => {
client.controlState[key] = isDown;
}, client.controlDelay);
} else {
client.controlState[key] = isDown;
}
});
socket.on('toggleView', () => {
const client = gameState.clients[socket.id];
client.viewingId = client.viewingId === 'human1' ? 'drone1' : 'human1';
client.controllingId = client.viewingId;
client.controlDelay = client.controllingId === 'human1' ? 0 : 250;
client.viewDelay = client.viewingId === 'human1' ? 0 : 250;
});
socket.on('disconnect', () => {
delete gameState.clients[socket.id];
});
});