Browse Source

Add TypeScript, generate stations and a line

Tyler Hallada 6 years ago
parent
commit
edae3f76f4
12 changed files with 430 additions and 37 deletions
  1. 185 0
      package-lock.json
  2. 8 1
      package.json
  3. 42 0
      src/Line.ts
  4. 11 0
      src/Station.ts
  5. 23 0
      src/Train.ts
  6. 0 30
      src/transport.js
  7. 106 0
      src/transport.ts
  8. 0 4
      src/utils.js
  9. 16 0
      src/utils.ts
  10. 11 0
      tsconfig.json
  11. 11 0
      tslint.json
  12. 17 2
      webpack.config.js

+ 185 - 0
package-lock.json

@@ -375,6 +375,17 @@
375 375
         "@babel/plugin-syntax-async-generators": "7.0.0-beta.42"
376 376
       }
377 377
     },
378
+    "@babel/plugin-proposal-class-properties": {
379
+      "version": "7.0.0-beta.42",
380
+      "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.0.0-beta.42.tgz",
381
+      "integrity": "sha512-VXRmo/t2nAVciXxEgPTAfcUBXj0UXNPIvX2aj3lzHL51N+uh+rtgsIF0nuZwGE4u89WvBDH66yjAu60Ra674kw==",
382
+      "dev": true,
383
+      "requires": {
384
+        "@babel/helper-function-name": "7.0.0-beta.42",
385
+        "@babel/helper-plugin-utils": "7.0.0-beta.42",
386
+        "@babel/plugin-syntax-class-properties": "7.0.0-beta.42"
387
+      }
388
+    },
378 389
     "@babel/plugin-proposal-object-rest-spread": {
379 390
       "version": "7.0.0-beta.42",
380 391
       "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.0.0-beta.42.tgz",
@@ -446,6 +457,15 @@
446 457
         "@babel/helper-plugin-utils": "7.0.0-beta.42"
447 458
       }
448 459
     },
460
+    "@babel/plugin-syntax-class-properties": {
461
+      "version": "7.0.0-beta.42",
462
+      "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-class-properties/-/plugin-syntax-class-properties-7.0.0-beta.42.tgz",
463
+      "integrity": "sha512-nG0XCeuni6GgjxOqtxVtnfSoRFeXdqY6cja83cmFpC1klw8f6XShbeDmK7xX1mUYBHkEqLTDurlX+fuua9siCg==",
464
+      "dev": true,
465
+      "requires": {
466
+        "@babel/helper-plugin-utils": "7.0.0-beta.42"
467
+      }
468
+    },
449 469
     "@babel/plugin-syntax-object-rest-spread": {
450 470
       "version": "7.0.0-beta.42",
451 471
       "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.0.0-beta.42.tgz",
@@ -927,12 +947,39 @@
927 947
         }
928 948
       }
929 949
     },
950
+    "@fimbul/bifrost": {
951
+      "version": "0.6.0",
952
+      "resolved": "https://registry.npmjs.org/@fimbul/bifrost/-/bifrost-0.6.0.tgz",
953
+      "integrity": "sha512-BJ19rjnFFCeopEhbyK2Chg3Tq+o5xkjd6dtKxmFhfjwLH1Il2G7Ha4Jel2hpbyZL2Fh9/vrM9U0bpkANAL3pjA==",
954
+      "dev": true,
955
+      "requires": {
956
+        "@fimbul/ymir": "0.6.0",
957
+        "get-caller-file": "1.0.2",
958
+        "tslib": "1.9.0"
959
+      }
960
+    },
961
+    "@fimbul/ymir": {
962
+      "version": "0.6.0",
963
+      "resolved": "https://registry.npmjs.org/@fimbul/ymir/-/ymir-0.6.0.tgz",
964
+      "integrity": "sha512-iyh/8OiZlzjlPytdjdodA86d38YtRL0sSAx169SMgqP4dsouH2rtctf4Nrg4FYvWoG0e9y9XT3iWL+mjTgYNRw==",
965
+      "dev": true,
966
+      "requires": {
967
+        "inversify": "4.11.1",
968
+        "reflect-metadata": "0.1.12",
969
+        "tslib": "1.9.0"
970
+      }
971
+    },
930 972
     "@sindresorhus/is": {
931 973
       "version": "0.7.0",
932 974
       "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.7.0.tgz",
933 975
       "integrity": "sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow==",
934 976
       "dev": true
935 977
     },
