This commit is contained in:
David Allemang
2026-07-04 13:24:41 -04:00
parent a92ac6d736
commit 9e05d39044
3 changed files with 75 additions and 2 deletions

View File

@@ -1,16 +1,30 @@
// world.js
export class World {
constructor() {
this.blocks = new Map(); // Keyed by "x,y,z"
this.blocks = new Map();
this.listeners = new Set();
}
// Subscribe to changes (returns an unsubscribe function)
subscribe(callback) {
this.listeners.add(callback);
return () => this.listeners.delete(callback);
}
notify() {
for (const listener of this.listeners) listener();
}
setBlock(id, x, y, z, props = {}) {
const key = `${x},${y},${z}`;
this.blocks.set(key, { id, x, y, z, props });
this.notify();
}
removeBlock(x, y, z) {
this.blocks.delete(`${x},${y},${z}`);
if (this.blocks.delete(`${x},${y},${z}`)) {
this.notify();
}
}
updateBlock(x, y, z, newProps) {
@@ -18,6 +32,7 @@ export class World {
const block = this.blocks.get(key);
if (block) {
block.props = { ...block.props, ...newProps };
this.notify();
}
}