diff --git a/src/anims/PathAnimator.ts b/src/anims/PathAnimator.ts index 57ba8f6..08e12c5 100644 --- a/src/anims/PathAnimator.ts +++ b/src/anims/PathAnimator.ts @@ -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'; @@ -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(); } @@ -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( @@ -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); @@ -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); } }); diff --git a/src/components/Balloon.ts b/src/components/Balloon.ts index a84187f..554ce4b 100644 --- a/src/components/Balloon.ts +++ b/src/components/Balloon.ts @@ -75,7 +75,7 @@ function getBalloonTweenConfig( return { targets: image, x: x2, - ease: (t) => t, + ease: (t: number) => t, duration: 5000, yoyo: true, repeat: -1, @@ -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, @@ -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); }, }; } diff --git a/src/components/BannerButton.ts b/src/components/BannerButton.ts index 0b25630..0693ed0 100644 --- a/src/components/BannerButton.ts +++ b/src/components/BannerButton.ts @@ -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, diff --git a/src/components/CityContainer.ts b/src/components/CityContainer.ts index e61b45d..d404144 100644 --- a/src/components/CityContainer.ts +++ b/src/components/CityContainer.ts @@ -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); diff --git a/src/components/ImportLevelButton.ts b/src/components/ImportLevelButton.ts index 0fdb37a..966fc5a 100644 --- a/src/components/ImportLevelButton.ts +++ b/src/components/ImportLevelButton.ts @@ -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); } @@ -41,9 +41,9 @@ function parseLevelFromJsonUpload(event: any): Promise { 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}` ); diff --git a/src/components/ShopContainer.ts b/src/components/ShopContainer.ts index 608d3bd..2bb2bc5 100644 --- a/src/components/ShopContainer.ts +++ b/src/components/ShopContainer.ts @@ -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); diff --git a/src/levels/ILevel.ts b/src/levels/ILevel.ts index dcfe894..8fabd41 100644 --- a/src/levels/ILevel.ts +++ b/src/levels/ILevel.ts @@ -1,5 +1,5 @@ export interface ILevel { - name?: string; + name: string; cities: ICity[]; shops?: IShop[]; travelPaths: Array<{ diff --git a/src/scenes/TitleScene.ts b/src/scenes/TitleScene.ts index b5737e4..e6d0fea 100644 --- a/src/scenes/TitleScene.ts +++ b/src/scenes/TitleScene.ts @@ -11,7 +11,7 @@ export interface ITitleSceneInitData { } export class TitleScene extends Scene { - private backgroundSound: Sound.BaseSound; + private backgroundSound!: Sound.BaseSound; private fadeInEnabled = true; constructor() { diff --git a/src/scenes/editorScene.ts b/src/scenes/editorScene.ts index 5ece016..f7b9762 100644 --- a/src/scenes/editorScene.ts +++ b/src/scenes/editorScene.ts @@ -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', () => { @@ -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, @@ -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) { @@ -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; @@ -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; @@ -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); } } diff --git a/src/scenes/mainScene.ts b/src/scenes/mainScene.ts index dd919c7..e989e44 100644 --- a/src/scenes/mainScene.ts +++ b/src/scenes/mainScene.ts @@ -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); } diff --git a/tsconfig.json b/tsconfig.json index b7d3c6f..65b7d5e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "ES2017", "module": "CommonJs", - "strict": false, + "strict": true, "noImplicitReturns": true, "noUnusedLocals": false, "noUnusedParameters": false,