978
+    "@types/pixi.js": {
979
+      "version": "4.7.2",
980
+      "resolved": "https://registry.npmjs.org/@types/pixi.js/-/pixi.js-4.7.2.tgz",
981
+      "integrity": "sha512-ybrqVdncNCa81fCYCqxz/CISyMbXl8usszNv0mwdeYDyfDqmemQHJtf4GtduHva+3suhItPc9Akr/WfV19zWiQ=="
982
+    },
936 983
     "accepts": {
937 984
       "version": "1.3.5",
938 985
       "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.5.tgz",
@@ -6801,6 +6848,12 @@
6801 6848
         "loose-envify": "1.3.1"
6802 6849
       }
6803 6850
     },
6851
+    "inversify": {
6852
+      "version": "4.11.1",
6853
+      "resolved": "https://registry.npmjs.org/inversify/-/inversify-4.11.1.tgz",
6854
+      "integrity": "sha512-9bs/36crPdTSOCcoomHMb96s+B8W0+2c9dHFP/Srv9ZQaPnUvsMgzmMHfgVECqfHVUIW+M5S7SYOjoig8khWuQ==",
6855
+      "dev": true
6856
+    },
6804 6857
     "invert-kv": {
6805 6858
       "version": "1.0.0",
6806 6859
       "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-1.0.0.tgz",
@@ -10156,6 +10209,12 @@
10156 10209
         }
10157 10210
       }
10158 10211
     },
10212
+    "reflect-metadata": {
10213
+      "version": "0.1.12",
10214
+      "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.12.tgz",
10215
+      "integrity": "sha512-n+IyV+nGz3+0q3/Yf1ra12KpCyi001bi4XFxSjbiWWjfqb52iTTtpGXmCCAOWWIAn9KEuFZKGqBERHmrtScZ3A==",
10216
+      "dev": true
10217
+    },
10159 10218
     "regenerate": {
10160 10219
       "version": "1.3.3",
10161 10220
       "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.3.3.tgz",
@@ -11509,6 +11568,126 @@
11509 11568
       "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=",
11510 11569
       "dev": true
11511 11570
     },
