-
Notifications
You must be signed in to change notification settings - Fork 19
/
mainloop.js
161 lines (138 loc) · 3.99 KB
/
mainloop.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let isPaused = false
window.onload = function() {
// Toggle pause when the pause button is clicked
document.getElementById('pauseButton').addEventListener('click', function() {
isPaused = !isPaused;
});
};
function MAINLOOP(time){
if (!isPaused) {
const elapsedTime = time - prevTime;
if (elapsedTime >= 1000) {
count = 0;
prevTime = time;
}
count++;
//use of requestAnimationFrame: adapted from https://zenn.dev/baroqueengine/books/a19140f2d9fc1a/viewer/ebed0a
if(T%DELTAT == 0){
updates()
}
if(T%DELTAT_DRAW == 0){
draws()
}
if(T%DELTAT_EDIE == 0){
edies()
}
T++
}
requestAnimationFrame(MAINLOOP);
}
function updates(){
for(let link of LINKS){
link.update()
}
for(let fdc of FDCHANGERS){
if(document.getElementById("bn_none") != null){
fdc.delta = document.getElementsByName("bn_impact").item(0).checked*1 + document.getElementsByName("bn_impact").item(1).checked*2 + document.getElementsByName("bn_impact").item(2).checked*4
}
fdc.update()
}
for(let veh of VEHS){
veh.speed_change()
}
for(let veh of VEHS){
veh.update()
}
for(let s of SPAWNERS){
s.flow = Number(document.getElementById("inflow").value)/100
document.getElementById("inflow_value").innerHTML = s.flow.toFixed(2)
s.update()
}
for(let r of REGULATORS){
r.num = Number(document.getElementById("num").value)
document.getElementById("num_value").innerHTML = r.num.toFixed(0)
r.update()
}
if(document.getElementsByName("model").item(1).checked == 0){
ACC = 999
DECEL_PROB = 0
for(let veh of VEHS){
veh.acc = ACC
veh.decel_prob = DECEL_PROB
}
}else{
ACC = 1
DECEL_PROB = 0.05
for(let veh of VEHS){
veh.acc = ACC
veh.decel_prob = DECEL_PROB
}
}
var VEHS_new = []
for(let veh of VEHS){
if(veh.flag_delete != 1){
VEHS_new.push(veh)
}
}
VEHS = VEHS_new
for(let tsp of TSPLOTS){
tsp.update()
}
for(let cp of CUMPLOTS){
cp.update()
}
}
function draws(){
CTX.fillStyle = "#eeeeee"
CTX.fillRect(0, 0, CANVAS.width, CANVAS.height)
for(let qkp of QKPLOTS){
if(document.getElementById("flow_density").checked == 1){
qkp.draw()
}
}
for(let link of LINKS){
link.draw()
if(document.getElementById("traffic_state").checked == 1){
link.draw_trafficstate()
}
}
for(let veh of VEHS){
veh.draw()
}
for(let s of SPAWNERS){
s.draw()
}
if(document.getElementById("ts_diagram_trajects").checked == 1 || document.getElementById("ts_diagram_state").checked == 1){
if(CANVAS.height != 590 && CANVAS.height != 860){
CANVAS.height = 590
}
for(let tsp of TSPLOTS){
tsp.draw(
document.getElementById("ts_diagram_trajects").checked,
document.getElementById("ts_diagram_state").checked,
document.getElementsByName("ts_diagram_state_which").item(0).checked*100 + document.getElementsByName("ts_diagram_state_which").item(1).checked*10 + document.getElementsByName("ts_diagram_state_which").item(2).checked*1
)
}
}
if(document.getElementById("cumlative").checked == 1){
if(CANVAS.height != 860){
CANVAS.height = 860
}
for(let cp of CUMPLOTS){
cp.draw()
}
}
}
function edies(){
for(let link of LINKS){
link.calc_edie()
}
for(let qkp of QKPLOTS){
if(document.getElementById("flow_density").checked == 1){
qkp.add_qk()
}
}
for(let tsp of TSPLOTS){
tsp.collect_states()
}
}