Browse Source

Move couple util funcs to Station, add comments

Tyler Hallada 6 years ago
parent
commit
61642cb70f
2 changed files with 27 additions and 23 deletions
  1. 23 1
      src/Station.ts
  2. 4 22
      src/transport.ts

+ 23 - 1
src/Station.ts

@@ -1,6 +1,6 @@
1 1
 import * as tinycolor from 'tinycolor2';
2 2
 
3
-import { distance } from './utils';
3
+import { distance, randomPoint } from './utils';
4 4
 
5 5
 let stationCount = 0;
6 6
 
@@ -27,6 +27,28 @@ export default class Station {
27 27
     );
28 28
   }
29 29
 
30
+  public static isPointDistant(point: PIXI.Point, stations: Station[],
31
+                               minDistance: number): boolean {
32
+    for (const station of stations) {
33
+      if (distance(point, station.location) < minDistance) {
34
+        return false;
35
+      }
36
+    }
37
+    return true;
38
+  }
39
+
40
+  public static randomDistantPoint(stations: Station[], minDistance: number): PIXI.Point | null {
41
+    let tries = 100;
42
+    while (tries > 0) {
43
+      const point = randomPoint();
44
+      if (Station.isPointDistant(point, stations, minDistance)) {
45
+        return point;
46
+      }
47
+      tries -= 1;
48
+    }
49
+    return null;
50
+  }
51
+
30 52
   public location: PIXI.Point;
31 53
   public population: number;
32 54
   public connections: Station[];

+ 4 - 22
src/transport.ts

@@ -19,32 +19,11 @@ const TRAIN_CAPACITY = 50;
19 19
 
20 20
 const trainTexts: PIXI.Text[] = [];
21 21
 
22
-const isPointDistant = (point: PIXI.Point, stations: Station[], minDistance: number): boolean => {
23
-  for (const station of stations) {
24
-    if (distance(point, station.location) < minDistance) {
25
-      return false;
26
-    }
27
-  }
28
-  return true;
29
-};
30
-
31
-const randomDistantPoint = (stations: Station[], minDistance: number): PIXI.Point | null => {
32
-  let tries = 100;
33
-  while (tries > 0) {
34
-    const point = randomPoint();
35
-    if (isPointDistant(point, stations, minDistance)) {
36
-      return point;
37
-    }
38
-    tries -= 1;
39
-  }
40
-  return null;
41
-};
42
-
43 22
 const initStations = (numStations: number): Station[] => {
44 23
   const stations: Station[] = [];
45 24
   for (let i = 0; i < numStations; i += 1) {
46 25
     stations.push(new Station(
47
-      randomDistantPoint(stations, 30),
26
+      Station.randomDistantPoint(stations, 30),
48 27
       randomInt(300, 2000),
49 28
       tinycolor.random()));
50 29
   }
@@ -82,12 +61,15 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
82 61
     if (pointsAlmostEqual(train.location, train.destination.location)) {
83 62
       train.speed = 0;
84 63
 
64
+      // average destination color with passenger color weighted by ratio
65
+      // (a simulation of culture mixing)
85 66
       train.destination.color = tinycolor.mix(
86 67
         train.destination.color,
87 68
         train.origin.color,
88 69
         Math.round((train.passengers / train.destination.population) * 100),
89 70
       );
90 71
 
72
+      // transfer passengers to destination
91 73
       train.destination.population += train.passengers;
92 74
       train.passengers = 0;
93 75