11571
+    "ts-loader": {
11572
+      "version": "4.1.0",
11573
+      "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-4.1.0.tgz",
11574
+      "integrity": "sha512-R4VEE0SZhU8gLa9Aayg2XOvpVjXtbB7KMLXs4He0xr92DM5G12i76IWd+lMLfmCz66Ztr2XFvDNvMAymHJGIqg==",
11575
+      "dev": true,
11576
+      "requires": {
11577
+        "chalk": "2.3.2",
11578
+        "enhanced-resolve": "4.0.0",
11579
+        "loader-utils": "1.1.0",
11580
+        "micromatch": "3.1.10",
11581
+        "semver": "5.5.0"
11582
+      }
11583
+    },
11584
+    "tslib": {
11585
+      "version": "1.9.0",
11586
+      "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.0.tgz",
11587
+      "integrity": "sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ==",
11588
+      "dev": true
11589
+    },
11590
+    "tslint": {
11591
+      "version": "5.9.1",
11592
+      "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.9.1.tgz",
11593
+      "integrity": "sha1-ElX4ej/1frCw4fDmEKi0dIBGya4=",
11594
+      "dev": true,
11595
+      "requires": {
11596
+        "babel-code-frame": "6.26.0",
11597
+        "builtin-modules": "1.1.1",
11598
+        "chalk": "2.3.2",
11599
+        "commander": "2.13.0",
11600
+        "diff": "3.5.0",
11601
+        "glob": "7.1.2",
11602
+        "js-yaml": "3.11.0",
11603
+        "minimatch": "3.0.4",
11604
+        "resolve": "1.6.0",
11605
+        "semver": "5.5.0",
11606
+        "tslib": "1.9.0",
11607
+        "tsutils": "2.25.0"
11608
+      }
11609
+    },
11610
+    "tslint-config-airbnb": {
11611
+      "version": "5.8.0",
11612
+      "resolved": "https://registry.npmjs.org/tslint-config-airbnb/-/tslint-config-airbnb-5.8.0.tgz",
11613
+      "integrity": "sha512-4fTcxL/3Xbct8BIt1jjZvp9EavqwUO7ND9mpRsdHmvdAYGCbGdaIaUg9NCB6VXLDtTBTR5XT/tEU3/jBN/JPAw==",
11614
+      "dev": true,
11615
+      "requires": {
11616
+        "tslint-consistent-codestyle": "1.13.0",
11617
+        "tslint-eslint-rules": "4.1.1",
11618
+        "tslint-microsoft-contrib": "5.0.3"
11619
+      }
11620
+    },
11621
+    "tslint-consistent-codestyle": {
11622
+      "version": "1.13.0",
11623
+      "resolved": "https://registry.npmjs.org/tslint-consistent-codestyle/-/tslint-consistent-codestyle-1.13.0.tgz",
11624
+      "integrity": "sha512-7fcstphFz9Rw2+SAe32VjtnQEHYEQVSGgEOea9hN/8JMJQGpGkxvVbqxhsXew9vkRtvPQuoj1pQoZ5Eadp4B6A==",
11625
+      "dev": true,
11626
+      "requires": {
11627
+        "@fimbul/bifrost": "0.6.0",
11628
+        "tslib": "1.9.0",
11629
+        "tsutils": "2.25.0"
11630
+      }
11631
+    },
11632
+    "tslint-eslint-rules": {
11633
+      "version": "4.1.1",
11634
+      "resolved": "https://registry.npmjs.org/tslint-eslint-rules/-/tslint-eslint-rules-4.1.1.tgz",
11635
+      "integrity": "sha1-fDDniC8mvCdr/5HSOEl1xp2viLo=",
11636
+      "dev": true,
11637
+      "requires": {
11638
+        "doctrine": "0.7.2",
11639
+        "tslib": "1.9.0",
11640
+        "tsutils": "1.9.1"
11641
+      },
11642
+      "dependencies": {
11643
+        "doctrine": {
11644
+          "version": "0.7.2",
11645
+          "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-0.7.2.tgz",
11646
+          "integrity": "sha1-fLhgNZujvpDgQLJrcpzkv6ZUxSM=",
11647
+          "dev": true,
11648
+          "requires": {
11649
+            "esutils": "1.1.6",
11650
+            "isarray": "0.0.1"
11651
+          }
11652
+        },
11653
+        "esutils": {
11654
+          "version": "1.1.6",
11655
+          "resolved": "https://registry.npmjs.org/esutils/-/esutils-1.1.6.tgz",
11656
+          "integrity": "sha1-wBzKqa5LiXxtDD4hCuUvPHqEQ3U=",
11657
+          "dev": true
11658
+        },
11659
+        "isarray": {
11660
+          "version": "0.0.1",
11661
+          "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
11662
+          "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=",
11663
+          "dev": true
11664
+        },
11665
+        "tsutils": {
11666
+          "version": "1.9.1",
11667
+          "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-1.9.1.tgz",
11668
+          "integrity": "sha1-ufmrROVa+WgYMdXyjQrur1x1DLA=",
11669
+          "dev": true
11670
+        }
11671
+      }
11672
+    },
11673
+    "tslint-microsoft-contrib": {
11674
+      "version": "5.0.3",
11675
+      "resolved": "https://registry.npmjs.org/tslint-microsoft-contrib/-/tslint-microsoft-contrib-5.0.3.tgz",
11676
+      "integrity": "sha512-5AnfTGlfpUzpRHLmoojPBKFTTmbjnwgdaTHMdllausa4GBPya5u36i9ddrTX4PhetGZvd4JUYIpAmgHqVnsctg==",
11677
+      "dev": true,
11678
+      "requires": {
11679
+        "tsutils": "2.25.0"
11680
+      }
11681
+    },
11682
+    "tsutils": {
11683
+      "version": "2.25.0",
11684
+      "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.25.0.tgz",
11685
+      "integrity": "sha512-SPgUlOAUAe6fCyPi0QR4U0jRuDsHHKvzIR6/hHd0YR0bb8MzeLJgCagkPSmZeJjWImnpJ0xq6XHa9goTvMBBCQ==",
11686
+      "dev": true,
11687
+      "requires": {
11688
+        "tslib": "1.9.0"
11689
+      }
11690
+    },
11512 11691
     "tty-browserify": {
11513 11692
       "version": "0.0.0",
11514 11693
       "resolved": "https://registry.npmjs.org/tty-browserify/-/tty-browserify-0.0.0.tgz",
@@ -11540,6 +11719,12 @@
11540 11719
       "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=",
11541 11720
       "dev": true
11542 11721
     },
11722
+    "typescript": {
11723
+      "version": "2.8.1",
11724
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.1.tgz",
11725
+      "integrity": "sha512-Ao/f6d/4EPLq0YwzsQz8iXflezpTkQzqAyenTiw4kCUGr1uPiFLC3+fZ+gMZz6eeI/qdRUqvC+HxIJzUAzEFdg==",
11726
+      "dev": true
11727
+    },
11543 11728
     "uglify-es": {
11544 11729
       "version": "3.3.9",
11545 11730
       "resolved": "https://registry.npmjs.org/uglify-es/-/uglify-es-3.3.9.tgz",

+ 8 - 1
package.json

@@ -7,7 +7,7 @@
7 7
     "start": "NODE_ENV=development ./node_modules/.bin/webpack-dev-server --mode development",
8 8
     "build": "NODE_ENV=production ./node_modules/.bin/webpack --mode production",
9 9
     "deploy": "./node_modules/.bin/gh-pages -d dist",
10
-    "lint": "./node_modules/.bin/eslint src/",
10
+    "lint": "./node_modules/.bin/tslint src/",
11 11
     "test": "echo \"Error: no test specified\" && exit 1"
12 12
   },
13 13
   "repository": {
@@ -18,6 +18,8 @@
18 18
   "license": "ISC",
19 19
   "devDependencies": {
20 20
     "@babel/core": "^7.0.0-beta.42",
21
+    "@babel/plugin-proposal-class-properties": "^7.0.0-beta.42",
22
+    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.42",
21 23
     "@babel/preset-env": "^7.0.0-beta.42",
22 24
     "babel-loader": "^8.0.0-beta.2",
23 25
     "css-loader": "^0.28.11",
@@ -31,11 +33,16 @@
31 33
     "gh-pages": "^1.1.0",
32 34
     "html-webpack-plugin": "^3.1.0",
33 35
     "style-loader": "^0.20.3",
36
+    "ts-loader": "^4.1.0",
37
+    "tslint": "^5.9.1",
38
+    "tslint-config-airbnb": "^5.8.0",
39
+    "typescript": "^2.8.1",
34 40
     "webpack": "^4.4.1",
35 41
     "webpack-cli": "^2.0.13",
36 42
     "webpack-dev-server": "^3.1.1"
37 43
   },
38 44
   "dependencies": {
45
+    "@types/pixi.js": "^4.7.2",
39 46
     "pixi.js": "^4.7.1"
40 47
   }
41 48
 }

