-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (81 loc) · 2.57 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express')
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
app.use(express.static('public'));
players={}
io.on('connect', (socket) => {
socket.id=Math.random()
players[socket.id]={}
players[socket.id].socket=socket.id
players[socket.id].x=0
players[socket.id].z=0
players[socket.id].walkSpeed=0.2
socket.on('angle',(e)=>{
players[socket.id].xa=e.x
players[socket.id].ya=e.y
})
socket.on('move',(e)=>{
//ZINGULAR:
if(e.key==65){//Left.
players[socket.id].k1=true
}else{
players[socket.id].k1=false
}
if(e.key==87){//Up.
players[socket.id].k2=true
}else{
players[socket.id].k2=false
}
if(e.key==68){//Right.
players[socket.id].k3=true
}else{
players[socket.id].k3=false
}
if(e.key==83){//Down.
players[socket.id].k4=true
}else{
players[socket.id].k4=false
}
if(e.key==81){//Q (Stop).
players[socket.id].walkSpeed=0
}else{
players[socket.id].walkSpeed=0.2
}
})
setInterval(()=>{
if(players[socket.id].k1==true){
players[socket.id].x+=players[socket.id].walkSpeed*Math.cos(players[socket.id].xa)
players[socket.id].z-=players[socket.id].walkSpeed*Math.sin(players[socket.id].xa)
}
if(players[socket.id].k2==true){
players[socket.id].x+=players[socket.id].walkSpeed*Math.sin(players[socket.id].xa)
players[socket.id].z+=players[socket.id].walkSpeed*Math.cos(players[socket.id].xa)
}
if(players[socket.id].k3==true){
players[socket.id].x-=players[socket.id].walkSpeed*Math.cos(players[socket.id].xa)
players[socket.id].z+=players[socket.id].walkSpeed*Math.sin(players[socket.id].xa)
}
if(players[socket.id].k4==true){
players[socket.id].x-=players[socket.id].walkSpeed*Math.sin(players[socket.id].xa)
players[socket.id].z-=players[socket.id].walkSpeed*Math.cos(players[socket.id].xa)
}
socket.emit('pack',{x:players[socket.id].x,z:players[socket.id].z})
socket.emit('all',{all:players})
},1000/60)
});
server.listen(8000, () => {
const dns = require('node:dns');
const os = require('node:os');
const options = { family: 4 };
dns.lookup(os.hostname(), options, (err, addr) => {
if (err) {
console.error(err);
} else {
console.log(`servidor escuchando en la dirección IP ${addr}:8000`);
}
});
});