-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic.js
123 lines (107 loc) · 3.81 KB
/
traffic.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Traffic{
constructor(y, height, cnt = 10, range = 300, laneCount, lane){
this.y = y;
this.start = y;
this.height = height // height of car
this.infinity = 100200
this.cars = [];
this.range = range;
this.laneCount = laneCount
this.yCoords = []
for(let i = -this.infinity ; i <= this.infinity ; i += this.range){
this.yCoords.push(i)
}
this.begin = find(this.yCoords, this.y)
for(let i = this.begin - 2; i <= this.begin + 2 ;++i){
if(i != this.begin){
this.cars.push(this.#getLine(this.yCoords[i], -1))
}
else{
this.cars.push(this.#getLine(this.yCoords[i], (lane + 2) % laneCount))
}
}
this.first = this.begin - 2;
this.last = this.begin + 2;
}
// #checkY(x, y){
// this.cars.forEach(car => {
// if(x == car.x && y > car.y - car.height - 10 && y < car.y + car.height + 10){
// return false;
// }
// })
// return true;
// }
#getLine(y, num = -1){
if(num == -1)
num = Math.floor(Math.random() * this.laneCount)
let line = []
for(let i = 0 ; i < this.laneCount ; ++i){
if(i != num){
line.push(new Car(road.getLaneCenter(i), y, carCanvas.width * 0.9/6 * 0.4, carCanvas.width * 0.9/6 * 0.7, "DUMMY", 2.75));
}
else{
continue;
}
}
this.yCoords.push(y);
return line
}
// #getY(x){
// let y = Math.random() > 0.5 ?
// lerp(this.frontMax, this.frontMin , Math.random()) :
// lerp(this.backMin, this.backMax, Math.random())
// while(!this.#checkY(x, y)){
// y = Math.random() > 0.5 ?
// lerp(this.frontMax, this.frontMin, Math.random()) :
// lerp(this.backMin, this.backMax, Math.random())
// }
// return y
// }
// #getCar(laneCount, laneNum, y){
// let angle = 0
// let car;
// if(Math.random() > 0.5){
// let lane = Math.floor(Math.random() * laneCount / 2)
// let x = road.getLaneCenter(lane)
// car = new Car(x, this.#getY(x), 30, 50, "DUMMY", 2.75)
// }
// else{
// let lane = laneCount/2 + Math.floor(Math.random() * laneCount/ 2)
// let x = road.getLaneCenter(lane)
// car = new Car(x, this.#getY(x), 30, 50, "DUMMY", 2.75)
// angle = Math.PI
// }
// car.angle = angle;
// return car
// }
update(y, laneCount){
// traffic cars has to spawn in particular range within OutBestCar
// for memory efficient purpose
this.y = y;
// this.frontMax = this.y - 3 * this.range
// this.frontMin = this.y - 2 * this.range
// this.backMin = this.y + 2 * this.range
// this.backMax = this.y + 3 * this.range
let x = find(this.yCoords, this.y);
if(x > this.first + 2){
this.cars.shift()
this.cars.push(this.#getLine(this.yCoords[this.last + 1]))
this.first++;
this.last++;
}
else if(x < this.first + 2){
this.cars.pop();
this.cars.unshift(this.#getLine(this.yCoords[this.first - 1]))
this.last--;
this.first--;
}
this.cars.forEach(
(trafficcars) => trafficcars.forEach(
car => car.update(road.borders, this.cars, true)
)
)
}
// draw(ctx){
// this.cars.forEach((trafficcars) => trafficcars.draw(ctx))
// }
}