2023-11-15 09:09:13 +01:00
|
|
|
let mapWidth = 210.;
|
|
|
|
let mapHeith = 100.;
|
2023-12-06 09:02:34 +01:00
|
|
|
const playerSize = 50.;
|
2023-11-29 19:03:50 +01:00
|
|
|
const playerSpeed=.2;
|
2023-12-06 09:50:34 +01:00
|
|
|
const bulletSpeed=playerSpeed*2;
|
2023-11-15 09:09:13 +01:00
|
|
|
const halfSqrtTwo=0.70710678118;
|
2023-12-06 14:02:35 +01:00
|
|
|
|
2023-11-15 09:09:13 +01:00
|
|
|
class Player
|
|
|
|
{
|
2023-11-19 18:55:21 +01:00
|
|
|
constructor (id,x,y,name, dir)
|
2023-11-15 09:09:13 +01:00
|
|
|
{
|
|
|
|
this.name=name;
|
|
|
|
this.x=x;
|
|
|
|
this.y=y;
|
|
|
|
this.id=id;
|
|
|
|
this.visibleDir=1;
|
2023-11-19 18:55:21 +01:00
|
|
|
this.dir=dir;//0=standStill
|
2023-11-15 09:09:13 +01:00
|
|
|
//1=North
|
|
|
|
//2=North-East
|
|
|
|
//3=East
|
|
|
|
//4=South-East
|
|
|
|
//5=South
|
|
|
|
//6=South-West
|
|
|
|
//7=West
|
|
|
|
//8=North-West
|
|
|
|
this.ammo=10;
|
|
|
|
this.health=10;
|
2023-12-06 14:02:35 +01:00
|
|
|
this.kill=0;
|
|
|
|
this.death=0;
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
takeDamage(amount)
|
|
|
|
{
|
|
|
|
this.health-=amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
retrieveServerInfo(id,x,y,dir)
|
|
|
|
{
|
|
|
|
if(this.id==id)
|
|
|
|
{
|
|
|
|
this.x=x;
|
|
|
|
this.y=y;
|
|
|
|
this.dir=dir;
|
|
|
|
if(dir!=0)
|
|
|
|
this.visibleDir=dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 19:56:29 +01:00
|
|
|
changeDirection(newDirection){
|
|
|
|
this.dir = newDirection;
|
|
|
|
if(this.dir!=0){
|
|
|
|
this.visibleDir = newDirection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 19:03:50 +01:00
|
|
|
update(squares,circles, dt)//update position
|
2023-11-15 09:09:13 +01:00
|
|
|
{
|
|
|
|
if(this.dir==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let dx,dy;
|
|
|
|
switch(this.dir)
|
|
|
|
{
|
2023-11-29 19:09:13 +01:00
|
|
|
case 1: dx=0.;dy=-playerSpeed;break;
|
|
|
|
case 2: dx=halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break;
|
|
|
|
case 3: dx=playerSpeed;dy=0.;break;
|
|
|
|
case 4: dx=halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break;
|
|
|
|
case 5: dx=0.;dy=playerSpeed;break;
|
|
|
|
case 6: dx=-halfSqrtTwo*playerSpeed;dy=halfSqrtTwo*playerSpeed;break;
|
|
|
|
case 7: dx=-playerSpeed;dy=0.;break;
|
|
|
|
case 8: dx=-halfSqrtTwo*playerSpeed;dy=-halfSqrtTwo*playerSpeed;break;
|
2023-11-15 09:09:13 +01:00
|
|
|
default:
|
|
|
|
}
|
2023-11-29 19:09:13 +01:00
|
|
|
this.x += dx*dt;
|
|
|
|
this.y += dy*dt;
|
2023-11-15 09:09:13 +01:00
|
|
|
|
2023-12-05 15:18:25 +01:00
|
|
|
squares.forEach(square => {
|
2023-11-15 09:09:13 +01:00
|
|
|
if(square.collide(this))
|
|
|
|
{
|
2023-12-05 15:18:25 +01:00
|
|
|
this.x-=dx*dt;
|
|
|
|
this.y-=dy*dt;
|
2023-11-15 09:09:13 +01:00
|
|
|
this.dir=0;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-05 15:18:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
circles.forEach(circle => {
|
2023-11-15 09:09:13 +01:00
|
|
|
if(circle.collide(this))
|
|
|
|
{
|
2023-12-05 15:18:25 +01:00
|
|
|
this.x-=dx*dt;
|
|
|
|
this.y-=dy*dt;
|
2023-11-15 09:09:13 +01:00
|
|
|
this.dir=0;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-05 15:18:25 +01:00
|
|
|
});
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get angle()
|
|
|
|
{
|
|
|
|
return (this.visibleDir-3)*3.1415926535/4.;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bullet
|
|
|
|
{
|
2023-12-06 10:40:08 +01:00
|
|
|
constructor (x,y,dx,dy,id)
|
2023-11-15 09:09:13 +01:00
|
|
|
{
|
|
|
|
this.x=x;
|
|
|
|
this.y=y;
|
|
|
|
this.dx=dx;
|
|
|
|
this.dy=dy;
|
|
|
|
this.deleted=false;
|
2023-12-06 10:40:08 +01:00
|
|
|
this.parentId=id;
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-06 09:50:34 +01:00
|
|
|
update(dt)
|
2023-11-15 09:09:13 +01:00
|
|
|
{
|
|
|
|
if(!this.deleted)
|
|
|
|
{
|
2023-12-06 09:50:34 +01:00
|
|
|
this.x+=this.dx*dt*bulletSpeed;
|
|
|
|
this.y+=this.dy*dt*bulletSpeed;
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkCollisions(player,squares,circles)//only the client's player /!\
|
|
|
|
{
|
|
|
|
if(!this.deleted)
|
|
|
|
{
|
2023-12-06 10:40:08 +01:00
|
|
|
if(player!=null && player.id!=this.parentId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
|
2023-11-15 09:09:13 +01:00
|
|
|
{
|
|
|
|
player.takeDamage(1);
|
|
|
|
this.deleted=true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-06 09:50:34 +01:00
|
|
|
squares.forEach((square) => {
|
2023-11-15 09:09:13 +01:00
|
|
|
if(square.collide(this))
|
|
|
|
{
|
|
|
|
this.deleted=true;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-06 09:50:34 +01:00
|
|
|
});
|
2023-11-15 09:09:13 +01:00
|
|
|
|
2023-12-06 09:50:34 +01:00
|
|
|
circles.forEach((circle) => {
|
2023-11-15 09:09:13 +01:00
|
|
|
if(circle.collide(this))
|
|
|
|
{
|
|
|
|
this.deleted=true;
|
|
|
|
return;
|
|
|
|
}
|
2023-12-06 09:50:34 +01:00
|
|
|
});
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Square
|
|
|
|
{
|
|
|
|
constructor(x,y,width,heigth)
|
|
|
|
{
|
|
|
|
this.x=x;
|
|
|
|
this.y=y;
|
|
|
|
this.w=width;
|
|
|
|
this.h=heigth;
|
|
|
|
}
|
|
|
|
|
|
|
|
collide(point)
|
|
|
|
{
|
|
|
|
return (this.x<=point.x && point.x<=this.x+this.w && this.y<=point.y && point.y<=this.y+this.h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Circle
|
|
|
|
{
|
|
|
|
constructor(x,y,radius)
|
|
|
|
{
|
|
|
|
this.x=x;
|
|
|
|
this.y=y;
|
|
|
|
this.r=radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
collide(point)
|
|
|
|
{
|
2023-11-29 17:25:40 +01:00
|
|
|
return ((point.x-this.x)**2+(point.y-this.y)**2<=this.r**2);
|
2023-11-15 09:09:13 +01:00
|
|
|
}
|
|
|
|
}
|