|
@@ -1,4 +1,6 @@
|
|
1
|
+import * as Viewport from 'pixi-viewport';
|
1
|
2
|
import * as PIXI from 'pixi.js';
|
|
3
|
+import * as Stats from 'stats.js';
|
2
|
4
|
import * as tinycolor from 'tinycolor2';
|
3
|
5
|
|
4
|
6
|
import Direction from './Direction';
|
|
@@ -16,10 +18,14 @@ const NODE_RES = 100;
|
16
|
18
|
const MAX_SPEED = 10.0;
|
17
|
19
|
const ACCELERATION = 0.025;
|
18
|
20
|
const APPROACH_DISTANCE = 3.0;
|
19
|
|
-const MAX_JOURNEY = Math.floor(Math.sqrt(
|
20
|
|
- Math.pow(window.innerWidth, 2) + Math.pow(window.innerHeight, 2),
|
21
|
|
-) / 4);
|
22
|
21
|
const TRAIN_CAPACITY = 50;
|
|
22
|
+const LINE_CONNECTION_LIMIT = 5;
|
|
23
|
+const WORLD_WIDTH = 1000;
|
|
24
|
+const WORLD_HEIGHT = 1000;
|
|
25
|
+const ZOOM_MIN_WIDTH = 100;
|
|
26
|
+const ZOOM_MIN_HEIGHT = 100;
|
|
27
|
+const ZOOM_MAX_WIDTH = 4000;
|
|
28
|
+const ZOOM_MAX_HEIGHT = 4000;
|
23
|
29
|
|
24
|
30
|
const trainTexts: PIXI.Text[] = [];
|
25
|
31
|
|
|
@@ -36,8 +42,11 @@ const initStations = (numStations: number): Station[] => {
|
36
|
42
|
|
37
|
43
|
const initTrains = (numTrains: number, stations: Station[]): Train[] => {
|
38
|
44
|
const trains = [];
|
|
45
|
+ const stationsWithConnections = stations.filter(station => station.connections.length > 0);
|
39
|
46
|
for (let i = 0; i < numTrains; i += 1) {
|
40
|
|
- const originStation = stations[Math.floor(Math.random() * stations.length)];
|
|
47
|
+ const originStation = stationsWithConnections[
|
|
48
|
+ Math.floor(Math.random() * stationsWithConnections.length)
|
|
49
|
+ ];
|
41
|
50
|
trains.push(new Train(
|
42
|
51
|
new PIXI.Point(originStation.location.x, originStation.location.y),
|
43
|
52
|
0, 0, originStation, undefined, tinycolor('grey')),
|
|
@@ -46,15 +55,36 @@ const initTrains = (numTrains: number, stations: Station[]): Train[] => {
|
46
|
55
|
return trains;
|
47
|
56
|
};
|
48
|
57
|
|
|
58
|
+const initLines = (numLines: number, stations: Station[]): Line[] => {
|
|
59
|
+ const lines = [];
|
|
60
|
+ for (let i = 0; i < numLines; i += 1) {
|
|
61
|
+ let color = tinycolor.random();
|
|
62
|
+ while (color.isDark()) {
|
|
63
|
+ color = tinycolor.random();
|
|
64
|
+ }
|
|
65
|
+ const stationsWithoutConnections = stations.filter(station =>
|
|
66
|
+ station.connections.length === 0,
|
|
67
|
+ );
|
|
68
|
+ const centralHub = Station.largestStation(stationsWithoutConnections);
|
|
69
|
+ const line = new Line(`line-${i}`, tinycolor.random());
|
|
70
|
+ const stationsLeft = stations.slice(0);
|
|
71
|
+ line.connectStations(centralHub, stationsLeft, [], LINE_CONNECTION_LIMIT);
|
|
72
|
+ lines.push(line);
|
|
73
|
+ }
|
|
74
|
+ return lines;
|
|
75
|
+};
|
|
76
|
+
|
49
|
77
|
const moveTrains = (trains: Train[], stations: Station[]) => {
|
50
|
78
|
for (const train of trains) {
|
|
79
|
+ if (train.origin.connections.length === 0) {
|
|
80
|
+ // train is stuck at an orphaned station
|
|
81
|
+ continue;
|
|
82
|
+ }
|
51
|
83
|
// choose a destination randomly with a bias towards larger stations
|
52
|
84
|
if (train.destination === undefined) {
|
53
|
|
- const otherStations = stations.filter(station => station !== train.origin);
|
54
|
|
- const closeStations = Station.stationsWithinRadius(otherStations, train.location,
|
55
|
|
- MAX_JOURNEY);
|
56
|
|
- const closeStationWeights = closeStations.map(station => station.population);
|
57
|
|
- train.destination = weightedRandom(closeStations, closeStationWeights);
|
|
85
|
+ const otherStations = train.origin.connections.map(conn => conn.station);
|
|
86
|
+ const closeStationWeights = otherStations.map(station => station.population);
|
|
87
|
+ train.destination = weightedRandom(otherStations, closeStationWeights);
|
58
|
88
|
|
59
|
89
|
// board passengers
|
60
|
90
|
const boardingPassengers = randomInt(0, Math.min(TRAIN_CAPACITY - train.passengers,
|
|
@@ -116,7 +146,7 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
|
116
|
146
|
|
117
|
147
|
const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
|
118
|
148
|
for (const station of stations) {
|
119
|
|
- const radius = station.population / 60;
|
|
149
|
+ const radius = station.population / 150;
|
120
|
150
|
graphics.beginFill(parseInt(station.color.toHex(), 16), 0.5);
|
121
|
151
|
graphics.drawCircle(station.location.x, station.location.y, radius);
|
122
|
152
|
graphics.endFill();
|
|
@@ -127,10 +157,6 @@ const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
|
127
|
157
|
|
128
|
158
|
const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
|
129
|
159
|
for (const train of trains) {
|
130
|
|
- // graphics.beginFill(parseInt(train.color.toHex(), 16), 0.8);
|
131
|
|
- // graphics.drawCircle(train.location.x, train.location.y,
|
132
|
|
- // rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5));
|
133
|
|
- // graphics.endFill();
|
134
|
160
|
const trainSize = rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5);
|
135
|
161
|
const scale = trainSize / NODE_RES;
|
136
|
162
|
train.sprite.x = train.location.x;
|
|
@@ -143,13 +169,18 @@ const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
|
143
|
169
|
}
|
144
|
170
|
};
|
145
|
171
|
|
146
|
|
-const drawLines = (lines: Line[], graphics: PIXI.Graphics) => {
|
147
|
|
- for (const line of lines) {
|
148
|
|
- graphics.lineStyle(1, parseInt(line.color.toHex(), 16), 1);
|
149
|
|
- const start = line.stations[0].location;
|
150
|
|
- graphics.moveTo(start.x, start.y);
|
151
|
|
- for (const station of line.stations.slice(1)) {
|
152
|
|
- graphics.lineTo(station.location.x, station.location.y);
|
|
172
|
+const drawLines = (stations: Station[], graphics: PIXI.Graphics) => {
|
|
173
|
+ for (const station of stations) {
|
|
174
|
+ for (const connection of station.connections) {
|
|
175
|
+ let twoWay = false;
|
|
176
|
+ for (const conn of connection.station.connections) {
|
|
177
|
+ if (conn.station === station) {
|
|
178
|
+ twoWay = true;
|
|
179
|
+ }
|
|
180
|
+ }
|
|
181
|
+ graphics.lineStyle(twoWay ? 2 : 1, parseInt(connection.line.color.toHex(), 16), 1);
|
|
182
|
+ graphics.moveTo(station.location.x, station.location.y);
|
|
183
|
+ graphics.lineTo(connection.station.location.x, connection.station.location.y);
|
153
|
184
|
}
|
154
|
185
|
}
|
155
|
186
|
};
|
|
@@ -160,41 +191,29 @@ const run = () => {
|
160
|
191
|
height: window.innerHeight,
|
161
|
192
|
width: window.innerWidth,
|
162
|
193
|
});
|
|
194
|
+ const viewport = new Viewport({
|
|
195
|
+ screenHeight: window.innerHeight,
|
|
196
|
+ screenWidth: window.innerWidth,
|
|
197
|
+ worldHeight: WORLD_HEIGHT,
|
|
198
|
+ worldWidth: WORLD_WIDTH,
|
|
199
|
+ });
|
|
200
|
+ const stats = new Stats();
|
|
201
|
+ stats.showPanel(0);
|
|
202
|
+ document.body.appendChild(stats.dom);
|
163
|
203
|
const ticker = new PIXI.ticker.Ticker();
|
164
|
204
|
const graphics = new PIXI.Graphics();
|
165
|
|
- const fpsText = new PIXI.Text('', { fontSize: '25px', fontFamily: 'monospace', fill: 'yellow' });
|
166
|
|
-
|
167
|
|
- fpsText.anchor = new PIXI.ObservablePoint(null, 0, 1);
|
168
|
|
- fpsText.x = window.innerWidth;
|
169
|
|
- fpsText.y = 0;
|
170
|
205
|
|
171
|
206
|
const stations = initStations(30);
|
|
207
|
+ const lines = initLines(4, stations);
|
172
|
208
|
const trains = initTrains(50, stations);
|
173
|
|
- const lines = [
|
174
|
|
- new Line(
|
175
|
|
- stations, new PIXI.Point(0, 0),
|
176
|
|
- Direction.Southeast, 12, tinycolor('red'),
|
177
|
|
- ),
|
178
|
|
- new Line(
|
179
|
|
- stations, new PIXI.Point(window.innerWidth, 0),
|
180
|
|
- Direction.Southwest, 12, tinycolor('darkcyan'),
|
181
|
|
- ),
|
182
|
|
- new Line(
|
183
|
|
- stations, new PIXI.Point(window.innerWidth, window.innerHeight),
|
184
|
|
- Direction.Northwest, 12, tinycolor('yellow'),
|
185
|
|
- ),
|
186
|
|
- new Line(
|
187
|
|
- stations, new PIXI.Point(0, window.innerHeight),
|
188
|
|
- Direction.Northeast, 12, tinycolor('green'),
|
189
|
|
- ),
|
190
|
|
- ];
|
191
|
209
|
|
192
|
210
|
ticker.stop();
|
193
|
211
|
ticker.add((deltaTime) => {
|
|
212
|
+ stats.begin();
|
|
213
|
+
|
194
|
214
|
moveTrains(trains, stations);
|
195
|
215
|
|
196
|
216
|
graphics.clear();
|
197
|
|
- fpsText.text = `${Math.round(ticker.FPS)}`;
|
198
|
217
|
|
199
|
218
|
graphics.lineStyle(1, 0xFFA500, 1);
|
200
|
219
|
drawStations(stations, graphics);
|
|
@@ -202,24 +221,32 @@ const run = () => {
|
202
|
221
|
graphics.lineStyle(1, 0xAEAEAE, 1);
|
203
|
222
|
drawTrains(trains, graphics);
|
204
|
223
|
|
205
|
|
- drawLines(lines, graphics);
|
|
224
|
+ drawLines(stations, graphics);
|
|
225
|
+
|
|
226
|
+ stats.end();
|
206
|
227
|
});
|
207
|
228
|
ticker.start();
|
208
|
229
|
|
209
|
|
- app.stage.addChild(graphics);
|
210
|
|
- app.stage.addChild(fpsText);
|
|
230
|
+ viewport.addChild(graphics);
|
211
|
231
|
// add train sprites
|
212
|
232
|
for (const train of trains) {
|
213
|
|
- app.stage.addChild(train.sprite);
|
|
233
|
+ viewport.addChild(train.sprite);
|
214
|
234
|
}
|
215
|
235
|
// Add debug labels
|
216
|
236
|
for (const train of trains) {
|
217
|
|
- app.stage.addChild(train.label);
|
|
237
|
+ viewport.addChild(train.label);
|
218
|
238
|
}
|
219
|
239
|
for (const station of stations) {
|
220
|
|
- app.stage.addChild(station.label);
|
|
240
|
+ viewport.addChild(station.label);
|
221
|
241
|
}
|
222
|
242
|
document.body.appendChild(app.view);
|
|
243
|
+ app.stage.addChild(viewport);
|
|
244
|
+ viewport.drag().pinch().wheel().clampZoom({
|
|
245
|
+ maxHeight: ZOOM_MAX_HEIGHT,
|
|
246
|
+ maxWidth: ZOOM_MAX_WIDTH,
|
|
247
|
+ minHeight: ZOOM_MIN_HEIGHT,
|
|
248
|
+ minWidth: ZOOM_MIN_WIDTH,
|
|
249
|
+ }).decelerate();
|
223
|
250
|
|
224
|
251
|
window.addEventListener('resize', () => {
|
225
|
252
|
app.renderer.resize(window.innerWidth, window.innerHeight);
|