2024-08-10 23:13:20 +02:00
|
|
|
export class Painter {
|
|
|
|
constructor(paintArea) {
|
|
|
|
this.paintArea = paintArea;
|
|
|
|
}
|
|
|
|
|
2024-08-15 20:01:23 +02:00
|
|
|
renderBrushToWall(haku, centerX, centerY, wall) {
|
|
|
|
let evalResult = haku.evalBrush();
|
2024-08-19 23:56:11 +02:00
|
|
|
if (evalResult.status != "ok")
|
|
|
|
return { status: "error", phase: "eval", result: evalResult };
|
2024-08-15 20:01:23 +02:00
|
|
|
|
|
|
|
let left = centerX - this.paintArea / 2;
|
|
|
|
let top = centerY - this.paintArea / 2;
|
|
|
|
|
|
|
|
let leftChunk = Math.floor(left / wall.chunkSize);
|
|
|
|
let topChunk = Math.floor(top / wall.chunkSize);
|
|
|
|
let rightChunk = Math.ceil((left + this.paintArea) / wall.chunkSize);
|
|
|
|
let bottomChunk = Math.ceil((top + this.paintArea) / wall.chunkSize);
|
|
|
|
for (let chunkY = topChunk; chunkY < bottomChunk; ++chunkY) {
|
|
|
|
for (let chunkX = leftChunk; chunkX < rightChunk; ++chunkX) {
|
|
|
|
let x = Math.floor(-chunkX * wall.chunkSize + centerX);
|
|
|
|
let y = Math.floor(-chunkY * wall.chunkSize + centerY);
|
|
|
|
let chunk = wall.getOrCreateChunk(chunkX, chunkY);
|
2024-08-10 23:13:20 +02:00
|
|
|
|
2024-08-15 20:01:23 +02:00
|
|
|
let renderResult = haku.renderValue(chunk.pixmap, x, y);
|
2024-08-19 23:56:11 +02:00
|
|
|
if (renderResult.status != "ok") {
|
|
|
|
haku.resetVm();
|
|
|
|
return { status: "error", phase: "render", result: renderResult };
|
|
|
|
}
|
2024-08-15 20:01:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
haku.resetVm();
|
2024-08-10 23:13:20 +02:00
|
|
|
|
2024-08-15 20:01:23 +02:00
|
|
|
for (let y = topChunk; y < bottomChunk; ++y) {
|
|
|
|
for (let x = leftChunk; x < rightChunk; ++x) {
|
|
|
|
let chunk = wall.getChunk(x, y);
|
|
|
|
chunk.syncFromPixmap();
|
|
|
|
}
|
|
|
|
}
|
2024-08-19 23:56:11 +02:00
|
|
|
|
|
|
|
return { status: "ok" };
|
2024-08-10 23:13:20 +02:00
|
|
|
}
|
|
|
|
}
|