|
@@ -37,7 +37,7 @@ const initTrains = (numTrains: number, stations: Station[]): Train[] => {
|
37
|
37
|
const originStation = stations[Math.floor(Math.random() * stations.length)];
|
38
|
38
|
trains.push(new Train(
|
39
|
39
|
new PIXI.Point(originStation.location.x, originStation.location.y),
|
40
|
|
- 0, 0, originStation, undefined),
|
|
40
|
+ 0, 0, originStation, undefined, tinycolor('grey')),
|
41
|
41
|
);
|
42
|
42
|
}
|
43
|
43
|
return trains;
|
|
@@ -54,8 +54,20 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
|
54
|
54
|
train.destination = weightedRandom(closeStations, closeStationWeights);
|
55
|
55
|
|
56
|
56
|
// board passengers
|
57
|
|
- train.passengers += randomInt(0, TRAIN_CAPACITY);
|
58
|
|
- train.origin.population -= randomInt(0, TRAIN_CAPACITY);
|
|
57
|
+ const boardingPassengers = randomInt(0, Math.min(TRAIN_CAPACITY - train.passengers,
|
|
58
|
+ train.origin.population));
|
|
59
|
+ // set or mix train color with the color of new passenger origin
|
|
60
|
+ if (train.passengers === 0) {
|
|
61
|
+ train.color = train.origin.color;
|
|
62
|
+ } else {
|
|
63
|
+ train.color = tinycolor.mix(
|
|
64
|
+ train.color,
|
|
65
|
+ train.origin.color,
|
|
66
|
+ Math.round((boardingPassengers / train.passengers) * 100),
|
|
67
|
+ );
|
|
68
|
+ }
|
|
69
|
+ train.passengers += boardingPassengers;
|
|
70
|
+ train.origin.population -= boardingPassengers;
|
59
|
71
|
}
|
60
|
72
|
|
61
|
73
|
// train reached destination, stop moving and let passengers off
|
|
@@ -71,8 +83,9 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
|
71
|
83
|
);
|
72
|
84
|
|
73
|
85
|
// transfer passengers to destination
|
74
|
|
- train.destination.population += train.passengers;
|
75
|
|
- train.passengers = 0;
|
|
86
|
+ const disembarkingPassengers = randomInt(0, train.passengers);
|
|
87
|
+ train.destination.population += disembarkingPassengers;
|
|
88
|
+ train.passengers -= disembarkingPassengers;
|
76
|
89
|
|
77
|
90
|
// prepare for next journey
|
78
|
91
|
train.origin = train.destination;
|
|
@@ -111,7 +124,7 @@ const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
|
111
|
124
|
|
112
|
125
|
const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
|
113
|
126
|
for (const train of trains) {
|
114
|
|
- graphics.beginFill(parseInt(train.origin.color.toHex(), 16), 0.8);
|
|
127
|
+ graphics.beginFill(parseInt(train.color.toHex(), 16), 0.8);
|
115
|
128
|
graphics.drawCircle(train.location.x, train.location.y,
|
116
|
129
|
rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5));
|
117
|
130
|
graphics.endFill();
|
|
@@ -186,9 +199,9 @@ const run = () => {
|
186
|
199
|
app.stage.addChild(graphics);
|
187
|
200
|
app.stage.addChild(fpsText);
|
188
|
201
|
// Add debug labels
|
189
|
|
- // for (const train of trains) {
|
190
|
|
- // app.stage.addChild(train.label);
|
191
|
|
- // }
|
|
202
|
+ for (const train of trains) {
|
|
203
|
+ app.stage.addChild(train.label);
|
|
204
|
+ }
|
192
|
205
|
for (const station of stations) {
|
193
|
206
|
app.stage.addChild(station.label);
|
194
|
207
|
}
|