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 class Player
{ {
constructor (id,x,y,name, dir) constructor (id,x,y,name, dir,net)
{ {
this.name=name; this.name=name;
this.x=x; this.x=x;
@ -31,12 +31,18 @@ class Player
this.death=0; this.death=0;
} }
takeDamage(amount) takeDamage(amount,killerId,network)
{ {
this.health-=amount; 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) if(this.id==id)
{ {
@ -46,7 +52,7 @@ class Player
if(dir!=0) if(dir!=0)
this.visibleDir=dir; this.visibleDir=dir;
} }
} }*/
changeDirection(newDirection){ changeDirection(newDirection){
this.dir = 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(!this.deleted)
{ {
if(player!=null && player.id!=this.parentId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2) 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; this.deleted=true;
return; return;
} }

View file

@ -38,7 +38,6 @@ function update()
player.y=playerToUpdate[i].y; player.y=playerToUpdate[i].y;
player.dir=playerToUpdate[i].dir; player.dir=playerToUpdate[i].dir;
player.visibleDir=playerToUpdate[i].visibleDir; player.visibleDir=playerToUpdate[i].visibleDir;
break;
} else { } else {
for (let j = 0;j<players.length;j++) for (let j = 0;j<players.length;j++)
{ {
@ -99,7 +98,7 @@ function updateBullets(dt)
for(let i = bullets.length-1;i>=0;i--) for(let i = bullets.length-1;i>=0;i--)
{ {
bullets[i].update(dt); bullets[i].update(dt);
bullets[i].checkCollisions(player,squares,circles); bullets[i].checkCollisions(player,squares,circles,Net);
if(bullets[i].deleted) if(bullets[i].deleted)
{ {
Renderer.remBullet(bullets[i]); Renderer.remBullet(bullets[i]);
@ -137,11 +136,6 @@ function game() {
remPlayers(); remPlayers();
addBullets(); addBullets();
player.update(squares, circles, dt); 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--) { for (var i = players.length - 1; i >= 0; i--) {
players[i].update(squares, circles, dt); 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)); this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id));
break; break;
case "died": case "died":
console.log(data.data); console.log("player ",data.data.id," died");
console.log("someone died");
default: default:
break; break;
} }