340 lines
No EOL
8.7 KiB
JavaScript
340 lines
No EOL
8.7 KiB
JavaScript
let mapWidth = 210.;
|
|
let mapHeith = 100.;
|
|
const playerSize = 50.;
|
|
const carSize = 40.;
|
|
const playerSpeed=.2;
|
|
const bulletSpeed=playerSpeed*2;
|
|
const halfSqrtTwo=0.70710678118;
|
|
const defaulthealth=10.;
|
|
|
|
class Player
|
|
{
|
|
constructor (id,x,y,name, dir,net)
|
|
{
|
|
this.name=name;
|
|
this.x=x;
|
|
this.y=y;
|
|
this.id=id;
|
|
this.visibleDir=1;
|
|
this.dir=dir;//0=standStill
|
|
//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=defaulthealth;
|
|
this.kill=0;
|
|
this.death=0;
|
|
}
|
|
|
|
takeDamage(amount,killerId,network)
|
|
{
|
|
this.health-=amount;
|
|
if(this.health<=0)
|
|
{
|
|
this.death++;
|
|
network.died(this.id,killerId);
|
|
this.health=10;
|
|
}
|
|
}
|
|
|
|
/*retrieveServerInfo(id,x,y,dir)
|
|
{
|
|
if(this.id==id)
|
|
{
|
|
this.x=x;
|
|
this.y=y;
|
|
this.dir=dir;
|
|
if(dir!=0)
|
|
this.visibleDir=dir;
|
|
}
|
|
}*/
|
|
|
|
changeDirection(newDirection){
|
|
this.dir = newDirection;
|
|
if(this.dir!=0){
|
|
this.visibleDir = newDirection;
|
|
}
|
|
}
|
|
|
|
update(squares,circles, dt)//update position
|
|
{
|
|
if(this.dir==0)
|
|
return;
|
|
|
|
let dx,dy;
|
|
switch(this.dir)
|
|
{
|
|
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;
|
|
default:
|
|
}
|
|
this.x += dx*dt;
|
|
this.y += dy*dt;
|
|
|
|
squares.forEach(square => {
|
|
if(square.collide(this))
|
|
{
|
|
this.x-=dx*dt;
|
|
this.y-=dy*dt;
|
|
this.dir=0;
|
|
return;
|
|
}
|
|
});
|
|
|
|
circles.forEach(circle => {
|
|
if(circle.collide(this))
|
|
{
|
|
this.x-=dx*dt;
|
|
this.y-=dy*dt;
|
|
this.dir=0;
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
get angle()
|
|
{
|
|
return (this.visibleDir-3)*3.1415926535/4.;
|
|
}
|
|
}
|
|
|
|
class Bullet
|
|
{
|
|
constructor (x,y,dx,dy,id)
|
|
{
|
|
this.x=x;
|
|
this.y=y;
|
|
this.dx=dx;
|
|
this.dy=dy;
|
|
this.deleted=false;
|
|
this.parentId=id;
|
|
}
|
|
|
|
update(dt)
|
|
{
|
|
if(!this.deleted)
|
|
{
|
|
this.x+=this.dx*dt*bulletSpeed;
|
|
this.y+=this.dy*dt*bulletSpeed;
|
|
}
|
|
}
|
|
|
|
checkCollisions(player,squares,circles,network)//only the client's player /!\
|
|
{
|
|
if(!this.deleted)
|
|
{
|
|
if(player!=null && player.id!=this.parentId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
|
|
{
|
|
player.takeDamage(1,this.parentId,network);
|
|
this.deleted=true;
|
|
return;
|
|
}
|
|
|
|
squares.forEach((square) => {
|
|
if(square.collide(this))
|
|
{
|
|
this.deleted=true;
|
|
return;
|
|
}
|
|
});
|
|
|
|
circles.forEach((circle) => {
|
|
if(circle.collide(this))
|
|
{
|
|
this.deleted=true;
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
{
|
|
return ((point.x-this.x)**2+(point.y-this.y)**2<=this.r**2);
|
|
}
|
|
}
|
|
|
|
class Car
|
|
{
|
|
constructor(Renderer, x, y, type, dir, angle)
|
|
{
|
|
this.type=type // 0 circule à droite vers le haut
|
|
// 1 circule vers le bas
|
|
this.x=x;
|
|
this.y=y;
|
|
this.dir=dir;
|
|
this.angle=angle;
|
|
this.tick=1;
|
|
this.Renderer=Renderer;
|
|
}
|
|
|
|
Update()
|
|
{
|
|
this.ChangeDirection()
|
|
switch (this.dir) {
|
|
case 1:
|
|
this.x=this.x+this.tick
|
|
break;
|
|
case 2:
|
|
this.y=this.y-this.tick
|
|
break;
|
|
case 3:
|
|
this.x=this.x-this.tick
|
|
break;
|
|
case 4:
|
|
this.y=this.y+this.tick
|
|
break;
|
|
}
|
|
this.Renderer.RenderCar(this.x,this.y, this.angle)
|
|
}
|
|
|
|
pseudoaleatoire() {
|
|
return Math.floor(Date.now()/1000)%10
|
|
}
|
|
|
|
ChangeDirection()
|
|
{
|
|
if(this.type == 1) {
|
|
if(this.x==1247 && this.y==36) {
|
|
this.dir=3
|
|
this.angle=-Math.PI
|
|
} else if(this.x==430 && this.y==36) {
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
} else if(this.x==430 && this.y==330) {
|
|
//tourner à droite
|
|
this.dir=1
|
|
this.angle=0
|
|
} else if(this.x==607 && this.y==330) {
|
|
if(this.pseudoaleatoire()>5) {
|
|
//vers le bas
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
}
|
|
} else if(this.x==1247 && this.y==330) {
|
|
//vers le bas
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
} else if(this.x==607 && this.y==551) {
|
|
//vers la gauche
|
|
this.dir=3
|
|
this.angle=Math.PI
|
|
} else if(this.x==190 && this.y==551) {
|
|
//vers le bas
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
} else if(this.x==190 && this.y==877) {
|
|
//vers la droite
|
|
this.dir=1
|
|
this.angle=0
|
|
} else if(this.x==907 && this.y==877) {
|
|
//vers la droite
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
} else if(this.x==1247 && this.y==550) {
|
|
if(this.pseudoaleatoire()>5) {
|
|
//vers la gauche
|
|
this.dir=3
|
|
this.angle=Math.PI
|
|
}
|
|
} else if(this.x==910 && this.y==550) {
|
|
//vers le bas
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
} else if(this.x==1247 && this.y==840) {
|
|
//vers la gauche
|
|
this.dir=3
|
|
this.angle=Math.PI
|
|
} else if(this.x==907 && this.y==840) {
|
|
//vers le bas
|
|
this.dir=4
|
|
this.angle=Math.PI/2
|
|
}
|
|
} else {
|
|
if(this.x == 1280 && this.y==75) {
|
|
this.dir=2
|
|
this.y=60
|
|
this.angle=-Math.PI/2
|
|
} else if(this.y<-40) {
|
|
this.dir=2
|
|
this.x=947
|
|
this.y=1000
|
|
} else if(this.x==947 && this.y==875 ) {
|
|
if(this.pseudoaleatoire()>7) {
|
|
//tourner à droite
|
|
this.dir=1
|
|
this.angle=0
|
|
}
|
|
} else if(this.x==947 && this.y==839 ) {
|
|
if(this.pseudoaleatoire()>4) {
|
|
//tourner à gauche
|
|
this.dir=3
|
|
this.angle=-Math.PI
|
|
}
|
|
} else if(this.x==947 && this.y==587) {
|
|
this.dir=1
|
|
this.angle=0
|
|
} else if(this.x==1280 && this.y>400) {
|
|
this.dir=2
|
|
this.angle=-Math.PI/2
|
|
} else if(this.x==228 && this.y==839) {
|
|
this.y==823
|
|
this.dir=2
|
|
this.angle=-Math.PI/2
|
|
} else if(this.x==228 && this.y==587) {
|
|
this.dir=1
|
|
this.angle=0
|
|
} else if(this.x==644 && this.y==587) {
|
|
this.dir=2
|
|
this.angle=-Math.PI/2
|
|
} else if(this.dir==2 && this.y==295) {
|
|
this.dir=3
|
|
this.angle=-Math.PI
|
|
} else if(this.x==468 && this.y==295) {
|
|
this.dir=2
|
|
this.angle=-Math.PI/2
|
|
} else if(this.x==468 && this.y==75) {
|
|
this.dir=1
|
|
this.angle=0
|
|
}
|
|
}
|
|
}
|
|
} |