thread_split: have structure layer draw updated structures

This commit is contained in:
evanpelle
2025-02-01 12:05:11 -08:00
committed by Evan
parent 06fd66ae9e
commit ad06b14388
6 changed files with 12 additions and 27 deletions
+3 -17
View File
@@ -8,7 +8,7 @@ import missileSiloIcon from '../../../../resources/images/MissileSiloUnit.png';
import shieldIcon from '../../../../resources/images/ShieldIcon.png'; import shieldIcon from '../../../../resources/images/ShieldIcon.png';
import cityIcon from '../../../../resources/images/CityIcon.png'; import cityIcon from '../../../../resources/images/CityIcon.png';
import { GameView } from "../../../core/GameView"; 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"; import { euclDistFN } from "../../../core/game/GameMap";
interface UnitRenderConfig { interface UnitRenderConfig {
@@ -23,7 +23,6 @@ export class StructureLayer implements Layer {
private unitImages: Map<string, HTMLImageElement> = new Map(); private unitImages: Map<string, HTMLImageElement> = new Map();
private theme: Theme = null; private theme: Theme = null;
private seenUnits = new Set<Unit>();
// Configuration for supported unit types only // Configuration for supported unit types only
private readonly unitConfigs: Partial<Record<UnitType, UnitRenderConfig>> = { private readonly unitConfigs: Partial<Record<UnitType, UnitRenderConfig>> = {
@@ -69,7 +68,8 @@ export class StructureLayer implements Layer {
} }
tick() { tick() {
this.game.units().forEach(u => this.handleUnitRendering(u)); this.game.updatesSinceLastTick()[GameUpdateType.Unit]
.forEach(u => this.handleUnitRendering(this.game.unit(u.id)));
} }
init() { init() {
@@ -103,14 +103,6 @@ export class StructureLayer implements Layer {
const unitType = unit.type(); const unitType = unit.type();
if (!this.isUnitTypeSupported(unitType)) return; 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 config = this.unitConfigs[unitType];
const unitImage = this.unitImages.get(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))); 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 // Create temporary canvas for icon processing
const tempCanvas = document.createElement('canvas'); const tempCanvas = document.createElement('canvas');
const tempContext = tempCanvas.getContext('2d'); const tempContext = tempCanvas.getContext('2d');
+3
View File
@@ -308,6 +308,9 @@ export class GameView implements GameMap {
units(...types: UnitType[]): UnitView[] { units(...types: UnitType[]): UnitView[] {
return Array.from(this._units.values()) return Array.from(this._units.values())
} }
unit(id: number): UnitView {
return this._units.get(id)
}
unitInfo(type: UnitType): UnitInfo { unitInfo(type: UnitType): UnitInfo {
return this._config.unitInfo(type) return this._config.unitInfo(type)
} }
+1 -1
View File
@@ -26,7 +26,7 @@ export class DevConfig extends DefaultConfig {
unitInfo(type: UnitType): UnitInfo { unitInfo(type: UnitType): UnitInfo {
const info = super.unitInfo(type) const info = super.unitInfo(type)
const oldCost = info.cost const oldCost = info.cost
// info.cost = (p: Player) => oldCost(p) / 10000 info.cost = (p: Player) => oldCost(p) / 10
return info return info
} }
-4
View File
@@ -389,10 +389,6 @@ export class GameImpl implements MutableGame {
return false return false
} }
public fireUnitUpdateEvent(unit: Unit) {
this.addUpdate((unit as UnitImpl).toUpdate())
}
target(targeter: Player, target: Player) { target(targeter: Player, target: Player) {
this.addUpdate({ this.addUpdate({
type: GameUpdateType.TargetPlayer, type: GameUpdateType.TargetPlayer,
+2 -2
View File
@@ -433,7 +433,7 @@ export class PlayerImpl implements MutablePlayer {
(prev as PlayerImpl)._units = (prev as PlayerImpl)._units.filter(u => u != unit); (prev as PlayerImpl)._units = (prev as PlayerImpl)._units.filter(u => u != unit);
(unit as UnitImpl)._owner = this (unit as UnitImpl)._owner = this
this._units.push(unit as UnitImpl) 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(`${unit.type()} captured by ${this.displayName()}`, MessageType.ERROR, prev.id())
this.mg.displayMessage(`Captured ${unit.type()} from ${prev.displayName()}`, MessageType.SUCCESS, this.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._units.push(b);
this.removeGold(cost) this.removeGold(cost)
this.removeTroops(troops) this.removeTroops(troops)
this.mg.fireUnitUpdateEvent(b); this.mg.addUpdate(b.toUpdate());
return b; return b;
} }
+3 -3
View File
@@ -53,7 +53,7 @@ export class UnitImpl implements MutableUnit {
} }
this._lastTile = this._tile this._lastTile = this._tile
this._tile = tile; this._tile = tile;
this.mg.fireUnitUpdateEvent(this); this.mg.addUpdate(this.toUpdate());
} }
setTroops(troops: number): void { setTroops(troops: number): void {
this._troops = troops; this._troops = troops;
@@ -82,7 +82,7 @@ export class UnitImpl implements MutableUnit {
const oldOwner = this._owner const oldOwner = this._owner
oldOwner._units = oldOwner._units.filter(u => u != this) oldOwner._units = oldOwner._units.filter(u => u != this)
this._owner = newOwner as PlayerImpl this._owner = newOwner as PlayerImpl
this.mg.fireUnitUpdateEvent(this) this.mg.addUpdate(this.toUpdate())
this.mg.displayMessage( this.mg.displayMessage(
`Your ${this.type()} was captured by ${newOwner.displayName()}`, `Your ${this.type()} was captured by ${newOwner.displayName()}`,
MessageType.ERROR, MessageType.ERROR,
@@ -105,7 +105,7 @@ export class UnitImpl implements MutableUnit {
} }
this._owner._units = this._owner._units.filter(b => b != this); this._owner._units = this._owner._units.filter(b => b != this);
this._active = false; this._active = false;
this.mg.fireUnitUpdateEvent(this); this.mg.addUpdate(this.toUpdate());
if (displayMessage) { if (displayMessage) {
this.mg.displayMessage(`Your ${this.type()} was destroyed`, MessageType.ERROR, this.owner().id()) this.mg.displayMessage(`Your ${this.type()} was destroyed`, MessageType.ERROR, this.owner().id())
} }