Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (124 loc) · 4.61 KB
/
Copy pathserver.js
File metadata and controls
142 lines (124 loc) · 4.61 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { Server } = require("socket.io");
const express = require('express');
const { createServer } = require('node:http');
const app = express();
const server = createServer(app);
const io = new Server(server);
// Game map data
const gameMap = {
baseMap: [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1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name + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Add endpoint to get map data
app.get('/api/map', (req, res) => {
res.json(gameMap);
});
let players = new Map();
class Player {
constructor(x, y, i, username) {
this.i = i;
this.x = x;
this.y = y;
this.username = username;
}
}
const jogadores = (map) => map.size;
io.on('connection', (client) => {
const id = client.id;
players.set('player-'+id, new Player(1, 1, id, '')); //create new player with empty username
// Send complete list of players to all clients
io.emit('listaPlayers', { players: Object.fromEntries(players) });
client.on('move', (message) => {
console.log('movimento:'+message.direcao + ' player:' + message.meuPlayer);
let player = players.get('player-' + message.meuPlayer);
let newX = player.x;
let newY = player.y;
if(message.direcao == 'left'){
newX = Math.max(0, newX - 1);
}
if(message.direcao == 'right'){
newX = Math.min(15, newX + 1);
}
if(message.direcao == 'up'){
newY = Math.max(0, newY - 1);
}
if(message.direcao == 'down'){
newY = Math.min(15, newY + 1);
}
// Check for collision with objects
if (gameMap.objectMap[newY][newX] === 0) {
player.x = newX;
player.y = newY;
client.emit('update', { player: player });
client.broadcast.emit('update', { player: player });
} else {
// Send collision event to the client
client.emit('collision', {
player: player,
objectType: gameMap.objectMap[newY][newX]
});
}
})
client.on('registrar', (message) => {
console.log("registro " + id + ' tamanho:' + players.size);
let player = players.get('player-'+id);
player.username = message.username;
client.emit('registroOk', { player: player });
client.username = 'player-'+id;
});
client.on('disconnect', () => {
if (players.has('player-'+id)) {
console.log(`Player ${id} disconnected. Total players: ${players.size}`);
const removedPlayer = players.get('player-'+id);
players.delete('player-'+id);
io.emit('remove', {
player: 'player-'+id,
playerData: removedPlayer
});
io.emit('listaPlayers', { players: Object.fromEntries(players) });
} else {
console.log('Unknown client disconnected');
}
})
});
server.listen(8080, () => {
console.log('server is running on port 8080');
});