Skip to content
Snippets Groups Projects
Commit ea4ad61c authored by Julien Bouin's avatar Julien Bouin
Browse files

#36 Deconnexion de l'avatar et ajout d'une limite

parent ff89ff73
No related branches found
No related tags found
No related merge requests found
...@@ -121,7 +121,9 @@ function render() { ...@@ -121,7 +121,9 @@ function render() {
if (avatars[i] != undefined) { if (avatars[i] != undefined) {
context.fillStyle = colors[i - 1]; context.fillStyle = colors[i - 1];
const x = 10 + i * 100; const x = 10 + i * 100;
context.fillText(avatars[i].score, x, 50); if (avatars[i].score != undefined) {
context.fillText(avatars[i].score, x, 50);
}
imageCoeur.src = `/images/heart${i}.webp`; imageCoeur.src = `/images/heart${i}.webp`;
console.log(imageCoeur.src); console.log(imageCoeur.src);
for (let j = 0; j < avatars[i].vies; j++) { for (let j = 0; j < avatars[i].vies; j++) {
...@@ -200,6 +202,10 @@ function render() { ...@@ -200,6 +202,10 @@ function render() {
let avatars = []; let avatars = [];
socket.on('disconnectEvent', id => {
avatars[id] = {};
});
socket.on('newAvatar', data => { socket.on('newAvatar', data => {
avatars[data.id] = { x: data.x, y: data.y }; avatars[data.id] = { x: data.x, y: data.y };
}); });
......
...@@ -23,12 +23,6 @@ const fileOptions = { root: process.cwd() }; ...@@ -23,12 +23,6 @@ const fileOptions = { root: process.cwd() };
addWebpackMiddleware(app); addWebpackMiddleware(app);
const io = new IOServer(httpServer); const io = new IOServer(httpServer);
io.on('connection', socket => {
console.log(`Nouvelle connexion du client ${socket.id}`);
socket.on('disconnect', () => {
console.log(`Déconnexion du client ${socket.id}`);
});
});
app.use(express.static('client/public')); app.use(express.static('client/public'));
...@@ -52,45 +46,53 @@ let LVL2start = false; ...@@ -52,45 +46,53 @@ let LVL2start = false;
io.on('connection', socket => { io.on('connection', socket => {
cpt++; cpt++;
console.log(cpt); if (cpt <= 4) {
const avatar = new Avatar(`${socket.id}`, cpt); const avatar = new Avatar(`${socket.id}`, cpt);
console.log('Connexion du client' + socket.id); io.emit('newAvatar', { id: cpt, x: avatar.getX(), y: avatar.getY() });
io.emit('newAvatar', { id: cpt, x: avatar.getX(), y: avatar.getY() }); avatars.push(avatar);
avatars.push(avatar);
socket.on('disconnect', () => { socket.on('disconnect', () => {
console.log(`Déconnexion du client ${socket.id}`); avatars.forEach(avatar => {
}); if (avatar.nom == socket.id) {
console.log('taille du tableau : ' + avatars.length);
io.emit('disconnectEvent', avatar.id);
avatars.splice(avatars.indexOf(avatar), 1);
console.log('taille du tableau : ' + avatars.length);
}
});
console.log(`Déconnexion du client ${socket.id}`);
});
socket.on('clickEvent', clickEvent => { socket.on('clickEvent', clickEvent => {
const playerAvatar = avatars.find(avatar => avatar.nom === clickEvent.id); const playerAvatar = avatars.find(avatar => avatar.nom === clickEvent.id);
if (playerAvatar) { if (playerAvatar) {
playerAvatar.click[clickEvent.key] = clickEvent.pressed; playerAvatar.click[clickEvent.key] = clickEvent.pressed;
} else { } else {
console.log(`Aucun avatar trouvé avec le nom ${clickEvent.id}`); console.log(`Aucun avatar trouvé avec le nom ${clickEvent.id}`);
} }
}); });
socket.on('shoot', shoot => { socket.on('shoot', shoot => {
const playerAvatar = avatars.find(avatar => avatar.nom === shoot.id); const playerAvatar = avatars.find(avatar => avatar.nom === shoot.id);
if (canShoot) { if (canShoot) {
playerAvatar.tirer(); playerAvatar.tirer();
canShoot = false; canShoot = false;
setTimeout(function () { setTimeout(function () {
canShoot = true; canShoot = true;
}, 100); }, 100);
} }
}); });
socket.on('LVL2', LVL2start => { socket.on('LVL2', LVL2start => {
LVL2start = true; LVL2start = true;
//console.log(LVL2start); });
});
socket.on('canvasSize', canvasSize => { socket.on('canvasSize', canvasSize => {
console.log(canvasSize); console.log(canvasSize);
canvasSize = canvasSize; canvasSize = canvasSize;
}); });
}
}); });
let spawnIntervalLV1 = setInterval(() => { let spawnIntervalLV1 = setInterval(() => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment