-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroad.js
More file actions
70 lines (59 loc) · 2.09 KB
/
road.js
File metadata and controls
70 lines (59 loc) · 2.09 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
class Road{
constructor(x, width, laneCount=3){
this.x = x;
this.width = width;
this.laneCount = laneCount;
this.left= x-width/2;
this.right = x+width/2;
const infinity = 1000000;
this.top = -infinity
this.bottom = infinity;
this.lineWidth = 5;
this.lineColor = 'white';
// Borders Setting
const topLeft = {x: this.left, y: this.top};
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.borders = [
[topLeft,bottomLeft],
[topRight,bottomRight]
];
}
draw(context){
context.lineWidth = this.lineWidth;
context.strokeStyle = this.lineColor;
// Lane Lines Generation
for(let i=1; i<=this.laneCount-1; i++){
// To generate even percentage of lane width
// based on lane counts using Linear Interpolation
// Eg. Left = 15, Right = 285, LaneCount = 1
// B-A*0 = 0m, A = 15; B-A*1 = 285m, A = 285;
const x = lerp(
this.left,
this.right,
i/this.laneCount // even percentage of per lanewidth
);
// Set Line Dash for Lines in the middle
context.setLineDash([20,20]);
context.beginPath();
context.moveTo(x, this.top);
context.lineTo(x, this.bottom);
context.stroke();
}
// Borders Line Generation
context.setLineDash([]);
this.borders.forEach(border => {
context.beginPath();
context.moveTo(border[0].x, border[0].y);
context.lineTo(border[1].x, border[1].y);
context.stroke();
});
}
// Always get car spawned in the middle lane
getLaneCenter(laneIndex){
const laneWidth = this.width/this.laneCount;
return this.left + laneWidth/2 +
Math.min(laneIndex,this.laneCount-1)*laneWidth;
}
}