-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
199 lines (170 loc) · 4.47 KB
/
Copy pathserver.js
File metadata and controls
199 lines (170 loc) · 4.47 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const PORT = 3006; // ← demandé par toi
app.use(express.static("public"));
let players = {};
let foods = [];
let viruses = [];
let bots = [];
const WORLD = 5000;
// ---------------- SPAWN ----------------
function spawnFoods(n) {
for (let i = 0; i < n; i++) {
foods.push({
id: "f" + i + "_" + Date.now(),
x: (Math.random() - 0.5) * WORLD,
y: (Math.random() - 0.5) * WORLD,
radius: 8 + Math.random() * 6,
color: "orange"
});
}
}
function spawnViruses(n) {
for (let i = 0; i < n; i++) {
viruses.push({
id: "v" + i + "_" + Date.now(),
x: (Math.random() - 0.5) * WORLD,
y: (Math.random() - 0.5) * WORLD,
radius: 40,
color: "green"
});
}
}
function spawnBots(n) {
for (let i = 0; i < n; i++) {
const id = "bot_" + i + "_" + Date.now();
bots.push(id);
players[id] = {
id,
name: "Bot_" + (i + 1),
x: (Math.random() - 0.5) * WORLD,
y: (Math.random() - 0.5) * WORLD,
radius: 40,
color: "#ff4c4c",
skin: null,
isBot: true,
dx: Math.random() * 2 - 1,
dy: Math.random() * 2 - 1
};
}
}
spawnFoods(300);
spawnViruses(25);
spawnBots(10);
// ---------------- UTILS ----------------
function dist2(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return dx * dx + dy * dy;
}
// ---------------- SOCKET ----------------
io.on("connection", socket => {
console.log("Nouveau joueur:", socket.id);
socket.on("join", data => {
players[socket.id] = {
id: socket.id,
name: data.name || "Player",
x: (Math.random() - 0.5) * WORLD,
y: (Math.random() - 0.5) * WORLD,
radius: 40,
color: "#4CFF4C",
skin: data.skin || null,
isBot: false,
dx: 0,
dy: 0,
spectator: data.spectator || false
};
});
socket.on("input", data => {
const p = players[socket.id];
if (!p || p.spectator) return;
p.dx = data.dx;
p.dy = data.dy;
});
socket.on("split", () => {
const p = players[socket.id];
if (!p || p.spectator) return;
p.radius *= 0.6;
});
socket.on("eject", () => {
const p = players[socket.id];
if (!p || p.spectator) return;
foods.push({
id: "ej" + Date.now(),
x: p.x,
y: p.y,
radius: 6,
color: "yellow"
});
p.radius -= 1;
});
socket.on("disconnect", () => {
delete players[socket.id];
});
});
// ---------------- GAME LOOP ----------------
function updateBots() {
for (const id of bots) {
const b = players[id];
if (!b) continue;
if (Math.random() < 0.02) {
b.dx = Math.random() * 2 - 1;
b.dy = Math.random() * 2 - 1;
}
const speed = 3;
b.x += b.dx * speed;
b.y += b.dy * speed;
}
}
function movePlayers() {
for (const id in players) {
const p = players[id];
if (p.spectator) continue;
const speed = 5 * (40 / p.radius);
p.x += p.dx * speed;
p.y += p.dy * speed;
}
}
function eatFood() {
for (const id in players) {
const p = players[id];
if (p.spectator) continue;
for (let i = foods.length - 1; i >= 0; i--) {
const f = foods[i];
const r = p.radius + f.radius;
if (dist2(p, f) <= r * r) {
p.radius += 0.6;
foods.splice(i, 1);
}
}
}
if (foods.length < 250) spawnFoods(80);
}
function leaderboard() {
return Object.values(players)
.filter(p => !p.spectator)
.sort((a, b) => b.radius - a.radius)
.slice(0, 10)
.map(p => ({
name: p.name,
score: Math.round(p.radius)
}));
}
setInterval(() => {
updateBots();
movePlayers();
eatFood();
io.emit("state", {
players,
foods,
viruses,
leaderboard: leaderboard()
});
}, 50);
server.listen(PORT, () => {
console.log("Serveur lancé sur port " + PORT);
});