Skip to content

Commit

Permalink
BUILD #232 enable TS strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkraenz committed Sep 11, 2022
1 parent e802ef7 commit 93d0b22
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 34 deletions.
14 changes: 8 additions & 6 deletions src/anims/PathAnimator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cloneDeep } from 'lodash';
import { GameObjects } from 'phaser';
import { GameObjects, Scene } from 'phaser';
import { CoveredWagon } from '../components/CoveredWagon';
import { DEV } from '../dev-config';
import { ILevel } from '../levels/ILevel';
Expand All @@ -8,7 +8,7 @@ export class PathAnimator {
private wagon?: GameObjects.PathFollower;
private enabled = false; // TODO #208 enable

constructor(private scene, private currentLevel: ILevel) {
constructor(private scene: Scene, private currentLevel: ILevel) {
if (DEV.showPaths) {
this.showTravelPaths();
}
Expand Down Expand Up @@ -39,8 +39,10 @@ export class PathAnimator {
);
const isReverse = !!reverseDirection;
const path = cloneDeep(rightDirection || reverseDirection);
const directedPoints = isReverse ? path.points : path.points.reverse();
if (directedPoints.length === 0) {
const directedPoints = isReverse
? path?.points
: path?.points?.reverse();
if (!directedPoints || directedPoints.length === 0) {
return;
}
const points = directedPoints.map(
Expand Down Expand Up @@ -74,7 +76,7 @@ export class PathAnimator {
if (path.points?.length === 0) {
return;
}
const points = path.points.map(
const points = path.points?.map(
({ x, y }) => new Phaser.Math.Vector2(x, y)
);
const curve = new Phaser.Curves.Spline(points);
Expand All @@ -83,7 +85,7 @@ export class PathAnimator {
graphics.lineStyle(1, 0xffffff, 1);
curve.draw(graphics, 64);
graphics.fillStyle(0x00ff00, 1);
for (const point of points) {
for (const point of points || []) {
graphics.fillCircle(point.x, point.y, 4);
}
});
Expand Down
24 changes: 16 additions & 8 deletions src/components/Balloon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function getBalloonTweenConfig(
return {
targets: image,
x: x2,
ease: (t) => t,
ease: (t: number) => t,
duration: 5000,
yoyo: true,
repeat: -1,
Expand All @@ -90,24 +90,28 @@ function getBalloonTweenConfig(
image.x,
x1,
x2,
(this as CustomTween).movementPattern
(this as unknown as CustomTween).movementPattern
)
);
},
onYoyo() {
// @ts-ignore TODO fix this
this.setTimeScale(Math.max(Math.random(), MIN_BALLOON_SPEED));
(this as CustomTween).movementPattern = random(numberOfCases);
(this as unknown as CustomTween).movementPattern =
random(numberOfCases);
},
onRepeat() {
// @ts-ignore TODO fix this
this.setTimeScale(Math.max(Math.random(), MIN_BALLOON_SPEED));
(this as CustomTween).movementPattern = random(numberOfCases);
(this as unknown as CustomTween).movementPattern =
random(numberOfCases);
},
};
} else {
return {
targets: image,
y: y2,
ease: (t) => t,
ease: (t: number) => t,
duration: 2000,
yoyo: true,
repeat: -1,
Expand All @@ -122,17 +126,21 @@ function getBalloonTweenConfig(
image.y,
y1,
y2,
(this as CustomTween).movementPattern
(this as unknown as CustomTween).movementPattern
)
);
},
onYoyo() {
// @ts-ignore TODO fix this
this.setTimeScale(Math.max(Math.random(), MIN_BALLOON_SPEED));
(this as CustomTween).movementPattern = random(numberOfCases);
(this as unknown as CustomTween).movementPattern =
random(numberOfCases);
},
onRepeat() {
// @ts-ignore TODO fix this
this.setTimeScale(Math.max(Math.random(), MIN_BALLOON_SPEED));
(this as CustomTween).movementPattern = random(numberOfCases);
(this as unknown as CustomTween).movementPattern =
random(numberOfCases);
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/BannerButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TextConfig } from '../styles/Text';
const CLICK_COOLDOWN = 1000;

export class BannerButton extends GameObjects.Sprite {
private buttonText: GameObjects.Text;
private buttonText!: GameObjects.Text;

constructor(
scene: Scene,
Expand Down
2 changes: 1 addition & 1 deletion src/components/CityContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class CityContainer extends Phaser.GameObjects.Container {
}

private defineDrag() {
this.on('drag', (pointer, dragX, dragY) => {
this.on('drag', (_: unknown, dragX: number, dragY: number) => {
this.x = dragX;
this.y = dragY;
this.onTranslation(dragX, dragY);
Expand Down
6 changes: 3 additions & 3 deletions src/components/ImportLevelButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ImportLevelButton extends GameObjects.Text {
input.remove();
};

private async handleFileSelect(event) {
private async handleFileSelect(event: Event) {
const importedLevel = await parseLevelFromJsonUpload(event);
this.afterLevelParsedCb(importedLevel);
}
Expand All @@ -41,9 +41,9 @@ function parseLevelFromJsonUpload(event: any): Promise<ILevel> {
const reader = new FileReader();
reader.onload = (file) => {
try {
const json = JSON.parse(file.target.result as string);
const json = JSON.parse(file.target?.result as string);
resolve(json);
} catch (err) {
} catch (err: any) {
alert(
`Error when trying to parse file as JSON. Original error: ${err.message}`
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ShopContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class ShopContainer extends Phaser.GameObjects.Container {
}

private defineDrag() {
this.on('drag', (pointer, dragX, dragY) => {
this.on('drag', (_: unknown, dragX: number, dragY: number) => {
this.x = dragX;
this.y = dragY;
this.onTranslation(dragX, dragY);
Expand Down
2 changes: 1 addition & 1 deletion src/levels/ILevel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ILevel {
name?: string;
name: string;
cities: ICity[];
shops?: IShop[];
travelPaths: Array<{
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/TitleScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ITitleSceneInitData {
}

export class TitleScene extends Scene {
private backgroundSound: Sound.BaseSound;
private backgroundSound!: Sound.BaseSound;
private fadeInEnabled = true;

constructor() {
Expand Down
25 changes: 16 additions & 9 deletions src/scenes/editorScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ export class EditorScene extends Scene {
}

private defineStartIconEvents() {
this.startIcon.on('drag', (pointer, dragX, dragY) => {
this.startIcon.x = dragX;
this.startIcon.y = dragY;
});
this.startIcon.on(
'drag',
(_: unknown, dragX: number, dragY: number) => {
this.startIcon.x = dragX;
this.startIcon.y = dragY;
}
);
this.startIcon.on('pointerdown', () => {
this.noStartIconDrag = false;
this.startIcon.once('pointerup', () => {
Expand Down Expand Up @@ -249,6 +252,10 @@ export class EditorScene extends Scene {
const nodeW = this.buildings().display.find(
(container) => path.second === container.name
);
if (!nodeV || !nodeW)
throw new Error(
`Node not found for path ${path.first} to ${path.second}`
);
const line = new Phaser.Geom.Line(
nodeV.x,
nodeV.y,
Expand All @@ -265,14 +272,14 @@ export class EditorScene extends Scene {
const numberOfTargets = buildings.length;
const possibleNames = new Array(numberOfTargets + 1)
.fill(1)
.map((undefined, index) => this.concateName(isFor, index));
.map((_, index) => this.concateName(isFor, index));

const nameIsUnused = (name: string) =>
(buildings as NameHolder[]).every(
(building) => building.name !== name
);

return possibleNames.find((name) => nameIsUnused(name));
return possibleNames.find((name) => nameIsUnused(name))!;
}

private concateName(isFor: 'City' | 'Shop', index: number) {
Expand All @@ -294,7 +301,7 @@ export class EditorScene extends Scene {
}
const cityInList = this.level.cities.find(
(container) => city.name === container.name
);
)!;
const newEconomy = { stock: city.stock, production: city.production };
const stockAdd = (summand: number) => {
cityInList.stock += summand;
Expand Down Expand Up @@ -340,7 +347,7 @@ export class EditorScene extends Scene {
}
const shopInList = this.level.shops.find(
(container) => shop.name === container.name
);
)!;
const onTranslation = (x: number, y: number) => {
shopInList.x = x;
shopInList.y = y;
Expand Down Expand Up @@ -479,7 +486,7 @@ export class EditorScene extends Scene {
localStorage.removeItem(STORED_LEVEL_KEY);
localStorage.setItem(STORED_LEVEL_KEY, jsonTryOutLevel);
const textField = document.getElementById('text');
textField.remove();
textField?.remove();
this.scene.remove(this);
}
}
5 changes: 3 additions & 2 deletions src/scenes/mainScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ export class MainScene extends Scene {

private addBalloonForEdge(startCityName: string, targetCityName: string) {
const allNodes = getNodes(this.graph);
const startCity = allNodes.find((city) => city.name === startCityName);
// always defined by construction
const startCity = allNodes.find((city) => city.name === startCityName)!;
const targetCity = allNodes.find(
(city) => city.name === targetCityName
);
)!;
new Balloon(this, startCity, targetCity);
new DottedLine(this, startCity, targetCity);
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2017",
"module": "CommonJs",
"strict": false,
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
Expand Down

0 comments on commit 93d0b22

Please sign in to comment.