42 lines
1007 B
JavaScript
42 lines
1007 B
JavaScript
// world.js
|
|
export class World {
|
|
constructor() {
|
|
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) {
|
|
if (this.blocks.delete(`${x},${y},${z}`)) {
|
|
this.notify();
|
|
}
|
|
}
|
|
|
|
updateBlock(x, y, z, newProps) {
|
|
const key = `${x},${y},${z}`;
|
|
const block = this.blocks.get(key);
|
|
if (block) {
|
|
block.props = { ...block.props, ...newProps };
|
|
this.notify();
|
|
}
|
|
}
|
|
|
|
getBlock(x, y, z) {
|
|
return this.blocks.get(`${x},${y},${z}`);
|
|
}
|
|
} |