From 61642cb70f5439f52ad6074a6d5d22316145dc45 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Sat, 14 Apr 2018 18:22:16 -0400 Subject: [PATCH] Move couple util funcs to Station, add comments --- src/Station.ts | 24 +++++++++++++++++++++++- src/transport.ts | 26 ++++---------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Station.ts b/src/Station.ts index 582dcab..26c7aa2 100644 --- a/src/Station.ts +++ b/src/Station.ts @@ -1,6 +1,6 @@ import * as tinycolor from 'tinycolor2'; -import { distance } from './utils'; +import { distance, randomPoint } from './utils'; let stationCount = 0; @@ -27,6 +27,28 @@ export default class Station { ); } + public static isPointDistant(point: PIXI.Point, stations: Station[], + minDistance: number): boolean { + for (const station of stations) { + if (distance(point, station.location) < minDistance) { + return false; + } + } + return true; + } + + public static randomDistantPoint(stations: Station[], minDistance: number): PIXI.Point | null { + let tries = 100; + while (tries > 0) { + const point = randomPoint(); + if (Station.isPointDistant(point, stations, minDistance)) { + return point; + } + tries -= 1; + } + return null; + } + public location: PIXI.Point; public population: number; public connections: Station[]; diff --git a/src/transport.ts b/src/transport.ts index d479899..b906816 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -19,32 +19,11 @@ const TRAIN_CAPACITY = 50; const trainTexts: PIXI.Text[] = []; -const isPointDistant = (point: PIXI.Point, stations: Station[], minDistance: number): boolean => { - for (const station of stations) { - if (distance(point, station.location) < minDistance) { - return false; - } - } - return true; -}; - -const randomDistantPoint = (stations: Station[], minDistance: number): PIXI.Point | null => { - let tries = 100; - while (tries > 0) { - const point = randomPoint(); - if (isPointDistant(point, stations, minDistance)) { - return point; - } - tries -= 1; - } - return null; -}; - const initStations = (numStations: number): Station[] => { const stations: Station[] = []; for (let i = 0; i < numStations; i += 1) { stations.push(new Station( - randomDistantPoint(stations, 30), + Station.randomDistantPoint(stations, 30), randomInt(300, 2000), tinycolor.random())); } @@ -82,12 +61,15 @@ const moveTrains = (trains: Train[], stations: Station[]) => { if (pointsAlmostEqual(train.location, train.destination.location)) { train.speed = 0; + // average destination color with passenger color weighted by ratio + // (a simulation of culture mixing) train.destination.color = tinycolor.mix( train.destination.color, train.origin.color, Math.round((train.passengers / train.destination.population) * 100), ); + // transfer passengers to destination train.destination.population += train.passengers; train.passengers = 0;