GrandTabernacleAutoVI/public_html/js/class.js

431 lines
11 KiB
JavaScript
Raw Normal View History

2023-12-06 09:02:34 +01:00
const playerSize = 50.;
2023-12-07 10:03:55 +01:00
const carSize = 40.;
2023-11-29 19:03:50 +01:00
const playerSpeed=.2;
2023-12-09 14:53:07 +01:00
const PNJSpeed=.02;
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-08 18:29:33 +01:00
const defaulthealth=10;
2023-12-06 14:02:35 +01:00
2023-11-15 09:09:13 +01:00
class Player
{
2023-12-08 13:50:49 +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;
2023-12-08 18:02:42 +01:00
//this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena
2023-11-15 09:09:13 +01:00
this.id=id;
this.visibleDir=1;
2023-12-08 18:02:42 +01:00
this.dir=dir;//0=stand Still
//1=North
//2=North-East
//3=East
//4=South-East
//5=South
//6=South-West
//7=West
//8=North-West
2023-11-15 09:09:13 +01:00
this.ammo=10;
2023-12-08 18:29:33 +01:00
this.health=defaulthealth;
2023-12-06 14:02:35 +01:00
this.kill=0;
this.death=0;
2023-11-15 09:09:13 +01:00
}
2023-12-08 13:50:49 +01:00
takeDamage(amount,killerId)
2023-11-15 09:09:13 +01:00
{
this.health-=amount;
2023-12-07 13:42:18 +01:00
if(this.health<=0)
{
2023-12-07 14:06:11 +01:00
this.death++;
2023-12-08 18:29:33 +01:00
this.health=defaulthealth;
2023-12-09 10:58:00 +01:00
net.died(this.id,killerId);
2023-12-07 13:42:18 +01:00
}
2023-11-15 09:09:13 +01:00
}
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-12-08 18:02:42 +01:00
if(square.collide(this.x,this.y))
2023-11-15 09:09:13 +01:00
{
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-12-08 18:02:42 +01:00
if(circle.collide(this.x,this.y))
2023-11-15 09:09:13 +01:00
{
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-08 13:50:49 +01:00
constructor (x,y,dx,dy,id)
2023-11-15 09:09:13 +01:00
{
2023-12-08 18:29:33 +01:00
bulletSound.play();
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-08 13:50:49 +01:00
this.shooterId=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
}
}
2023-12-08 13:50:49 +01:00
checkCollisions(squares,circles)//only the client's player /!\
2023-11-15 09:09:13 +01:00
{
2023-12-08 13:50:49 +01:00
if(this.deleted)
return;
2023-12-08 18:29:33 +01:00
if(player!=null && player.id!=this.shooterId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
2023-11-15 09:09:13 +01:00
{
2023-12-08 18:29:33 +01:00
player.takeDamage(1,this.shooterId);
2023-12-08 13:50:49 +01:00
this.deleted=true;
return;
}
squares.forEach((square) => {
if(square.collide(this.x,this.y))
2023-11-15 09:09:13 +01:00
{
this.deleted=true;
return;
}
2023-12-08 13:50:49 +01:00
});
circles.forEach((circle) => {
if(circle.collide(this.x,this.y))
{
this.deleted=true;
return;
}
});
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;
}
2023-12-08 13:50:49 +01:00
collide(x,y)
2023-11-15 09:09:13 +01:00
{
2023-12-08 13:50:49 +01:00
return this.x<=x && x<=this.x+this.w && this.y<=y && y<=this.y+this.h;
2023-11-15 09:09:13 +01:00
}
}
class Circle
{
constructor(x,y,radius)
{
this.x=x;
this.y=y;
this.r=radius;
}
2023-12-08 13:50:49 +01:00
collide(x,y)
2023-11-15 09:09:13 +01:00
{
2023-12-08 13:50:49 +01:00
return (x-this.x)**2+(y-this.y)**2 <= this.r**2;
2023-11-15 09:09:13 +01:00
}
2023-12-07 10:45:31 +01:00
}
class Car
{
2023-12-08 13:50:49 +01:00
constructor(type, spawn)
2023-12-07 10:45:31 +01:00
{
2023-12-08 13:50:49 +01:00
this.type=type; // 0 circule vers le haut
2023-12-07 16:29:34 +01:00
// 1 circule vers le bas
2023-12-07 17:18:15 +01:00
if(this.type == 1) { //vers le bas
this.x=1247;
this.y=-40;
this.dir=4;
this.angle=Math.PI/2;
} else { //vers le haut
this.x=947;
this.y=1000;
this.dir=2;
this.angle=-Math.PI/2;
}
2023-12-07 18:53:34 +01:00
this.drift=0;
2023-12-08 13:50:49 +01:00
this.spawn=spawn;
2023-12-07 17:18:15 +01:00
this.tick=0;
2023-12-07 10:45:31 +01:00
}
2023-12-07 18:21:56 +01:00
collide(x,y)
{
let cx=this.x-carSize/2;
let cy=this.y-carSize/2;
2023-12-07 18:53:34 +01:00
let collide = (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize);
if(collide) {
2023-12-08 13:50:49 +01:00
driftSound.play()
2023-12-07 18:53:34 +01:00
this.drift=300;
}
2023-12-08 13:50:49 +01:00
return collide;
2023-12-07 18:21:56 +01:00
}
2023-12-07 10:45:31 +01:00
Update()
{
2023-12-11 15:03:47 +01:00
if(this.tick==0 && (new Date()).getSeconds()%20==this.spawn) {
2023-12-08 13:50:49 +01:00
this.tick=1;
2023-12-07 17:18:15 +01:00
}
2023-12-08 20:02:04 +01:00
if(this.tick==0)
return;
2023-12-08 13:50:49 +01:00
this.ChangeDirection();
2023-12-07 10:45:31 +01:00
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;
}
2023-12-07 18:53:34 +01:00
if(this.drift > 0) {
this.drift--
}
2023-12-07 10:45:31 +01:00
}
2023-12-07 12:04:32 +01:00
pseudoaleatoire() {
return Math.floor(Date.now()/1000)%10
2023-12-07 12:04:32 +01:00
}
2023-12-07 10:45:31 +01:00
ChangeDirection()
{
2023-12-07 16:29:34 +01:00
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) {
2023-12-07 12:04:32 +01:00
//tourner à droite
this.dir=1
this.angle=0
2023-12-07 16:29:34 +01:00
} 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
2023-12-07 17:18:15 +01:00
} else if(this.y>1000) {
//tp
this.x=1247
this.y=-40
this.tick=0;
2023-12-07 12:04:32 +01:00
}
2023-12-07 16:29:34 +01:00
} 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
2023-12-07 17:18:15 +01:00
this.tick=0;
2023-12-07 16:29:34 +01:00
} 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) {
2023-12-07 12:04:32 +01:00
this.dir=3
this.angle=-Math.PI
2023-12-07 16:29:34 +01:00
} 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
2023-12-07 12:04:32 +01:00
}
2023-12-07 10:45:31 +01:00
}
}
2023-12-09 14:53:07 +01:00
}
class PNJ{
constructor(x, y){
this.x=x;
this.y=y;
this.dir=1;
}
checkCollisions(x,y){
let colliding = false;
squares.forEach((square) => {
if(square.collide(x,y))
{
colliding = true;
return;
}
});
circles.forEach((circle) => {
if(circle.collide(x,y))
{
colliding=true;
return;
}
});
return colliding;
}
changeDirection(){
let newDir = this.dir;
while(newDir == this.dir){
newDir = Math.floor(Math.random()*8);
}
this.dir = newDir;
}
update(dt)
{
let dx,dy;
switch(this.dir)
{
case 1: dx=0.;dy=-PNJSpeed;break;
case 2: dx=halfSqrtTwo*PNJSpeed;dy=-halfSqrtTwo*PNJSpeed;break;
case 3: dx=PNJSpeed;dy=0.;break;
case 4: dx=halfSqrtTwo*PNJSpeed;dy=halfSqrtTwo*PNJSpeed;break;
case 5: dx=0.;dy=PNJSpeed;break;
case 6: dx=-halfSqrtTwo*PNJSpeed;dy=halfSqrtTwo*PNJSpeed;break;
case 7: dx=-PNJSpeed;dy=0.;break;
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.changeDirection();
}
if(Math.random()<=0.001){
this.changeDirection();
}
}
2023-11-15 09:09:13 +01:00
}