-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (48 loc) · 1.11 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const http = require('http');
const path = require('path');
const socketIO = require('socket.io');
const app = express();
const server = http.Server(app);
const io = socketIO(server);
app.set('port', 3000);
app.use('/static', express.static(__dirname + '/static'));
app.get('/', (request, response) => {
response.sendFile(path.join(__dirname, 'index.html'));
});
server.listen(3000, () => {
console.log('Starting server on port 3000');
});
const players = {};
// add the websocket connector
io.on('connection', (socket) => {
// disconnected players
socket.on('disconnect', () => {
delete players[socket.id];
});
socket.on('new player', () => {
players[socket.id] = {
x: 300,
y: 300
};
});
socket.on('movement', ({left, right, up, down}) => {
const player = players[socket.id] || {};
if (left) {
player.x -= 5;
}
if (up) {
player.y -= 5;
}
if (right) {
player.x += 5;
}
if (down) {
player.y += 5;
}
})
});
// 60 tps
setInterval(() => {
io.sockets.emit('state', players);
}, 1000 / 60);