Enhance TerritoryLayer with palette signature management

- Added `lastPaletteSignature` to track palette changes.
- Implemented `computePaletteSignature()` to generate a unique signature based on player views and settings.
- Introduced `refreshPaletteIfNeeded()` to refresh the palette when the signature changes, ensuring visual consistency in the TerritoryLayer.
This commit is contained in:
scamiv
2026-05-26 19:56:02 +02:00
parent 64a72c21e9
commit 61c9e9d819
@@ -65,6 +65,7 @@ export class TerritoryLayer implements Layer {
private lastFocusedPlayer: PlayerView | null = null;
private lastMyPlayerSmallId: number | null = null;
private lastPaletteSignature: string | null = null;
constructor(
private game: GameView,
@@ -98,6 +99,7 @@ export class TerritoryLayer implements Layer {
this.cachedTerritoryPatternsEnabled = patternsEnabled;
this.redraw();
}
this.refreshPaletteIfNeeded();
this.game.recentlyUpdatedTiles().forEach((t) => {
this.enqueueTile(t);
@@ -459,6 +461,7 @@ export class TerritoryLayer implements Layer {
strategy.setHoverHighlightOptions(this.hoverHighlightOptions());
strategy.setHover(this.highlightedTerritory?.smallID() ?? null);
this.territoryRenderer = strategy;
this.lastPaletteSignature = this.computePaletteSignature();
return;
}
@@ -467,6 +470,7 @@ export class TerritoryLayer implements Layer {
this.territoryRenderer.setHoverHighlightOptions(
this.hoverHighlightOptions(),
);
this.lastPaletteSignature = null;
}
private hoverHighlightOptions() {
@@ -676,4 +680,24 @@ export class TerritoryLayer implements Layer {
ctx.fillStyle = radGrad2;
ctx.fill();
}
private computePaletteSignature(): string {
let maxSmallId = 0;
for (const player of this.game.playerViews()) {
maxSmallId = Math.max(maxSmallId, player.smallID());
}
const patternsEnabled = this.userSettings.territoryPatterns();
return `${this.game.playerViews().length}:${maxSmallId}:${patternsEnabled ? 1 : 0}`;
}
private refreshPaletteIfNeeded() {
if (!this.territoryRenderer?.isWebGL()) {
return;
}
const signature = this.computePaletteSignature();
if (signature !== this.lastPaletteSignature) {
this.lastPaletteSignature = signature;
this.territoryRenderer.refreshPalette();
}
}
}