move config to Game

This commit is contained in:
evanpelle
2024-08-24 12:12:46 -07:00
parent a9785741b7
commit bb8c24e230
11 changed files with 102 additions and 51 deletions
+37
View File
@@ -0,0 +1,37 @@
import {inherits} from "util"
import {Game} from "../../core/Game";
export class TerrainRenderer {
private canvas: HTMLCanvasElement
private context: CanvasRenderingContext2D
private imageData: ImageData
constructor(private game: Game) { }
init() {
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 = document.createElement('canvas');
const backgroundCtx = this.canvas.getContext('2d');
this.canvas.width = this.game.width();
this.canvas.height = this.game.height();
backgroundCtx.putImageData(this.imageData, 0, 0);
}
initImageData() {
const theme = this.game.config().theme()
this.game.forEachTile((tile) => {
let terrainColor = theme.terrainColor(tile)
const index = (tile.cell().y * this.game.width()) + tile.cell().x
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
})
}
}