Skip to content
Snippets Groups Projects
Commit 78a5b942 authored by Paul-louis Gomis's avatar Paul-louis Gomis
Browse files

implement player kill

parent 5cfc618b
No related branches found
No related tags found
No related merge requests found
......@@ -33,8 +33,7 @@ class Player {
grow() {
this.radius += 0.5;
console.log(this.radius % 30);
if (this.radius % 30 === 0 && this.imageId <= 5) {
if (this.radius % 80 === 0 && this.imageId <= 5) {
this.imageId += 1;
this.image.src = `/images/character/${this.imageFolder}/${this.imageFolder}${this.imageId}.png`;
}
......
import express from "express";
import http from "http";
import { platform } from "os";
const app = express();
const httpServer = http.createServer(app);
import { Server as IOServer } from "socket.io";
......@@ -11,7 +10,7 @@ let users = [];
let sushis = [];
for (let i = 0; i < 1500; i++) {
sushis.push({ x: Math.random(), y: Math.random() });
sushis.push({ x: Math.random(), y: Math.random() }); //commit "implement kill Player" et push
}
//Player function
......@@ -23,6 +22,7 @@ function moveTo(player, x, y, canvasWidth, canvasHeight) {
player.direction.x = directionX / distance;
player.direction.y = directionY / distance;
}
function move(player, ctx, mouseX, mouseY, canvasWidth, canvasHeight) {
if (
mouseX > canvasWidth / 2 - player.radius &&
......@@ -47,6 +47,22 @@ function move(player, ctx, mouseX, mouseY, canvasWidth, canvasHeight) {
}
}
function killPlayer(MyPlayer) {
users.forEach((player, idx) => {
const dx = player.x - MyPlayer.x;
const dy = player.y - MyPlayer.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (
MyPlayer.radius > player.radius &&
distance < MyPlayer.radius &&
player.id != MyPlayer.id
) {
users.splice(idx, 1);
player.radius += users[idx].radius;
}
});
}
const io = new IOServer(httpServer);
io.on("connection", (socket) => {
io.emit("user", socket.id);
......@@ -57,17 +73,6 @@ io.on("connection", (socket) => {
socket.emit("allUsers", { users: users });
});
/*socket.on("updateUser", (data) => {
for (let i = 0; i < users.length; i++) {
if (users[i].id === data.id) {
users[i] = data;
}
users.sort((a, b) => a.radius - b.radius);
socket.emit("allUsers", users);
}
});
*/
socket.on("userMoveTo", (data) => {
let player = data.player;
moveTo(
......@@ -90,6 +95,8 @@ io.on("connection", (socket) => {
users[i] = player;
}
}
killPlayer(data.player);
users.sort((a, b) => a.radius - b.radius);
socket.emit("allUsers", { users: users, player: player });
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment