Id in bullets

This commit is contained in:
Atsuyo-INSA 2023-12-06 10:40:08 +01:00
parent bd84fd7247
commit 86d3bb86d8
3 changed files with 8 additions and 12 deletions

View file

@ -103,20 +103,16 @@ class Player
} }
} }
function dist(A,B)
{
return Math.sqrt((A.x-B.x)**2+(A.y-B.y)**2);
}
class Bullet class Bullet
{ {
constructor (x,y,dx,dy) constructor (x,y,dx,dy,id)
{ {
this.x=x; this.x=x;
this.y=y; this.y=y;
this.dx=dx; this.dx=dx;
this.dy=dy; this.dy=dy;
this.deleted=false; this.deleted=false;
this.parentId=id;
} }
update(dt) update(dt)
@ -132,7 +128,7 @@ class Bullet
{ {
if(!this.deleted) if(!this.deleted)
{ {
if(player!=null && dist(player,this)<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.deleted=true; this.deleted=true;

View file

@ -22,10 +22,10 @@ class Input {
let dx = this.mouseX-this.player.x; let dx = this.mouseX-this.player.x;
let dy = this.mouseY-this.player.y; let dy = this.mouseY-this.player.y;
let norm = Math.sqrt(dx*dx+dy*dy); let norm = Math.sqrt(dx*dx+dy*dy);
let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm); let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm,this.player.id);
this.bullets.push(b); this.bullets.push(b);
this.renderer.addBullet(b); this.renderer.addBullet(b);
this.network.newBullet(this.player.x,this.player.y,dx/norm,dy/norm); this.network.newBullet(b.x,b.y,b.dx,b.dy,b.parentId);
}); });
/*window.addEventListener("keydown", function(event) { /*window.addEventListener("keydown", function(event) {

View file

@ -35,7 +35,7 @@ class Network{
this.playersToRemove.push(data.data.id); this.playersToRemove.push(data.data.id);
break; break;
case "newBullet": case "newBullet":
this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy)); this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id));
break; break;
default: default:
break; break;
@ -59,9 +59,9 @@ class Network{
})); }));
} }
newBullet(x_,y_,dx_,dy_) newBullet(x_,y_,dx_,dy_,parentId)
{ {
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x_,y: y_,dx: dx_,dy: dy_}})); this.socket.send(JSON.stringify({type:"newBullet",data:{x: x_,y: y_,dx: dx_,dy: dy_,id:parentId}}));
} }
getPlayersToAdd(){ //returns the list of new players getPlayersToAdd(){ //returns the list of new players