+ 42 - 0
src/Line.ts

@@ -0,0 +1,42 @@
1
+import Station from './Station';
2
+import { distance, randomInt, randomPoint } from './utils';
3
+
4
+const largestStation = (stations: Station[]): Station => {
5
+  let largest: Station = null;
6
+  for (const station of stations) {
7
+    if (largest === null || station.population > largest.population) {
8
+      largest = station;
9
+    }
10
+  }
11
+  return largest;
12
+};
13
+
14
+const stationsWithinRadius = (stations: Station[], point: PIXI.Point,
15
+                              radius: number): Station[] => (
16
+  stations.filter(station => distance(point, station.location) <= radius)
17
+);
18
+
19
+const closestStations = (stations: Station[], point: PIXI.Point, num: number): Station[] => {
20
+  // bleh, i'm done
21
+  return stations;
22
+};
23
+
24
+export default class Line {
25
+  public stations: Station[];
26
+
27
+  constructor(stations: Station[], numStations: number) {
28
+    this.stations = [];
29
+    let stationsLeft = stations;
30
+    let largest = largestStation(stationsLeft);
31
+    stationsLeft = stationsLeft.filter(s => s !== largest);
32
+    this.stations.push(largest);
33
+    while (this.stations.length < numStations) {
34
+      largest = largestStation(stationsWithinRadius(stationsLeft, largest.location, 500));
35
+      if (largest === null) {
36
+        break;
37
+      }
38
+      stationsLeft = stationsLeft.filter(s => s !== largest);
39
+      this.stations.push(largest);
40
+    }
41
+  }
42
+}

+ 11 - 0
src/Station.ts

@@ -0,0 +1,11 @@
1
+export default class Station {
2
+  public location: PIXI.Point;
3
+  public population: number;
4
+  public connections: Station[];
5
+
6
+  constructor(location: PIXI.Point, population: number, connections?: Station[]) {
7
+    this.location = location;
8
+    this.population = population;
9
+    this.connections = connections;
10
+  }
11
+}

+ 23 - 0
src/Train.ts

@@ -0,0 +1,23 @@
1
+import Station from './Station';
2
+
3
+export default class Train {
4
+  public location: PIXI.Point;
5
+  public speed: number;
6
+  public origin: Station;
7
+  public destination: Station;
8
+  public passengers: number;
9
+
10
+  constructor(location: PIXI.Point, speed: number, passengers: number, origin: Station,
11
+              destination: Station) {
12
+    this.location = location;
13
+    this.speed = speed;
14
+    this.origin = origin;
15
+    this.destination = destination;
16
+    this.passengers = passengers;
17
+  }
18
+
19
+  public boardPassengers() {
20
+    if (this.location === this.origin.location) { // about to leave a station
21
+    }
22
+  }
23
+}

+ 0 - 30
src/transport.js

@@ -1,30 +0,0 @@
1
-import * as PIXI from 'pixi.js';
2
-import { randomInt } from './utils';
3
-import './style.css';
4
-
5
-const app = new PIXI.Application(window.innerWidth, window.innerHeight);
6
-const ticker = new PIXI.ticker.Ticker();
7
-const graphics = new PIXI.Graphics();
8
-const fpsText = new PIXI.Text('', { fontSize: '25px', fontFamily: 'monospace', fill: 'yellow' });
9
-
10
-fpsText.anchor = new PIXI.Point(1, 0);
11
-fpsText.x = window.innerWidth - 1;
12
-fpsText.y = 0;
13
-
14
-ticker.stop();
15
-ticker.add((deltaTime) => {
16
-  fpsText.setText(Math.round(ticker.FPS));
17
-  graphics.lineStyle(1, 0xaeaeae, 1);
18
-
19
-  graphics.moveTo(randomInt(9, window.innerWidth), randomInt(0, window.innerHeight));
20
-  graphics.lineTo(randomInt(9, window.innerWidth), randomInt(0, window.innerHeight));
21
-});
22
-ticker.start();
23
-
24
-app.stage.addChild(graphics);
25
-app.stage.addChild(fpsText);
26
-document.body.appendChild(app.view);
27
-
28
-window.addEventListener('resize', () => {
29
-  app.renderer.resize(window.innerWidth, window.innerHeight);
30
-});

+ 106 - 0
src/transport.ts

@@ -0,0 +1,106 @@
1
+import * as PIXI from 'pixi.js';
2
+import Line from './Line';
3
+import Station from './Station';
4
+import Train from './Train';
5
+import { distance, randomInt, randomPoint } from './utils';
6
+
7
+import './style.css';
8
+
9
+const isPointDistant = (point: PIXI.Point, stations: Station[], minDistance: number): boolean => {
10
+  for (const station of stations) {
11
+    if (distance(point, station.location) < minDistance) {
12
+      return false;
13
+    }
14
+  }
15
+  return true;
16
+};
17
+
18
+const randomDistantPoint = (stations: Station[], minDistance: number): PIXI.Point | null => {
19
+  let tries = 100;
20
+  while (tries > 0) {
21
+    const point = randomPoint();
22
+    if (isPointDistant(point, stations, minDistance)) {
23
+      return point;
24
+    }
25
+    tries -= 1;
26
+  }
27
+  return null;
28
+};
29
+
30
+const initStations = (numStations: number): Station[] => {
31
+  const stations: Station[] = [];
32
+  for (let i = 0; i < numStations; i += 1) {
33
+    stations.push(new Station(randomDistantPoint(stations, 30), randomInt(100, 1000)));
34
+  }
35
+  return stations;
36
+};
37
+
38
+const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
39
+  for (const station of stations) {
40
+    graphics.drawCircle(station.location.x, station.location.y, station.population / 60);
41
+  }
42
+};
43
+
44
+const initTrains = (numTrains: number, stations: Station[]): Train[] => {
45
+  const trains = [];
46
+  for (let i = 0; i < numTrains; i += 1) {
47
+    const originStation = stations[Math.floor(Math.random() * stations.length)];
48
+    const destStation = stations[Math.floor(Math.random() * stations.length)];
49
+    trains.push(new Train(originStation.location, 0, 0, originStation, destStation));
50
+  }
51
+  return trains;
52
+};
53
+
54
+const drawLine = (line: Line, graphics: PIXI.Graphics) => {
55
+  const start = line.stations[0].location;
56
+  graphics.moveTo(start.x, start.y);
57
+  for (const station of line.stations.slice(1)) {
58
+    graphics.lineTo(station.location.x, station.location.y);
59
+  }
60
+};
61
+
62
+const run = () => {
63
+  const app = new PIXI.Application({
64
+    antialias: true,
65
+    height: window.innerHeight,
66
+    width: window.innerWidth,
67
+  });
68
+  const ticker = new PIXI.ticker.Ticker();
69
+  const graphics = new PIXI.Graphics();
70
+  const fpsText = new PIXI.Text('', { fontSize: '25px', fontFamily: 'monospace', fill: 'yellow' });
71
+
72
+  fpsText.anchor = new PIXI.ObservablePoint(null, 0, 1);
73
+  fpsText.x = window.innerWidth;
74
+  fpsText.y = 0;
75
+
76
+  // make these const
77
+  let stations = initStations(30);
78
+  let trains = initTrains(15, stations);
79
+  let line = new Line(stations, 10);
80
+
81
+  stations = initStations(30);
82
+  trains = initTrains(15, stations);
83
+  line = new Line(stations, 10);
84
+
85
+  ticker.stop();
86
+  ticker.add((deltaTime) => {
87
+
88
+    graphics.clear();
89
+    fpsText.text = `${Math.round(ticker.FPS)}`;
90
+    graphics.lineStyle(1, 0xaeaeae, 1);
91
+
92
+    drawStations(stations, graphics);
93
+    drawLine(line, graphics);
94
+  });
95
+  ticker.start();
96
+
97
+  app.stage.addChild(graphics);
98
+  app.stage.addChild(fpsText);
99
+  document.body.appendChild(app.view);
100
+
101
+  window.addEventListener('resize', () => {
102
+    app.renderer.resize(window.innerWidth, window.innerHeight);
103
+  });
104
+};
105
+
106
+run();

+ 0 - 4
src/utils.js

@@ -1,4 +0,0 @@
1
-export const randomInt = (min, max) => (
2
-  // inclusive of min and max
3
-  Math.floor(Math.random() * (max - (min + 1))) + min
4
-);

+ 16 - 0
src/utils.ts

@@ -0,0 +1,16 @@
1
+import * as PIXI from 'pixi.js';
2
+
3
+export const randomInt = (min: number, max: number): number => (
4
+  // inclusive of min and max
5
+  Math.floor(Math.random() * (max - (min + 1))) + min
6
+);
7
+
8
+export const randomPoint = () => (
9
+  new PIXI.Point(randomInt(0, window.innerWidth), randomInt(0, window.innerHeight))
10
+);
11
+
12
+export const distance = (pointA: PIXI.Point, pointB: PIXI.Point): number => {
13
+  const distX = pointA.x - pointB.x;
14
+  const distY = pointA.y - pointB.y;
15
+  return Math.sqrt((distX * distX) + (distY * distY));
16
+};

+ 11 - 0
tsconfig.json

@@ -0,0 +1,11 @@
1
+{
2
+  "compilerOptions": {
3
+    "outDir": "./dist/",
4
+    "sourceMap": true,
5
+    "noImplicitAny": true,
6
+    "module": "es6",
7
+    "target": "es5",
8
+    "jsx": "react",
9
+    "allowJs": true
10
+  }
11
+}

+ 11 - 0
tslint.json

@@ -0,0 +1,11 @@
1
+{
2
+    "defaultSeverity": "error",
3
+    "extends": [
4
+        "tslint:recommended",
5
+        "tslint-config-airbnb",
6
+        "tslint-eslint-rules"
7
+    ],
8
+    "jsRules": {},
9
+    "rules": {},
10
+    "rulesDirectory": []
11
+}

+ 17 - 2
webpack.config.js

@@ -5,7 +5,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
5 5
 const env = process.env.NODE_ENV;
6 6
 
7 7
 module.exports = {
8
-  entry: './src/transport.js',
8
+  entry: './src/transport.ts',
9 9
   output: {
10 10
     filename: 'transport.js',
11 11
     path: path.resolve(__dirname, 'dist'),
@@ -18,10 +18,22 @@ module.exports = {
18 18
         use: {
19 19
           loader: 'babel-loader',
20 20
           options: {
21
-            presets: ['@babel/preset-env'],
21
+            presets: [
22
+              '@babel/preset-env',
23
+            ],
24
+            plugins: [
25
+              '@babel/proposal-class-properties',
26
+              '@babel/proposal-object-rest-spread',
27
+            ],
28
+            cacheDirectory: env === 'development',
22 29
           },
23 30
         },
24 31
       },
32
+      {
33
+        test: /\.tsx?$/,
34
+        use: 'ts-loader',
35
+        exclude: /node_modules/,
36
+      },
25 37
       {
26 38
         test: /\.css$/,
27 39
         use: env === 'production'
@@ -33,6 +45,9 @@ module.exports = {
33 45
       },
34 46
     ],
35 47
   },
48
+  resolve: {
49
+    extensions: ['.tsx', '.ts', '.js'],
50
+  },
36 51
   plugins: [
37 52
     new HtmlWebpackPlugin({
38 53
       template: path.join(__dirname, 'index.html'),