Browse Source

Add train & station labels, fix train movement

Tyler Hallada 6 years ago
parent
commit
502f15f1ef
3 changed files with 47 additions and 7 deletions
  1. 13 0
      src/Station.ts
  2. 13 0
      src/Train.ts
  3. 21 7
      src/transport.ts

+ 13 - 0
src/Station.ts

@@ -1,5 +1,7 @@
1 1
 import { distance } from './utils';
2 2
 
3
+let stationCount = 0;
4
+
3 5
 export default class Station {
4 6
   // Utility methods for working with arrays of Stations
5 7
   public static largestStation(stations: Station[]): Station {
@@ -26,10 +28,21 @@ export default class Station {
26 28
   public location: PIXI.Point;
27 29
   public population: number;
28 30
   public connections: Station[];
31
+  public id: number;
32
+  public label: PIXI.Text;
29 33
 
30 34
   constructor(location: PIXI.Point, population: number, connections?: Station[]) {
31 35
     this.location = location;
32 36
     this.population = population;
33 37
     this.connections = connections;
38
+
39
+    // for debugging
40
+    stationCount += 1;
41
+    this.id = stationCount;
42
+    this.label = new PIXI.Text(`${this.id}`, {
43
+      fill: 'orange',
44
+      fontFamily: 'monospace',
45
+      fontSize: '12px',
46
+    });
34 47
   }
35 48
 }

+ 13 - 0
src/Train.ts

@@ -1,11 +1,15 @@
1 1
 import Station from './Station';
2 2
 
3
+let trainCount = 0;
4
+
3 5
 export default class Train {
4 6
   public location: PIXI.Point;
5 7
   public speed: number;
6 8
   public origin: Station;
7 9
   public destination: Station;
8 10
   public passengers: number;
11
+  public id: number;
12
+  public label: PIXI.Text;
9 13
 
10 14
   constructor(location: PIXI.Point, speed: number, passengers: number, origin: Station,
11 15
               destination: Station) {
@@ -14,6 +18,15 @@ export default class Train {
14 18
     this.origin = origin;
15 19
     this.destination = destination;
16 20
     this.passengers = passengers;
21
+
22
+    // for debugging
23
+    trainCount += 1;
24
+    this.id = trainCount;
25
+    this.label = new PIXI.Text(`${this.id}`, {
26
+      fill: 'white',
27
+      fontFamily: 'monospace',
28
+      fontSize: '12px',
29
+    });
17 30
   }
18 31
 
19 32
   public boardPassengers() {

+ 21 - 7
src/transport.ts

@@ -9,6 +9,8 @@ import './style.css';
9 9
 const maxSpeed = 10.0;
10 10
 const acceleration = 0.25;
11 11
 
12
+const trainTexts: PIXI.Text[] = [];
13
+
12 14
 const isPointDistant = (point: PIXI.Point, stations: Station[], minDistance: number): boolean => {
13 15
   for (const station of stations) {
14 16
     if (distance(point, station.location) < minDistance) {
@@ -40,7 +42,10 @@ const initStations = (numStations: number): Station[] => {
40 42
 
41 43
 const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
42 44
   for (const station of stations) {
43
-    graphics.drawCircle(station.location.x, station.location.y, station.population / 60);
45
+    const radius = station.population / 60;
46
+    graphics.drawCircle(station.location.x, station.location.y, radius);
47
+    station.label.x = station.location.x + radius + 1;
48
+    station.label.y = station.location.y + radius + 1;
44 49
   }
45 50
 };
46 51
 
@@ -83,8 +88,8 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
83 88
     // advance train
84 89
     const progress = train.speed / journeyLeft;
85 90
     train.location = new PIXI.Point(
86
-      train.location.x + (Math.abs(train.location.x - train.destination.location.x) * progress),
87
-      train.location.y + (Math.abs(train.location.y - train.destination.location.y) * progress),
91
+      train.location.x + ((train.destination.location.x - train.location.x) * progress),
92
+      train.location.y + ((train.destination.location.y - train.location.y) * progress),
88 93
     );
89 94
   }
90 95
 };
@@ -92,6 +97,8 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
92 97
 const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
93 98
   for (const train of trains) {
94 99
     graphics.drawCircle(train.location.x, train.location.y, 2);
100
+    train.label.x = train.location.x + 1;
101
+    train.label.y = train.location.y + 1;
95 102
   }
96 103
 };
97 104
 
@@ -118,12 +125,12 @@ const run = () => {
118 125
   fpsText.y = 0;
119 126
 
120 127
   // make these const
121
-  let stations = initStations(30);
122
-  let trains = initTrains(15, stations);
128
+  // let stations = initStations(30);
129
+  // let trains = initTrains(15, stations);
123 130
   // let line = new Line(stations, 10);
124 131
 
125
-  stations = initStations(30);
126
-  trains = initTrains(15, stations);
132
+  const stations = initStations(30);
133
+  const trains = initTrains(15, stations);
127 134
 
128 135
   ticker.stop();
129 136
   ticker.add((deltaTime) => {
@@ -141,6 +148,13 @@ const run = () => {
141 148
 
142 149
   app.stage.addChild(graphics);
143 150
   app.stage.addChild(fpsText);
151
+  // Add debug labels
152
+  for (const train of trains) {
153
+    app.stage.addChild(train.label);
154
+  }
155
+  for (const station of stations) {
156
+    app.stage.addChild(station.label);
157
+  }
144 158
   document.body.appendChild(app.view);
145 159
 
146 160
   window.addEventListener('resize', () => {