Fix random cases where init would error out

This commit is contained in:
Tyler Hallada 2018-04-16 22:49:29 -04:00
parent 4ecc93189b
commit 12b2fa7400

View File

@ -65,7 +65,12 @@ const initLines = (numLines: number, stations: Station[]): Line[] => {
const stationsWithoutConnections = stations.filter(station => const stationsWithoutConnections = stations.filter(station =>
station.connections.length === 0, station.connections.length === 0,
); );
const centralHub = Station.largestStation(stationsWithoutConnections); let centralHub: Station;
if (stationsWithoutConnections.length > 0) {
centralHub = Station.largestStation(stationsWithoutConnections);
} else {
centralHub = stations[randomInt(0, stations.length - 1)];
}
const line = new Line(`line-${i}`, tinycolor.random()); const line = new Line(`line-${i}`, tinycolor.random());
const stationsLeft = stations.slice(0); const stationsLeft = stations.slice(0);
line.connectStations(centralHub, stationsLeft, [], LINE_CONNECTION_LIMIT); line.connectStations(centralHub, stationsLeft, [], LINE_CONNECTION_LIMIT);
@ -203,8 +208,15 @@ const run = () => {
const ticker = new PIXI.ticker.Ticker(); const ticker = new PIXI.ticker.Ticker();
const graphics = new PIXI.Graphics(); const graphics = new PIXI.Graphics();
const stations = initStations(30); let stations: Station[] = [];
const lines = initLines(4, stations); let lines: Line[] = [];
let stationsWithConnections: Station[] = [];
while (stationsWithConnections.length === 0) {
// If all stations are too far away to connect, try generating again
stations = initStations(30);
lines = initLines(4, stations);
stationsWithConnections = stations.filter(station => station.connections.length > 0);
}
const trains = initTrains(50, stations); const trains = initTrains(50, stations);
ticker.stop(); ticker.stop();