diff --git a/src/client/graphics/layers/UnitLayer.ts b/src/client/graphics/layers/UnitLayer.ts index 4ad9ebea4..51e05a387 100644 --- a/src/client/graphics/layers/UnitLayer.ts +++ b/src/client/graphics/layers/UnitLayer.ts @@ -193,7 +193,9 @@ export class UnitLayer implements Layer { if (context === null) throw new Error("2d context not supported"); this.context = context; this.transportShipTrailCanvas = document.createElement("canvas"); - this.unitTrailContext = this.transportShipTrailCanvas.getContext("2d"); + const trailContext = this.transportShipTrailCanvas.getContext("2d"); + if (trailContext === null) throw new Error("2d context not supported"); + this.unitTrailContext = trailContext; this.canvas.width = this.game.width(); this.canvas.height = this.game.height(); @@ -219,15 +221,13 @@ export class UnitLayer implements Layer { private updateUnitsSprites() { const unitsToUpdate = this.game .updatesSinceLastTick() - ?.[GameUpdateType.Unit]?.map((unit) => this.game.unit(unit.id)); - unitsToUpdate - ?.filter((UnitView) => isSpriteReady(UnitView.type())) - .forEach((unitView) => { - this.clearUnitCells(unitView); + ?.[GameUpdateType.Unit]?.map((unit) => this.game.unit(unit.id)) + ?.forEach((unitView) => { + if (unitView === undefined) return; + const ready = isSpriteReady(unitView.type()); + if (ready) this.clearUnitCells(unitView); + this.onUnitEvent(unitView); }); - unitsToUpdate?.forEach((unitView) => { - this.onUnitEvent(unitView); - }); } private clearUnitCells(unit: UnitView) { @@ -350,7 +350,7 @@ export class UnitLayer implements Layer { } private clearTrail(unit: UnitView) { - const trail = this.unitToTrail.get(unit); + const trail = this.unitToTrail.get(unit) ?? []; const rel = this.relationship(unit); for (const t of trail) { this.clearCell(this.game.x(t), this.game.y(t), this.unitTrailContext); @@ -383,7 +383,7 @@ export class UnitLayer implements Layer { } let newTrailSize = 1; - const trail = this.unitToTrail.get(unit); + const trail = this.unitToTrail.get(unit) ?? []; // It can move faster than 1 pixel, draw a line for the trail or else it will be dotted if (trail.length >= 1) { const cur = { @@ -443,7 +443,7 @@ export class UnitLayer implements Layer { if (!this.unitToTrail.has(unit)) { this.unitToTrail.set(unit, []); } - const trail = this.unitToTrail.get(unit); + const trail = this.unitToTrail.get(unit) ?? []; trail.push(unit.lastTile()); // Paint trail @@ -498,15 +498,16 @@ export class UnitLayer implements Layer { const x = this.game.x(unit.tile()); const y = this.game.y(unit.tile()); - let alternateViewColor = null; + let alternateViewColor: Colord | null = null; if (this.alternateView) { let rel = this.relationship(unit); - if (unit.type() == UnitType.TradeShip && unit.dstPortId() != null) { - const target = this.game.unit(unit.dstPortId())?.owner(); + const dstPortId = unit.dstPortId(); + if (unit.type() === UnitType.TradeShip && dstPortId !== undefined) { + const target = this.game.unit(dstPortId)?.owner(); const myPlayer = this.game.myPlayer(); - if (myPlayer != null && target != null) { - if (myPlayer == target) { + if (myPlayer !== null && target !== undefined) { + if (myPlayer === target) { rel = Relationship.Self; } else if (myPlayer.isFriendly(target)) { rel = Relationship.Ally; @@ -530,7 +531,7 @@ export class UnitLayer implements Layer { unit, this.theme, alternateViewColor ?? customTerritoryColor, - alternateViewColor, + alternateViewColor ?? undefined, ); if (unit.isActive()) {