second, not last

This commit is contained in:
Victor Lasserre 2023-12-11 15:13:06 +01:00
parent 1ef94e22df
commit 68539426a7

View file

@ -13,8 +13,7 @@ class Player
this.name=name; this.name=name;
this.x=x; this.x=x;
this.y=y; this.y=y;
this.z=z; this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena
//this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena
this.id=id; this.id=id;
this.visibleDir=1; this.visibleDir=1;
this.dir=dir;//0=stand Still this.dir=dir;//0=stand Still
@ -73,7 +72,7 @@ class Player
this.y += dy*dt; this.y += dy*dt;
squares.forEach(square => { squares.forEach(square => {
if(square.collide(this.x,this.y)) if(square.collide(this.x,this.y,this.z))
{ {
this.x-=dx*dt; this.x-=dx*dt;
this.y-=dy*dt; this.y-=dy*dt;
@ -83,7 +82,7 @@ class Player
}); });
circles.forEach(circle => { circles.forEach(circle => {
if(circle.collide(this.x,this.y)) if(circle.collide(this.x,this.y,this.z))
{ {
this.x-=dx*dt; this.x-=dx*dt;
this.y-=dy*dt; this.y-=dy*dt;
@ -101,11 +100,13 @@ class Player
class Bullet class Bullet
{ {
constructor (x,y,dx,dy,id) constructor (x,y,z,dx,dy,id)
{ {
bulletSound.play(); if(z==player.z)
bulletSound.play();
this.x=x; this.x=x;
this.y=y; this.y=y;
this.z=z;
this.dx=dx; this.dx=dx;
this.dy=dy; this.dy=dy;
this.deleted=false; this.deleted=false;
@ -126,21 +127,21 @@ class Bullet
if(this.deleted) if(this.deleted)
return; return;
if(player!=null && player.id!=this.shooterId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2) if(player!=null && player.z==this.z && player.id!=this.shooterId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
{ {
player.takeDamage(1,this.shooterId); player.takeDamage(1,this.shooterId);
this.deleted=true; this.deleted=true;
return; return;
} }
squares.forEach((square) => { squares.forEach((square) => {
if(square.collide(this.x,this.y)) if(square.collide(this.x,this.y,this.z))
{ {
this.deleted=true; this.deleted=true;
return; return;
} }
}); });
circles.forEach((circle) => { circles.forEach((circle) => {
if(circle.collide(this.x,this.y)) if(circle.collide(this.x,this.y,this.z))
{ {
this.deleted=true; this.deleted=true;
return; return;
@ -152,32 +153,34 @@ class Bullet
class Square class Square
{ {
constructor(x,y,width,heigth) constructor(x,y,z,width,heigth)
{ {
this.x=x; this.x=x;
this.y=y; this.y=y;
this.z=z;
this.w=width; this.w=width;
this.h=heigth; this.h=heigth;
} }
collide(x,y) collide(x,y,z)
{ {
return this.x<=x && x<=this.x+this.w && this.y<=y && y<=this.y+this.h; return this.z==z && this.x<=x && x<=this.x+this.w && this.y<=y && y<=this.y+this.h;
} }
} }
class Circle class Circle
{ {
constructor(x,y,radius) constructor(x,y,z,radius)
{ {
this.x=x; this.x=x;
this.y=y; this.y=y;
this.z=z;
this.r=radius; this.r=radius;
} }
collide(x,y) collide(x,y,z)
{ {
return (x-this.x)**2+(y-this.y)**2 <= this.r**2; return this.z==z && (x-this.x)**2+(y-this.y)**2 <= this.r**2;
} }
} }
@ -187,7 +190,7 @@ class Car
{ {
this.type=type; // 0 circule vers le haut this.type=type; // 0 circule vers le haut
// 1 circule vers le bas // 1 circule vers le bas
this.z=0;
if(this.type == 1) { //vers le bas if(this.type == 1) { //vers le bas
this.x=1247; this.x=1247;
this.y=-40; this.y=-40;
@ -207,8 +210,11 @@ class Car
this.tick=0; this.tick=0;
} }
collide(x,y) collide(x,y,z)
{ {
if(this.z!=z) {
return false;
}
let cx=this.x-carSize/2; let cx=this.x-carSize/2;
let cy=this.y-carSize/2; let cy=this.y-carSize/2;
let collide = (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize); let collide = (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize);
@ -369,41 +375,42 @@ class Car
} }
class PNJ{ class PNJ{
constructor(x, y){ constructor(x, y, z){
this.x=x; this.x=x;
this.y=y; this.y=y;
this.z=z;
this.dir=1; this.dir=1;
} }
checkCollisions(x,y){ checkCollisions(){
let colliding = false;
squares.forEach((square) => { squares.forEach((square) => {
if(square.collide(x,y)) if(square.collide(this.x,this.y,this.z))
{ {
colliding = true; return true;
return;
} }
}); });
circles.forEach((circle) => { circles.forEach((circle) => {
if(circle.collide(x,y)) if(circle.collide(this.x,this.y,this.z))
{ {
colliding=true; return true;
return;
} }
}); });
return colliding; return false;
} }
changeDirection(){ changeDirection(){
let newDir = this.dir; let newDir = Math.floor(Math.random()*8);
while(newDir == this.dir){ if(newDir == this.dir){
newDir = Math.floor(Math.random()*8); newDir = (this.dir+3)%4+1;
} }
this.dir = newDir; this.dir = newDir;
} }
update(dt) update(dt)
{ {
if(this.z!=player.z){
return;
}
let dx,dy; let dx,dy;
switch(this.dir) switch(this.dir)
{ {
@ -417,10 +424,11 @@ class PNJ{
case 8: dx=-halfSqrtTwo*PNJSpeed;dy=-halfSqrtTwo*PNJSpeed;break; case 8: dx=-halfSqrtTwo*PNJSpeed;dy=-halfSqrtTwo*PNJSpeed;break;
default: dx=0;dy=0;break; default: dx=0;dy=0;break;
} }
if(!this.checkCollisions(this.x + dx*dt, this.y + dy*dt)){ this.x+=dx*dt;
this.x += dx*dt; this.y+=dy*dt;
this.y += dy*dt; if(this.checkCollisions()){
}else{ this.x -= dx*dt;
this.y -= dy*dt;
this.changeDirection(); this.changeDirection();
} }