From 33f9086c43793970e240f07cfa74e3aec9644ac2 Mon Sep 17 00:00:00 2001 From: Atsuyo-INSA Date: Tue, 14 Nov 2023 16:54:18 +0100 Subject: [PATCH] class.js added --- class.js | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 class.js diff --git a/class.js b/class.js new file mode 100644 index 0000000..53dd1e8 --- /dev/null +++ b/class.js @@ -0,0 +1,130 @@ +const mapWidth = 210.; +const mapHeith = 100.; +const playerSize = 10.; + +class Player +{ + constructor (x,y,id) + { + this.x=x; + this.y=y; + this.id=id; + this.dir=0;//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=10; + } + + takeDamage(amount) + { + this.health-=amount; + if(this.health<=0) + { + //send death message to server + //this.reset() + } + } +} + +function dist(A,B) +{ + return sqrt((A.x-B.x)**2+(A.y-B.y)**2); +} + +class Bullet +{ + constructor (x,y,dx,dy) + { + this.x=x; + this.y=y; + this.dx=dx; + this.dy=dy; + this.deleted=false; + } + + update() + { + if(!this.deleted) + { + this.x+=this.dx; + this.y+=this.dy; + } + } + + checkCollisions(player,squares,circles)//only the client's player /!\ + { + if(!this.deleted) + { + if(dist(player,this) { + if (collideSquare(this,square)) + { + this.deleted=true; + break; + } + });*/ + for(let square in squares) + { + if(square.collide(this)) + { + this.deleted=true; + return; + } + } + + for(let circle in circles) + { + 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<=r**2); + } +} \ No newline at end of file