Browse Source

Trains have own color, passengers can stay on

Tyler Hallada 6 years ago
parent
commit
d1d342ee0f
2 changed files with 27 additions and 10 deletions
  1. 5 1
      src/Train.ts
  2. 22 9
      src/transport.ts

+ 5 - 1
src/Train.ts

@@ -1,3 +1,5 @@
1
+import * as tinycolor from 'tinycolor2';
2
+
1 3
 import Station from './Station';
2 4
 
3 5
 let trainCount = 0;
@@ -10,14 +12,16 @@ export default class Train {
10 12
   public passengers: number;
11 13
   public id: number;
12 14
   public label: PIXI.Text;
15
+  public color: tinycolorInstance;
13 16
 
14 17
   constructor(location: PIXI.Point, speed: number, passengers: number, origin: Station,
15
-              destination: Station) {
18
+              destination: Station, color: tinycolorInstance) {
16 19
     this.location = location;
17 20
     this.speed = speed;
18 21
     this.origin = origin;
19 22
     this.destination = destination;
20 23
     this.passengers = passengers;
24
+    this.color = color;
21 25
 
22 26
     // for debugging
23 27
     trainCount += 1;

+ 22 - 9
src/transport.ts

@@ -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
   }