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