rkgk/static/wall.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-08-15 20:01:23 +02:00
import { Pixmap } from "./haku.js";
2024-08-10 23:13:20 +02:00
import { OnlineUsers } from "./online-users.js";
export class Chunk {
constructor(size) {
2024-08-15 20:01:23 +02:00
this.pixmap = new Pixmap(size, size);
2024-08-10 23:13:20 +02:00
this.canvas = new OffscreenCanvas(size, size);
this.ctx = this.canvas.getContext("2d");
2024-09-03 22:16:28 +02:00
this.renderDirty = false;
2024-08-10 23:13:20 +02:00
}
2024-08-15 20:01:23 +02:00
syncFromPixmap() {
2024-09-03 22:16:28 +02:00
this.ctx.putImageData(this.pixmap.getImageData(), 0, 0);
2024-08-15 20:01:23 +02:00
}
syncToPixmap() {
let imageData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
2024-09-03 22:16:28 +02:00
this.pixmap.getImageData().data.set(imageData.data, 0);
}
markModified() {
this.renderDirty = true;
2024-08-15 20:01:23 +02:00
}
2024-08-10 23:13:20 +02:00
}
export class Wall {
#chunks = new Map();
2024-08-15 20:01:23 +02:00
constructor(wallInfo) {
this.chunkSize = wallInfo.chunkSize;
this.onlineUsers = new OnlineUsers(wallInfo);
2024-08-10 23:13:20 +02:00
}
static chunkKey(x, y) {
return `(${x},${y})`;
}
getChunk(x, y) {
return this.#chunks.get(Wall.chunkKey(x, y));
}
getOrCreateChunk(x, y) {
let key = Wall.chunkKey(x, y);
if (this.#chunks.has(key)) {
return this.#chunks.get(key);
} else {
let chunk = new Chunk(this.chunkSize);
this.#chunks.set(key, chunk);
return chunk;
}
}
}