PlayerCollisions+Update
This commit is contained in:
parent
37d5b1fe38
commit
c01995d423
1 changed files with 56 additions and 2 deletions
58
class.js
58
class.js
|
@ -1,7 +1,8 @@
|
|||
const mapWidth = 210.;
|
||||
const mapHeith = 100.;
|
||||
const playerSize = 10.;
|
||||
|
||||
const playerSize = 50.;
|
||||
const playerSpeed=5.;
|
||||
const halfSqrtTwo=sqrt(2.)/2.;
|
||||
class Player
|
||||
{
|
||||
constructor (id,x,y,name)
|
||||
|
@ -34,7 +35,60 @@ class Player
|
|||
}
|
||||
}
|
||||
|
||||
retrieveServerInfo(id,x,y,dir)
|
||||
{
|
||||
if(this.id==id)
|
||||
{
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
this.dir=dir;
|
||||
if(dir!=0)
|
||||
this.visibleDir=dir;
|
||||
}
|
||||
}
|
||||
|
||||
update(squares,circles)//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=1.;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;
|
||||
this.y += dy;
|
||||
|
||||
for(let square in squares)
|
||||
{
|
||||
if(square.collide(this))
|
||||
{
|
||||
this.x-=dx;
|
||||
this.y-=dy;
|
||||
this.dir=0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
for(let circle in circles)
|
||||
{
|
||||
if(circle.collide(this))
|
||||
{
|
||||
this.x-=dx;
|
||||
this.y-=dy;
|
||||
this.dir=0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get angle()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue