Browse Source

Use image sprite for trains

Tyler Hallada 6 years ago
parent
commit
35b4ef25bb
10 changed files with 66 additions and 15 deletions
  1. 10 0
      package-lock.json
  2. 1 0
      package.json
  3. 7 2
      src/Line.ts
  4. 6 2
      src/Station.ts
  5. 11 2
      src/Train.ts
  6. BIN
      src/node.png
  7. 21 7
      src/transport.ts
  8. 5 2
      tsconfig.json
  9. 1 0
      typings/custom.d.ts
  10. 4 0
      webpack.config.js

+ 10 - 0
package-lock.json

@@ -4753,6 +4753,16 @@
4753 4753
         "object-assign": "4.1.1"
4754 4754
       }
4755 4755
     },
4756
+    "file-loader": {
4757
+      "version": "1.1.11",
4758
+      "resolved": "https://registry.npmjs.org/file-loader/-/file-loader-1.1.11.tgz",
4759
+      "integrity": "sha512-TGR4HU7HUsGg6GCOPJnFk06RhWgEWFLAGWiT6rcD+GRC2keU3s9RGJ+b3Z6/U73jwwNb2gKLJ7YCrp+jvU4ALg==",
4760
+      "dev": true,
4761
+      "requires": {
4762
+        "loader-utils": "1.1.0",
4763
+        "schema-utils": "0.4.5"
4764
+      }
4765
+    },
4756 4766
     "filename-regex": {
4757 4767
       "version": "2.0.1",
4758 4768
       "resolved": "https://registry.npmjs.org/filename-regex/-/filename-regex-2.0.1.tgz",

+ 1 - 0
package.json

@@ -30,6 +30,7 @@
30 30
     "eslint-plugin-promise": "^3.5.0",
31 31
     "eslint-plugin-standard": "^2.1.1",
32 32
     "extract-text-webpack-plugin": "^4.0.0-beta.0",
33
+    "file-loader": "^1.1.11",
33 34
     "gh-pages": "^1.1.0",
34 35
     "html-webpack-plugin": "^3.1.0",
35 36
     "style-loader": "^0.20.3",

+ 7 - 2
src/Line.ts

@@ -16,8 +16,13 @@ export default class Line {
16 16
   public stations: Station[];
17 17
   public color: tinycolorInstance;
18 18
 
19
-  constructor(stations: Station[], start: PIXI.Point, startDirection: Direction,
20
-              numStations: number, color: tinycolorInstance) {
19
+  constructor(
20
+    stations: Station[],
21
+    start: PIXI.Point,
22
+    startDirection: Direction,
23
+    numStations: number,
24
+    color: tinycolorInstance
25
+  ) {
21 26
     this.color = color;
22 27
     this.stations = [];
23 28
     let stationsLeft = stations.slice();

+ 6 - 2
src/Station.ts

@@ -70,8 +70,12 @@ export default class Station {
70 70
   public label: PIXI.Text;
71 71
   public color: tinycolorInstance;
72 72
 
73
-  constructor(location: PIXI.Point, population: number, color: tinycolorInstance,
74
-              connections?: Station[]) {
73
+  constructor(
74
+    location: PIXI.Point,
75
+    population: number,
76
+    color: tinycolorInstance,
77
+    connections?: Station[],
78
+  ) {
75 79
     this.location = location;
76 80
     this.population = population;
77 81
     this.color = color;

+ 11 - 2
src/Train.ts

@@ -13,9 +13,16 @@ export default class Train {
13 13
   public id: number;
14 14
   public label: PIXI.Text;
15 15
   public color: tinycolorInstance;
16
+  public sprite: PIXI.Sprite;
16 17
 
17
-  constructor(location: PIXI.Point, speed: number, passengers: number, origin: Station,
18
-              destination: Station, color: tinycolorInstance) {
18
+  constructor(
19
+    location: PIXI.Point,
20
+    speed: number,
21
+    passengers: number,
22
+    origin: Station,
23
+    destination: Station,
24
+    color: tinycolorInstance
25
+  ) {
19 26
     this.location = location;
20 27
     this.speed = speed;
21 28
     this.origin = origin;
@@ -23,6 +30,8 @@ export default class Train {
23 30
     this.passengers = passengers;
24 31
     this.color = color;
25 32
 
33
+    this.sprite = new PIXI.Sprite(PIXI.loader.resources.nodeImg.texture);
34
+
26 35
     // for debugging
27 36
     trainCount += 1;
28 37
     this.id = trainCount;

BIN
src/node.png


+ 21 - 7
src/transport.ts

@@ -8,8 +8,11 @@ import Train from './Train';
8 8
 import { distance, pointsAlmostEqual, pointsEqual, randomInt, randomPoint,
9 9
          rangeMap, weightedRandom } from './utils';
10 10
 
11
+import * as imgNode from './node.png';
11 12
 import './style.css';
12 13
 
14
+const NODE_RES = 100;
15
+
13 16
 const MAX_SPEED = 10.0;
14 17
 const ACCELERATION = 0.025;
15 18
 const APPROACH_DISTANCE = 3.0;
@@ -124,12 +127,19 @@ const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
124 127
 
125 128
 const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
126 129
   for (const train of trains) {
127
-    graphics.beginFill(parseInt(train.color.toHex(), 16), 0.8);
128
-    graphics.drawCircle(train.location.x, train.location.y,
129
-                        rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5));
130
-    graphics.endFill();
131
-    train.label.x = train.location.x + 1;
132
-    train.label.y = train.location.y + 1;
130
+    // graphics.beginFill(parseInt(train.color.toHex(), 16), 0.8);
131
+    // graphics.drawCircle(train.location.x, train.location.y,
132
+                        // rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5));
133
+    // graphics.endFill();
134
+    const trainSize = rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5);
135
+    const scale = trainSize / NODE_RES;
136
+    train.sprite.x = train.location.x;
137
+    train.sprite.y = train.location.y;
138
+    train.sprite.scale.x = scale;
139
+    train.sprite.scale.y = scale;
140
+    train.sprite.tint = parseInt(train.color.toHex(), 16);
141
+    train.label.x = train.location.x + scale + 1;
142
+    train.label.y = train.location.y + scale + 1;
133 143
   }
134 144
 };
135 145
 
@@ -198,6 +208,10 @@ const run = () => {
198 208
 
199 209
   app.stage.addChild(graphics);
200 210
   app.stage.addChild(fpsText);
211
+  // add train sprites
212
+  for (const train of trains) {
213
+    app.stage.addChild(train.sprite);
214
+  }
201 215
   // Add debug labels
202 216
   for (const train of trains) {
203 217
     app.stage.addChild(train.label);
@@ -212,4 +226,4 @@ const run = () => {
212 226
   });
213 227
 };
214 228
 
215
-run();
229
+PIXI.loader.add('nodeImg', imgNode).load(run);

+ 5 - 2
tsconfig.json

@@ -4,8 +4,11 @@
4 4
     "sourceMap": true,
5 5
     "noImplicitAny": true,
6 6
     "module": "es6",
7
-    "target": "es5",
7
+    "target": "es6",
8 8
     "jsx": "react",
9 9
     "allowJs": true
10
-  }
10
+  },
11
+  "include": [
12
+    "typings"
13
+  ]
11 14
 }

+ 1 - 0
typings/custom.d.ts

@@ -0,0 +1 @@
1
+declare module '*.png';

+ 4 - 0
webpack.config.js

@@ -43,6 +43,10 @@ module.exports = {
43 43
           })
44 44
           : ['style-loader', 'css-loader'],
45 45
       },
46
+      {
47
+        test: /\.png$/,
48
+        use: 'file-loader',
49
+      },
46 50
     ],
47 51
   },
48 52
   resolve: {