better death handling

This commit is contained in:
Atsuyo-INSA 2023-12-07 13:42:18 +01:00
parent e843dda5f8
commit c58f00930a
3 changed files with 14 additions and 15 deletions

View file

@ -9,7 +9,7 @@ const defaulthealth=10.;
class Player
{
constructor (id,x,y,name, dir)
constructor (id,x,y,name, dir,net)
{
this.name=name;
this.x=x;
@ -31,12 +31,18 @@ class Player
this.death=0;
}
takeDamage(amount)
takeDamage(amount,killerId,network)
{
this.health-=amount;
if(this.health<=0)
{
console.log(network);
network.died(this.id,killerId);
this.health=10;
}
}
retrieveServerInfo(id,x,y,dir)
/*retrieveServerInfo(id,x,y,dir)
{
if(this.id==id)
{
@ -46,7 +52,7 @@ class Player
if(dir!=0)
this.visibleDir=dir;
}
}
}*/
changeDirection(newDirection){
this.dir = newDirection;
@ -124,13 +130,13 @@ class Bullet
}
}
checkCollisions(player,squares,circles)//only the client's player /!\
checkCollisions(player,squares,circles,network)//only the client's player /!\
{
if(!this.deleted)
{
if(player!=null && player.id!=this.parentId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
{
player.takeDamage(1);
player.takeDamage(1,this.parentId,network);
this.deleted=true;
return;
}

View file

@ -38,7 +38,6 @@ function update()
player.y=playerToUpdate[i].y;
player.dir=playerToUpdate[i].dir;
player.visibleDir=playerToUpdate[i].visibleDir;
break;
} else {
for (let j = 0;j<players.length;j++)
{
@ -99,7 +98,7 @@ function updateBullets(dt)
for(let i = bullets.length-1;i>=0;i--)
{
bullets[i].update(dt);
bullets[i].checkCollisions(player,squares,circles);
bullets[i].checkCollisions(player,squares,circles,Net);
if(bullets[i].deleted)
{
Renderer.remBullet(bullets[i]);
@ -137,11 +136,6 @@ function game() {
remPlayers();
addBullets();
player.update(squares, circles, dt);
if(player.health <= 0)
{
Net.died(player.id);
player.health=10;
}
for (var i = players.length - 1; i >= 0; i--) {
players[i].update(squares, circles, dt);
}

View file

@ -38,8 +38,7 @@ class Network{
this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id));
break;
case "died":
console.log(data.data);
console.log("someone died");
console.log("player ",data.data.id," died");
default:
break;
}