Files
wireless-docs/world.js
2026-07-05 10:14:01 -04:00

39 lines
818 B
JavaScript

export class Block {
constructor(id, pos, state) {
this.id = id;
this.pos = pos;
this.state = state;
}
}
export class World {
constructor() {
this.blocks = new Map();
this.listeners = new Set();
}
subscribe(callback) {
this.listeners.add(callback);
return () => this.listeners.delete(callback);
}
notify() {
for (const listener of this.listeners) listener();
}
set(id, x, y, z, state = {}) {
const key = `${x},${y},${z}`;
this.blocks.set(key, new Block(id, [x, y, z], state));
this.notify();
}
del(x, y, z) {
if (this.blocks.delete(`${x},${y},${z}`)) {
this.notify();
}
}
get(x, y, z) {
return this.blocks.get(`${x},${y},${z}`);
}
}