Files
OpenFrontIO/src/client/graphics/layers/TerrainLayer.ts
T
NewHappyRabbit e9eb006cea Added dark mode
2025-02-20 00:48:45 +02:00

66 lines
1.8 KiB
TypeScript

import { Layer } from "./Layer";
import { GameView } from "../../../core/game/GameView";
import { Theme } from "../../../core/configuration/Config";
export class TerrainLayer implements Layer {
private canvas: HTMLCanvasElement;
private context: CanvasRenderingContext2D;
private imageData: ImageData;
private theme: Theme;
constructor(private game: GameView) {}
shouldTransform(): boolean {
return true;
}
tick() {
if (this.game.config().theme() !== this.theme) {
this.redraw();
}
}
init() {
console.log("redrew terrain layer");
this.redraw();
}
redraw(): void {
this.canvas = document.createElement("canvas");
this.context = this.canvas.getContext("2d");
this.imageData = this.context.getImageData(
0,
0,
this.game.width(),
this.game.height(),
);
this.initImageData();
this.canvas.width = this.game.width();
this.canvas.height = this.game.height();
this.context.putImageData(this.imageData, 0, 0);
}
initImageData() {
this.theme = this.game.config().theme();
this.game.forEachTile((tile) => {
let terrainColor = this.theme.terrainColor(this.game, tile);
// TODO: isn'te tileref and index the same?
const index = this.game.y(tile) * this.game.width() + this.game.x(tile);
const offset = index * 4;
this.imageData.data[offset] = terrainColor.rgba.r;
this.imageData.data[offset + 1] = terrainColor.rgba.g;
this.imageData.data[offset + 2] = terrainColor.rgba.b;
this.imageData.data[offset + 3] = (terrainColor.rgba.a * 255) | 0;
});
}
renderLayer(context: CanvasRenderingContext2D) {
context.drawImage(
this.canvas,
-this.game.width() / 2,
-this.game.height() / 2,
this.game.width(),
this.game.height(),
);
}
}