-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
208 lines (178 loc) · 5.9 KB
/
app.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
!function (window, canvas) {
'use strict';
var busImage = new Image();
var stopImage = new Image();
var buses = [];
var stops = [];
var bus = function (x, y) {
var speed = 1;
var stuckAtLights = false;
var currentStop = null;
var tick = function () {
if (!stuckAtLights) {
x += speed;
}
if (currentStop !== null) {
speed = 0;
currentStop.removeWaitingPassenger();
if (currentStop.nPassengersWaiting() <= 0) { // we don't have to wait next time
departedStop();
}
} else { // between bus stops
speed = 1;
}
if (x >= 1600) {
x -= 1600;
}
};
var arrivedAtStop = function (stop) {
stop.setBusCurrentlyStopped(true);
currentStop = stop;
};
var departedStop = function () {
currentStop.setBusCurrentlyStopped(false);
currentStop = null;
};
var boundingBox = function () {
var toReturn = {};
toReturn.xmin = x - 10;
toReturn.xmax = x + 42;
toReturn.ymin = y - 10;
toReturn.ymax = y + 32;
return toReturn;
};
var click = function () {
stuckAtLights = !stuckAtLights;
console.log("Bus clicked. Stuck : " + stuckAtLights);
};
var draw = function (ctx) {
ctx.drawImage(busImage, x - 3.5, y + 3.5);
if (x > 1500) {
ctx.drawImage(busImage, x - 3.5 - 1600, y + 3.5);
}
};
return {
x: function () {
return x;
},
draw: draw,
click: click,
tick: tick,
arrivedAtStop: arrivedAtStop,
boundingBox: boundingBox
};
};
var stop = function (x, y) {
var nPassengersWaiting = 100.0;
var arrivalRate = 0.2;
var busCurrentlyStopped = false;
var tick = function () {
nPassengersWaiting += arrivalRate;
};
var click = function () {
if (arrivalRate < 0.3) {
arrivalRate += 0.2;
} else {
arrivalRate = 0.2;
}
};
var boundingBox = function () {
var toReturn = {};
toReturn.xmin = x - 30;
toReturn.xmax = x + 62;
toReturn.ymin = y;
toReturn.ymax = y + 100;
return toReturn;
};
var draw = function (ctx) {
var seed = x;
for (var i = 0; i < nPassengersWaiting; i += 10) {
var tmp = Math.sin(seed++) * 10000;
var randomInZeroToOne = tmp - Math.floor(tmp);
var xOffset = (randomInZeroToOne - 0.5) * 20;
ctx.drawImage(stopImage, x + xOffset, y + i / 2);
}
};
return {
x: function () {
return x;
},
isBusCurrentlyStopped: function () {
return busCurrentlyStopped;
},
setBusCurrentlyStopped: function (b) {
busCurrentlyStopped = b;
},
nPassengersWaiting: function () {
return nPassengersWaiting;
},
removeWaitingPassenger: function () {
nPassengersWaiting -= 1;
},
tick: tick,
click: click,
boundingBox: boundingBox,
draw: draw
}
};
var draw = function () {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 1600, 800);
// Update buses and draw them
for (var bus of buses) {
bus.draw(ctx);
bus.tick();
}
// Update bus stops and draw them
for (var stop of stops) {
stop.draw(ctx);
stop.tick();
}
// Check for buses arriving at stops
for (var possiblyArrivingBus of buses) {
for (var stopWithPossibleBus of stops) {
if (possiblyArrivingBus.x() === stopWithPossibleBus.x() && !stopWithPossibleBus.isBusCurrentlyStopped()) {
possiblyArrivingBus.arrivedAtStop(stopWithPossibleBus);
}
}
}
window.requestAnimationFrame(draw);
};
var inside = function (x, y, bbox) {
if (x > bbox.xmax) return false;
if (x < bbox.xmin) return false;
if (y > bbox.ymax) return false;
//noinspection RedundantIfStatementJS
if (y < bbox.ymin) return false;
return true;
};
canvas.addEventListener("mousedown", function (event) {
var xClicked = event.x;
var yClicked = event.y;
xClicked -= canvas.offsetLeft;
yClicked -= canvas.offsetTop;
// Find which stop the click is within, if any
for (var stopClickIndex = 0; stopClickIndex < stops.length; ++stopClickIndex) {
var stop = stops[stopClickIndex];
if (inside(xClicked, yClicked, stop.boundingBox())) {
stop.click();
}
}
// Find which bus the click is within, if any
for (var busClickIndex = 0; busClickIndex < buses.length; ++busClickIndex) {
var bus = buses[busClickIndex];
if (inside(xClicked, yClicked, bus.boundingBox())) {
bus.click();
}
}
}, false);
busImage.src = 'smallBus.png';
stopImage.src = 'person.png';
for (var busPosition = 0; busPosition < 1600; busPosition += 400) {
buses.push(bus(busPosition, 150));
}
for (var stopPosition = 200; stopPosition < 1600; stopPosition += 400) {
stops.push(stop(stopPosition, 200));
}
window.requestAnimationFrame(draw);
}(window, document.getElementById('tutorial'));