Create road.js
Browse files
road.js
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Road{
|
2 |
+
constructor(x,width,laneCount=3){
|
3 |
+
this.x=x;
|
4 |
+
this.width=width;
|
5 |
+
this.laneCount=laneCount;
|
6 |
+
this.left=x-width/4;
|
7 |
+
this.right=x+width/4;
|
8 |
+
|
9 |
+
const infinity=1000000;
|
10 |
+
this.top=-infinity;
|
11 |
+
this.bottom=infinity;
|
12 |
+
|
13 |
+
const topLeft={x:this.left,y:this.top};
|
14 |
+
const bottomLeft={x:this.left,y:this.bottom};
|
15 |
+
const topRight={x:this.right,y:this.top};
|
16 |
+
const bottomRight={x:this.right,y:this.bottom};
|
17 |
+
this.borders=[
|
18 |
+
[topLeft],
|
19 |
+
[topRight]
|
20 |
+
];
|
21 |
+
for(let y=-1000;y<=0;y++){
|
22 |
+
const x=Math.sin(y*0.01)*50;
|
23 |
+
this.borders[0].push({x:x+this.left,y:y});
|
24 |
+
this.borders[1].push({x:x+this.right,y:y});
|
25 |
+
}
|
26 |
+
this.borders[0].push(bottomLeft);
|
27 |
+
this.borders[1].push(bottomRight);
|
28 |
+
}
|
29 |
+
|
30 |
+
getLaneCenter(laneIndex){
|
31 |
+
const laneWidth=this.width/this.laneCount;
|
32 |
+
return this.left+laneWidth/2+
|
33 |
+
Math.min(laneIndex,this.laneCount-1)*laneWidth;
|
34 |
+
}
|
35 |
+
|
36 |
+
draw(ctx){
|
37 |
+
ctx.lineWidth=5;
|
38 |
+
ctx.strokeStyle="white";
|
39 |
+
|
40 |
+
/*
|
41 |
+
ctx.setLineDash([20,20]);
|
42 |
+
for(let i=1;i<=this.laneCount-1;i++){
|
43 |
+
const x=lerp(
|
44 |
+
this.left,
|
45 |
+
this.right,
|
46 |
+
i/this.laneCount
|
47 |
+
);
|
48 |
+
ctx.beginPath();
|
49 |
+
ctx.moveTo(x,this.top);
|
50 |
+
ctx.lineTo(x,this.bottom);
|
51 |
+
ctx.stroke();
|
52 |
+
}
|
53 |
+
*/
|
54 |
+
|
55 |
+
ctx.setLineDash([20,20]);
|
56 |
+
this.borders.forEach(border=>{
|
57 |
+
ctx.beginPath();
|
58 |
+
ctx.moveTo(border[0].x,border[0].y);
|
59 |
+
for(let i=1;i<border.length;i++){
|
60 |
+
ctx.lineTo(border[i].x,border[i].y);
|
61 |
+
}
|
62 |
+
ctx.stroke();
|
63 |
+
});
|
64 |
+
ctx.setLineDash([]);
|
65 |
+
}
|
66 |
+
}
|