From ad06b14388d876baa15c19ebc81aaee63d3df24c Mon Sep 17 00:00:00 2001 From: evanpelle Date: Wed, 22 Jan 2025 16:45:33 -0800 Subject: [PATCH] thread_split: have structure layer draw updated structures --- src/client/graphics/layers/StructureLayer.ts | 20 +++----------------- src/core/GameView.ts | 3 +++ src/core/configuration/DevConfig.ts | 2 +- src/core/game/GameImpl.ts | 4 ---- src/core/game/PlayerImpl.ts | 4 ++-- src/core/game/UnitImpl.ts | 6 +++--- 6 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/client/graphics/layers/StructureLayer.ts b/src/client/graphics/layers/StructureLayer.ts index 8b1165eac..c96422352 100644 --- a/src/client/graphics/layers/StructureLayer.ts +++ b/src/client/graphics/layers/StructureLayer.ts @@ -8,7 +8,7 @@ import missileSiloIcon from '../../../../resources/images/MissileSiloUnit.png'; import shieldIcon from '../../../../resources/images/ShieldIcon.png'; import cityIcon from '../../../../resources/images/CityIcon.png'; import { GameView } from "../../../core/GameView"; -import { Cell, Unit, UnitType } from "../../../core/game/Game"; +import { Cell, GameUpdateType, Unit, UnitType } from "../../../core/game/Game"; import { euclDistFN } from "../../../core/game/GameMap"; interface UnitRenderConfig { @@ -23,7 +23,6 @@ export class StructureLayer implements Layer { private unitImages: Map = new Map(); private theme: Theme = null; - private seenUnits = new Set(); // Configuration for supported unit types only private readonly unitConfigs: Partial> = { @@ -69,7 +68,8 @@ export class StructureLayer implements Layer { } tick() { - this.game.units().forEach(u => this.handleUnitRendering(u)); + this.game.updatesSinceLastTick()[GameUpdateType.Unit] + .forEach(u => this.handleUnitRendering(this.game.unit(u.id))); } init() { @@ -103,14 +103,6 @@ export class StructureLayer implements Layer { const unitType = unit.type(); if (!this.isUnitTypeSupported(unitType)) return; - if (unit.isActive() && this.seenUnits.has(unit)) { - // Already rendered, so don't do anything. - return; - } - if (!unit.isActive() && !this.seenUnits.has(unit)) { - // Has been deleted and render is cleared so don't do anything. - return; - } const config = this.unitConfigs[unitType]; const unitImage = this.unitImages.get(unitType); @@ -122,12 +114,6 @@ export class StructureLayer implements Layer { this.clearCell(new Cell(this.game.x(tile), this.game.y(tile))); } - if (!unit.isActive()) { - this.seenUnits.delete(unit); - return; - } - this.seenUnits.add(unit); - // Create temporary canvas for icon processing const tempCanvas = document.createElement('canvas'); const tempContext = tempCanvas.getContext('2d'); diff --git a/src/core/GameView.ts b/src/core/GameView.ts index 268adaf4e..1e1d90bba 100644 --- a/src/core/GameView.ts +++ b/src/core/GameView.ts @@ -308,6 +308,9 @@ export class GameView implements GameMap { units(...types: UnitType[]): UnitView[] { return Array.from(this._units.values()) } + unit(id: number): UnitView { + return this._units.get(id) + } unitInfo(type: UnitType): UnitInfo { return this._config.unitInfo(type) } diff --git a/src/core/configuration/DevConfig.ts b/src/core/configuration/DevConfig.ts index 9a83906a4..773229cdc 100644 --- a/src/core/configuration/DevConfig.ts +++ b/src/core/configuration/DevConfig.ts @@ -26,7 +26,7 @@ export class DevConfig extends DefaultConfig { unitInfo(type: UnitType): UnitInfo { const info = super.unitInfo(type) const oldCost = info.cost - // info.cost = (p: Player) => oldCost(p) / 10000 + info.cost = (p: Player) => oldCost(p) / 10 return info } diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index d351ecc3f..1558d886a 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -389,10 +389,6 @@ export class GameImpl implements MutableGame { return false } - public fireUnitUpdateEvent(unit: Unit) { - this.addUpdate((unit as UnitImpl).toUpdate()) - } - target(targeter: Player, target: Player) { this.addUpdate({ type: GameUpdateType.TargetPlayer, diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index 939728380..17155a590 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -433,7 +433,7 @@ export class PlayerImpl implements MutablePlayer { (prev as PlayerImpl)._units = (prev as PlayerImpl)._units.filter(u => u != unit); (unit as UnitImpl)._owner = this this._units.push(unit as UnitImpl) - this.mg.fireUnitUpdateEvent(unit) + this.mg.addUpdate(unit.toUpdate()) this.mg.displayMessage(`${unit.type()} captured by ${this.displayName()}`, MessageType.ERROR, prev.id()) this.mg.displayMessage(`Captured ${unit.type()} from ${prev.displayName()}`, MessageType.SUCCESS, this.id()) } @@ -444,7 +444,7 @@ export class PlayerImpl implements MutablePlayer { this._units.push(b); this.removeGold(cost) this.removeTroops(troops) - this.mg.fireUnitUpdateEvent(b); + this.mg.addUpdate(b.toUpdate()); return b; } diff --git a/src/core/game/UnitImpl.ts b/src/core/game/UnitImpl.ts index 2a2c1e617..15e833f56 100644 --- a/src/core/game/UnitImpl.ts +++ b/src/core/game/UnitImpl.ts @@ -53,7 +53,7 @@ export class UnitImpl implements MutableUnit { } this._lastTile = this._tile this._tile = tile; - this.mg.fireUnitUpdateEvent(this); + this.mg.addUpdate(this.toUpdate()); } setTroops(troops: number): void { this._troops = troops; @@ -82,7 +82,7 @@ export class UnitImpl implements MutableUnit { const oldOwner = this._owner oldOwner._units = oldOwner._units.filter(u => u != this) this._owner = newOwner as PlayerImpl - this.mg.fireUnitUpdateEvent(this) + this.mg.addUpdate(this.toUpdate()) this.mg.displayMessage( `Your ${this.type()} was captured by ${newOwner.displayName()}`, MessageType.ERROR, @@ -105,7 +105,7 @@ export class UnitImpl implements MutableUnit { } this._owner._units = this._owner._units.filter(b => b != this); this._active = false; - this.mg.fireUnitUpdateEvent(this); + this.mg.addUpdate(this.toUpdate()); if (displayMessage) { this.mg.displayMessage(`Your ${this.type()} was destroyed`, MessageType.ERROR, this.owner().id()) }