From e395619abc0b8ad21b618f844a76d519c714bc6d Mon Sep 17 00:00:00 2001 From: Evan Date: Fri, 10 Jan 2025 20:37:30 -0800 Subject: [PATCH] update unitlayer to use new api --- src/client/graphics/layers/UnitLayer.ts | 139 ++++++++++++------------ src/core/GameRunner.ts | 2 +- src/core/GameView.ts | 5 + src/core/configuration/DevConfig.ts | 12 +- 4 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/client/graphics/layers/UnitLayer.ts b/src/client/graphics/layers/UnitLayer.ts index efc027d19..94cfd6fba 100644 --- a/src/client/graphics/layers/UnitLayer.ts +++ b/src/client/graphics/layers/UnitLayer.ts @@ -1,6 +1,6 @@ import { Colord } from "colord"; import { Theme } from "../../../core/configuration/Config"; -import { Unit, Cell, Game, Tile, UnitType, Player } from "../../../core/game/Game"; +import { Unit, Cell, Game, Tile, UnitType, Player, UnitUpdate } from "../../../core/game/Game"; import { bfs, dist, euclDist } from "../../../core/Util"; import { Layer } from "./Layer"; import { EventBus } from "../../../core/EventBus"; @@ -14,10 +14,6 @@ enum Relationship { Enemy } -class UnitEvent { - constructor(public unit: Unit, public oldTile: Tile) { } -} - export class UnitLayer implements Layer { private canvas: HTMLCanvasElement; private context: CanvasRenderingContext2D; @@ -46,13 +42,12 @@ export class UnitLayer implements Layer { if (this.myPlayer == null) { this.myPlayer = this.game.playerByClientID(this.clientID) } - for (const unit of this.game.units()) { - this.onUnitEvent(new UnitEvent(unit, unit.tile())) + for (const unit of this.game.recentlyUpdatedUnits()) { + this.onUnitEvent(unit) } } init() { - this.eventBus.on(UnitEvent, e => this.onUnitEvent(e)); this.eventBus.on(AlternateViewEvent, e => this.onAlternativeViewEvent(e)) this.redraw() } @@ -97,125 +92,125 @@ export class UnitLayer implements Layer { return Relationship.Enemy } - onUnitEvent(event: UnitEvent) { - switch (event.unit.type()) { + onUnitEvent(unit: Unit) { + switch (unit.type()) { case UnitType.TransportShip: - this.handleBoatEvent(event); + this.handleBoatEvent(unit); break; case UnitType.Destroyer: - this.handleDestroyerEvent(event); + this.handleDestroyerEvent(unit); break; case UnitType.Battleship: - this.handleBattleshipEvent(event); + this.handleBattleshipEvent(unit); break; case UnitType.Shell: - this.handleShellEvent(event) + this.handleShellEvent(unit) break; case UnitType.TradeShip: - this.handleTradeShipEvent(event) + this.handleTradeShipEvent(unit) break; case UnitType.AtomBomb: case UnitType.HydrogenBomb: - this.handleNuke(event) + this.handleNuke(unit) break } } - private handleDestroyerEvent(event: UnitEvent) { - const rel = this.relationship(event.unit) - bfs(event.oldTile, euclDist(event.oldTile, 4)).forEach(t => { + private handleDestroyerEvent(unit: Unit) { + const rel = this.relationship(unit) + bfs(unit.lastTile(), euclDist(unit.lastTile(), 4)).forEach(t => { this.clearCell(t.cell()); }); - if (!event.unit.isActive()) { + if (!unit.isActive()) { return } - bfs(event.unit.tile(), euclDist(event.unit.tile(), 4)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255)); - bfs(event.unit.tile(), dist(event.unit.tile(), 3)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 255)); + bfs(unit.tile(), euclDist(unit.tile(), 4)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(unit.owner().info()), 255)); + bfs(unit.tile(), dist(unit.tile(), 3)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 255)); } - private handleBattleshipEvent(event: UnitEvent) { - const rel = this.relationship(event.unit) - bfs(event.oldTile, euclDist(event.oldTile, 6)).forEach(t => { + private handleBattleshipEvent(unit: Unit) { + const rel = this.relationship(unit) + bfs(unit.lastTile(), euclDist(unit.lastTile(), 6)).forEach(t => { this.clearCell(t.cell()); }); - if (!event.unit.isActive()) { + if (!unit.isActive()) { return } - bfs(event.unit.tile(), euclDist(event.unit.tile(), 5)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 255)); - bfs(event.unit.tile(), dist(event.unit.tile(), 4)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255)); - bfs(event.unit.tile(), euclDist(event.unit.tile(), 1)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 255)); + bfs(unit.tile(), euclDist(unit.tile(), 5)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 255)); + bfs(unit.tile(), dist(unit.tile(), 4)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(unit.owner().info()), 255)); + bfs(unit.tile(), euclDist(unit.tile(), 1)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 255)); } - private handleShellEvent(event: UnitEvent) { - const rel = this.relationship(event.unit) + private handleShellEvent(unit: Unit) { + const rel = this.relationship(unit) - this.clearCell(event.oldTile.cell()) - if (this.oldShellTile.has(event.unit)) { - this.clearCell(this.oldShellTile.get(event.unit).cell()) + this.clearCell(unit.lastTile().cell()) + if (this.oldShellTile.has(unit)) { + this.clearCell(this.oldShellTile.get(unit).cell()) } - this.oldShellTile.set(event.unit, event.oldTile) - if (!event.unit.isActive()) { + this.oldShellTile.set(unit, unit.lastTile()) + if (!unit.isActive()) { return } - this.paintCell(event.unit.tile().cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255) - this.paintCell(event.oldTile.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255) + this.paintCell(unit.tile().cell(), rel, this.theme.borderColor(unit.owner().info()), 255) + this.paintCell(unit.lastTile().cell(), rel, this.theme.borderColor(unit.owner().info()), 255) } - private handleNuke(event: UnitEvent) { - const rel = this.relationship(event.unit) - bfs(event.oldTile, euclDist(event.oldTile, 2)).forEach(t => { + private handleNuke(unit: Unit) { + const rel = this.relationship(unit) + bfs(unit.lastTile(), euclDist(unit.lastTile(), 2)).forEach(t => { this.clearCell(t.cell()); }); - if (event.unit.isActive()) { - bfs(event.unit.tile(), euclDist(event.unit.tile(), 2)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255)); + if (unit.isActive()) { + bfs(unit.tile(), euclDist(unit.tile(), 2)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(unit.owner().info()), 255)); } } - private handleTradeShipEvent(event: UnitEvent) { - const rel = this.relationship(event.unit) - bfs(event.oldTile, euclDist(event.oldTile, 3)).forEach(t => { + private handleTradeShipEvent(unit: Unit) { + const rel = this.relationship(unit) + bfs(unit.lastTile(), euclDist(unit.lastTile(), 3)).forEach(t => { this.clearCell(t.cell()); }); - if (event.unit.isActive()) { - bfs(event.unit.tile(), dist(event.unit.tile(), 2)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 255)); + if (unit.isActive()) { + bfs(unit.tile(), dist(unit.tile(), 2)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 255)); } - if (event.unit.isActive()) { - bfs(event.unit.tile(), dist(event.unit.tile(), 1)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255)); + if (unit.isActive()) { + bfs(unit.tile(), dist(unit.tile(), 1)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(unit.owner().info()), 255)); } } - private handleBoatEvent(event: UnitEvent) { - const rel = this.relationship(event.unit) - if (!this.boatToTrail.has(event.unit)) { - this.boatToTrail.set(event.unit, new Set()); + private handleBoatEvent(unit: Unit) { + const rel = this.relationship(unit) + if (!this.boatToTrail.has(unit)) { + this.boatToTrail.set(unit, new Set()); } - const trail = this.boatToTrail.get(event.unit); - trail.add(event.oldTile); - bfs(event.oldTile, dist(event.oldTile, 3)).forEach(t => { + const trail = this.boatToTrail.get(unit); + trail.add(unit.lastTile()); + bfs(unit.lastTile(), dist(unit.lastTile(), 3)).forEach(t => { this.clearCell(t.cell()); }); - if (event.unit.isActive()) { + if (unit.isActive()) { for (const t of trail) { - this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 150); + this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 150); } - bfs(event.unit.tile(), dist(event.unit.tile(), 2)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(event.unit.owner().info()), 255)); - bfs(event.unit.tile(), dist(event.unit.tile(), 1)) - .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(event.unit.owner().info()), 255)); + bfs(unit.tile(), dist(unit.tile(), 2)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.borderColor(unit.owner().info()), 255)); + bfs(unit.tile(), dist(unit.tile(), 1)) + .forEach(t => this.paintCell(t.cell(), rel, this.theme.territoryColor(unit.owner().info()), 255)); } else { trail.forEach(t => this.clearCell(t.cell())); - this.boatToTrail.delete(event.unit); + this.boatToTrail.delete(unit); } } diff --git a/src/core/GameRunner.ts b/src/core/GameRunner.ts index edc96801b..4f0085df3 100644 --- a/src/core/GameRunner.ts +++ b/src/core/GameRunner.ts @@ -76,7 +76,7 @@ export class GameRunner { this.callBack({ tick: this.game.ticks(), - units: this.game.units().map(u => u.toUpdate()), + units: updates.filter(u => u.type == GameUpdateType.Unit), packedTileUpdates: updates.filter(u => u.type == GameUpdateType.Tile).map(u => packTileData(u)), players: playerViewData }) diff --git a/src/core/GameView.ts b/src/core/GameView.ts index 4e02dcf48..c14bb86ac 100644 --- a/src/core/GameView.ts +++ b/src/core/GameView.ts @@ -232,6 +232,7 @@ export class GameView { private _players = new Map() private _units = new Map() private updatedTiles: TileView[] = [] + private updatedUnits: UnitView[] = [] constructor(public worker: WorkerClient, private _config: Config, private _terrainMap: TerrainMap) { // Initialize the 2D array @@ -276,11 +277,15 @@ export class GameView { this._units.set(unit.id, new UnitView(this, unit)) } }) + this.updatedUnits = gu.units.map(u => this._units.get(u.id)) } recentlyUpdatedTiles(): TileView[] { return this.updatedTiles } + recentlyUpdatedUnits(): UnitView[] { + return this.updatedUnits + } player(id: PlayerID): PlayerView { if (this._players.has(id)) { diff --git a/src/core/configuration/DevConfig.ts b/src/core/configuration/DevConfig.ts index 59d83dc33..7e4999d6f 100644 --- a/src/core/configuration/DevConfig.ts +++ b/src/core/configuration/DevConfig.ts @@ -23,12 +23,12 @@ export class DevConfig extends DefaultConfig { // return 100 } - // unitInfo(type: UnitType): UnitInfo { - // const info = super.unitInfo(type) - // const oldCost = info.cost - // info.cost = (p: Player) => oldCost(p) / 10000 - // return info - // } + unitInfo(type: UnitType): UnitInfo { + const info = super.unitInfo(type) + const oldCost = info.cost + info.cost = (p: Player) => oldCost(p) / 10000 + return info + } // tradeShipSpawnRate(): number { // return 10