-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroad.js
More file actions
90 lines (74 loc) · 2.38 KB
/
road.js
File metadata and controls
90 lines (74 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Road
{
constructor(x,width,laneCount=3) //center it on the x and let it have a width
{
//its attributes
this.x=x;
this.width=width;
this.laneCount=laneCount;
this.left=x-width/2; //pre-compute , to be used later
this.right=x+width/2;
//have the road go beyond
const infinity=100000;
this.top = -infinity;
this.bottom=infinity;
const topLeft={x:this.left,y:this.top}; // the topleft corner of the road
const topRight={x:this.right,y:this.top};
const bottomLeft={x:this.left,y:this.bottom};
const bottomRight={x:this.right,y:this.bottom};
this.boarders = [
//defining the zone of operation/ driving
//two segments made of two points each
[topLeft,bottomLeft],
[topRight,bottomRight]
];
}
///define a function that will always position the vehicle ay the center of a
//given lane
getLaneCenter(laneIndex)
{
const laneWidth=this.width/this.laneCount; // get the width measurement of the
// lane
return this.left+laneWidth/2+
Math.min(laneIndex,this.laneCount-1)*laneWidth;
}
//draw the road
draw(ctx)
{
ctx.lineWidth=5;
ctx.strokeStyle="white";
for(let i=1; i<=this.laneCount-1;i++)
{
const x = lerp(
this.left,
this.right,
i/this.laneCount
);
ctx.setLineDash([20,20]); //20 pixels break and dash
ctx.beginPath();
ctx.moveTo(x,this.top);
ctx.lineTo(x,this.bottom);
ctx.stroke();
}
ctx.setLineDash([]);
this.boarders.forEach(boarder=>
{
ctx.beginPath();
ctx.moveTo(boarder[0].x,boarder[0].y); //move to the first point in the
//boarder
ctx.lineTo(boarder[1].x, boarder[1].y); //second point
ctx.stroke();
});
/*
// draw end-lanes manually
ctx.beginPath();
ctx.moveTo(this.left,this.top);
ctx.lineTo(this.left,this.bottom);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(this.right,this.top);
ctx.lineTo(this.right,this.bottom);
ctx.stroke();
*/
}
}